diff --git a/pkg/migrations/up.go b/pkg/migrations/up.go index aa1f5c2..5f2618a 100644 --- a/pkg/migrations/up.go +++ b/pkg/migrations/up.go @@ -1,6 +1,7 @@ package migrations import ( + "database/sql" "errors" "fmt" "io" @@ -134,7 +135,7 @@ func (v *versioning) migrate(f os.DirEntry, direction string) error { return fmt.Errorf("error beginning transaction: %w", err) } defer func() { - if err := tx.Rollback(); err != nil { + if err := tx.Rollback(); err != nil && !errors.Is(err, sql.ErrTxDone) { slog.Error("error rolling back transaction", slog.String("error", err.Error())) } }() @@ -153,7 +154,7 @@ func (v *versioning) migrate(f os.DirEntry, direction string) error { switch direction { case up: v.mustSetCurrentVersion(prefix) - v.mustCreateHistory(prefix, up) + v.mustCreateHistory(prefix, migratedUp) case down: // Set the current version to the previous version. prev, err := v.getPreviousVersion() @@ -165,7 +166,7 @@ func (v *versioning) migrate(f os.DirEntry, direction string) error { v.mustSetCurrentVersion(prev) } - v.mustCreateHistory(prefix, down) + v.mustCreateHistory(prefix, migratedDown) } return nil diff --git a/pkg/migrations/versioning.go b/pkg/migrations/versioning.go index fa9d343..7cc3f3b 100644 --- a/pkg/migrations/versioning.go +++ b/pkg/migrations/versioning.go @@ -17,7 +17,9 @@ const ( down = "down" migratingUp = "migrating_up" migratingDown = "migrating_down" - stateError = "error" + migratedUp = "migrated_up" + migratedDown = "migrated_down" + stateError = "migration_error" FilePrefix = "20060102150405" ) @@ -150,10 +152,10 @@ func (v *versioning) createHistoryTable(schema string) error { CREATE TABLE %s.%s ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, version VARCHAR(255) NOT NULL, - action enum('up', 'down', 'migrating_up', 'migrating_down', 'error') NOT NULL, + action enum('%s', '%s', '%s', '%s', '%s') NOT NULL, created_at TIMESTAMP ); -`, schema, historyTable) +`, schema, historyTable, migratingUp, migratingDown, migratedUp, migratedDown, stateError) _, err := v.db.Exec(sqlStmt) if err != nil {