# HG changeset patch # User Peter Sanchez # Date 1645034988 21600 # Wed Feb 16 12:09:48 2022 -0600 # Node ID 81c70aad9b83c1262f26e5ec94eedda82338e9ef # Parent 5e9b5652f8024c6e15de163118f1b8335cfc28db Rename of receiver for consistency diff --git a/migrate.go b/migrate.go --- a/migrate.go +++ b/migrate.go @@ -54,29 +54,29 @@ // `fakeIt` is a flag that says whether or not to "fake" the migration. // Faking the migration means it will add the migration entry into the // `migrations` db table as if it ran but without actually running it. -func (s *MigrationEngine) Migrate(ctx context.Context, stopID string, fakeIt bool) error { - s.printf("Creating/checking migrations table...\n") +func (me *MigrationEngine) Migrate(ctx context.Context, stopID string, fakeIt bool) error { + me.printf("Creating/checking migrations table...\n") var err error // Verify given stopID exists if stopID != "" { - err = s.checkID(stopID) + err = me.checkID(stopID) if err != nil { return err } } else { - if s.verbose { - s.printf("No migration ID given, running all pending migrations...\n") + if me.verbose { + me.printf("No migration ID given, running all pending migrations...\n") } } - ctx = Context(ctx, s.db) - err = s.createMigrationTable(ctx) + ctx = Context(ctx, me.db) + err = me.createMigrationTable(ctx) if err != nil { return err } - for _, m := range s.Migrations { - _, err := s.getRecord(ctx, m.ID) + for _, m := range me.Migrations { + _, err := me.getRecord(ctx, m.ID) switch err { case sql.ErrNoRows: msg := "Running migration" @@ -84,26 +84,26 @@ msg += " [faking it]" } msg += ": %v\n" - s.printf(msg, m.ID) + me.printf(msg, m.ID) // we need to run the migration so we continue to code below case nil: - if s.verbose { - s.printf("Skipping migration (prevoiusly ran): %v\n", m.ID) + if me.verbose { + me.printf("Skipping migration (prevoiusly ran): %v\n", m.ID) } if stopID == m.ID { - s.printf("Stopped at Migration ID: %s", stopID) + me.printf("Stopped at Migration ID: %s", stopID) return nil } continue default: return fmt.Errorf("looking up migration by id: %w", err) } - err = s.runMigration(ctx, m, fakeIt) + err = me.runMigration(ctx, m, fakeIt) if err != nil { return err } if stopID == m.ID { - s.printf("Stopped at Migration ID: %s", stopID) + me.printf("Stopped at Migration ID: %s", stopID) return nil } } @@ -112,91 +112,91 @@ // Rollback will run all rollbacks using the provided db connection. // `stopID` is the migration ID to stop at (after running it) -func (s *MigrationEngine) Rollback(ctx context.Context, stopID string) error { - s.printf("Creating/checking migrations table...\n") +func (me *MigrationEngine) Rollback(ctx context.Context, stopID string) error { + me.printf("Creating/checking migrations table...\n") var err error // Verify given stopID exists if stopID != "" { - err = s.checkID(stopID) + err = me.checkID(stopID) if err != nil { return err } } else { - if s.verbose { - s.printf("No migration ID given, rolling back all migrations...\n") + if me.verbose { + me.printf("No migration ID given, rolling back all migrations...\n") } } - ctx = Context(ctx, s.db) - err = s.createMigrationTable(ctx) + ctx = Context(ctx, me.db) + err = me.createMigrationTable(ctx) if err != nil { return err } - for i := len(s.Migrations) - 1; i >= 0; i-- { - m := s.Migrations[i] + for i := len(me.Migrations) - 1; i >= 0; i-- { + m := me.Migrations[i] if m.Rollback == nil { - s.printf("Rollback not provided: %v\n", m.ID) + me.printf("Rollback not provided: %v\n", m.ID) continue } - _, err := s.getRecord(ctx, m.ID) + _, err := me.getRecord(ctx, m.ID) switch err { case sql.ErrNoRows: - if s.verbose { - s.printf("Skipping rollback (not previously ran): %v\n", m.ID) + if me.verbose { + me.printf("Skipping rollback (not previously ran): %v\n", m.ID) } if stopID == m.ID { - s.printf("Stopped at Migration ID: %s", stopID) + me.printf("Stopped at Migration ID: %s", stopID) return nil } continue case nil: - s.printf("Running rollback: %v\n", m.ID) + me.printf("Running rollback: %v\n", m.ID) // we need to run the rollback so we continue to code below default: return fmt.Errorf("looking up rollback by id: %w", err) } - err = s.runRollback(ctx, m) + err = me.runRollback(ctx, m) if err != nil { return err } if stopID == m.ID { - s.printf("Stopped at Migration ID: %s", stopID) + me.printf("Stopped at Migration ID: %s", stopID) return nil } } return nil } -func (s *MigrationEngine) checkID(ID string) error { - if _, ok := s.ids[ID]; !ok { +func (me *MigrationEngine) checkID(ID string) error { + if _, ok := me.ids[ID]; !ok { return fmt.Errorf("Migration ID '%s' not found", ID) } return nil } -func (s *MigrationEngine) rebind(query string) string { - return rebind(s.bindType, query) +func (me *MigrationEngine) rebind(query string) string { + return rebind(me.bindType, query) } -func (s *MigrationEngine) getRecord(ctx context.Context, id string) (string, error) { +func (me *MigrationEngine) getRecord(ctx context.Context, id string) (string, error) { var found string err := WithTx(ctx, txOptionsRO, func(tx *sql.Tx) error { - query := s.rebind("SELECT id FROM migrations WHERE id=?") + query := me.rebind("SELECT id FROM migrations WHERE id=?") return tx.QueryRowContext(ctx, query, id).Scan(&found) }) return found, err } -func (s *MigrationEngine) printf(format string, a ...interface{}) (n int, err error) { - printf := s.Printf +func (me *MigrationEngine) printf(format string, a ...interface{}) (n int, err error) { + printf := me.Printf if printf == nil { printf = fmt.Printf } return printf(format, a...) } -func (s *MigrationEngine) createMigrationTable(ctx context.Context) error { +func (me *MigrationEngine) createMigrationTable(ctx context.Context) error { err := WithTx(ctx, nil, func(tx *sql.Tx) error { _, err := tx.ExecContext(ctx, "CREATE TABLE IF NOT EXISTS migrations (id TEXT PRIMARY KEY)") if err != nil { @@ -207,7 +207,7 @@ return err } -func (s *MigrationEngine) runMigration(ctx context.Context, m Migration, fakeIt bool) error { +func (me *MigrationEngine) runMigration(ctx context.Context, m Migration, fakeIt bool) error { errorf := func(err error) error { return fmt.Errorf("running migration: %w", err) } var cancel context.CancelFunc @@ -217,7 +217,7 @@ } err := WithTx(ctx, nil, func(tx *sql.Tx) error { - query := s.rebind("INSERT INTO migrations (id) VALUES (?)") + query := me.rebind("INSERT INTO migrations (id) VALUES (?)") _, err := tx.ExecContext(ctx, query, m.ID) if err != nil { return errorf(err) @@ -235,7 +235,7 @@ return err } -func (s *MigrationEngine) runRollback(ctx context.Context, m Migration) error { +func (me *MigrationEngine) runRollback(ctx context.Context, m Migration) error { errorf := func(err error) error { return fmt.Errorf("running rollback: %w", err) } var cancel context.CancelFunc @@ -245,7 +245,7 @@ } err := WithTx(ctx, nil, func(tx *sql.Tx) error { - query := s.rebind("DELETE FROM migrations WHERE id=?") + query := me.rebind("DELETE FROM migrations WHERE id=?") _, err := tx.ExecContext(ctx, query, m.ID) if err != nil { return errorf(err)