refactor: consolidate applyMigrations into single exported function
All checks were successful
check / check (push) Successful in 1m8s
All checks were successful
check / check (push) Successful in 1m8s
Remove the unexported applyMigrations() and the runMigrations() method. ApplyMigrations() is now the single implementation, accepting context and an optional logger. connect() calls it directly. All callers updated to pass context.Background() and nil logger.
This commit is contained in:
@@ -119,7 +119,7 @@ func (s *Database) connect(ctx context.Context) error {
|
||||
s.db = db
|
||||
s.log.Info("database connected")
|
||||
|
||||
return s.runMigrations(ctx)
|
||||
return ApplyMigrations(ctx, s.db, s.log)
|
||||
}
|
||||
|
||||
// collectMigrations reads the embedded schema directory and returns
|
||||
@@ -159,9 +159,11 @@ func ensureMigrationsTable(ctx context.Context, db *sql.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// applyMigrations applies all pending migrations to db, using log for
|
||||
// informational output (may be nil for silent operation).
|
||||
func applyMigrations(ctx context.Context, db *sql.DB, log *slog.Logger) error {
|
||||
// ApplyMigrations applies all pending migrations to db. An optional logger
|
||||
// may be provided for informational output; pass nil for silent operation.
|
||||
// This is exported so tests can apply the real schema without the full fx
|
||||
// lifecycle.
|
||||
func ApplyMigrations(ctx context.Context, db *sql.DB, log *slog.Logger) error {
|
||||
if err := ensureMigrationsTable(ctx, db); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -228,18 +230,7 @@ func applyMigrations(ctx context.Context, db *sql.DB, log *slog.Logger) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Database) runMigrations(ctx context.Context) error {
|
||||
return applyMigrations(ctx, s.db, s.log)
|
||||
}
|
||||
|
||||
// DB returns the underlying sql.DB.
|
||||
func (s *Database) DB() *sql.DB {
|
||||
return s.db
|
||||
}
|
||||
|
||||
// ApplyMigrations applies all migrations to the given database.
|
||||
// This is useful for testing where you want to use the real schema
|
||||
// without the full fx lifecycle.
|
||||
func ApplyMigrations(db *sql.DB) error {
|
||||
return applyMigrations(context.Background(), db, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user