Skip to content

Commit

Permalink
fix(migrator): Updating the when the transaction should error (#38)
Browse files Browse the repository at this point in the history
Updating the transation errs
  • Loading branch information
Jacobbrewer1 authored Oct 30, 2024
1 parent 51cef94 commit 9adad53
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 4 additions & 3 deletions pkg/migrations/up.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package migrations

import (
"database/sql"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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()))
}
}()
Expand All @@ -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()
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions pkg/migrations/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9adad53

Please sign in to comment.