Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Nov 3, 2023
1 parent f944a3d commit 42b9456
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
17 changes: 5 additions & 12 deletions globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,17 @@ func ResetGlobalMigrations() {
func SetGlobalMigrations(migrations ...Migration) error {
for _, migration := range migrations {
m := &migration
if err := setGoMigration(m); err != nil {
return err
if _, ok := registeredGoMigrations[m.Version]; ok {
return fmt.Errorf("go migration with version %d already registered", m.Version)
}
if err := checkMigration(m); err != nil {
return fmt.Errorf("invalid go migration: %w", err)
}
registeredGoMigrations[m.Version] = m
}
return nil
}

func setGoMigration(m *Migration) error {
if _, ok := registeredGoMigrations[m.Version]; ok {
return fmt.Errorf("go migration with version %d already registered", m.Version)
}
if err := checkMigration(m); err != nil {
return fmt.Errorf("invalid go migration: %w", err)
}
return nil
}

func checkMigration(m *Migration) error {
if !m.construct {
return errors.New("must use NewGoMigration to construct migrations")
Expand Down
8 changes: 4 additions & 4 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func NewGoMigration(version int64, up, down *GoFunc) Migration {
// is registered.
if up != nil {
if up.RunDB != nil {
m.UpFnNoTxContext = up.RunDB // func(context.Context, *sql.DB) error
m.UpFnNoTx = func(db *sql.DB) error { return up.RunDB(context.Background(), db) }
m.UpFnNoTxContext = up.RunDB // func(context.Context, *sql.DB) error
m.UpFnNoTx = withoutContext(up.RunDB) // func(*sql.DB) error
}
if up.RunTx != nil {
m.UseTx = true
m.UpFnContext = up.RunTx // func(context.Context, *sql.Tx) error
m.UpFn = func(tx *sql.Tx) error { return up.RunTx(context.Background(), tx) }
m.UpFnContext = up.RunTx // func(context.Context, *sql.Tx) error
m.UpFn = withoutContext(up.RunTx) // func(*sql.Tx) error
}
}
if down != nil {
Expand Down
1 change: 0 additions & 1 deletion register.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func withContext[T any](fn func(T) error) func(context.Context, T) error {
if fn == nil {
return nil
}

return func(ctx context.Context, t T) error {
return fn(t)
}
Expand Down

0 comments on commit 42b9456

Please sign in to comment.