@@ 54,29 54,29 @@ func NewEngine(db *sql.DB, migrations []
// `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 @@ func (s *MigrationEngine) Migrate(ctx co
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 @@ func (s *MigrationEngine) Migrate(ctx co
// 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 @@ func (s *MigrationEngine) createMigratio
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 @@ func (s *MigrationEngine) runMigration(c
}
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 @@ func (s *MigrationEngine) runMigration(c
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 @@ func (s *MigrationEngine) runRollback(ct
}
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)