use module logger instead of slog.Default
This commit is contained in:
60
migrate.go
60
migrate.go
@@ -61,14 +61,8 @@ var (
|
|||||||
Depends: []string{ModuleMigrationsName},
|
Depends: []string{ModuleMigrationsName},
|
||||||
})
|
})
|
||||||
|
|
||||||
// ModuleMigrateBlank resets the database to a blank state, removing all data.
|
// moduleMigrateBlank resets the database to a blank state, removing all data.
|
||||||
ModuleMigrateBlank = app.NewModule(ModuleMigrateBlankName, app.ModuleOpts{
|
moduleMigrateBlank *app.Module
|
||||||
Setup: func(_ *app.Module) error {
|
|
||||||
_, err := MigrateToBlank()
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
Depends: []string{ModuleMigrationsName},
|
|
||||||
})
|
|
||||||
|
|
||||||
// autoMigrateEnabled controls whether auto-migration is enabled.
|
// autoMigrateEnabled controls whether auto-migration is enabled.
|
||||||
autoMigrateEnabled bool
|
autoMigrateEnabled bool
|
||||||
@@ -95,6 +89,9 @@ func ModuleMigrations(cfg MigrationOpts) *app.Module {
|
|||||||
if cfg.BasePath == "" {
|
if cfg.BasePath == "" {
|
||||||
cfg.BasePath = "." // default base path if not set
|
cfg.BasePath = "." // default base path if not set
|
||||||
}
|
}
|
||||||
|
if cfg.FS == nil {
|
||||||
|
panic("Migration filesystem (FS) must be set in the configuration")
|
||||||
|
}
|
||||||
|
|
||||||
migrationsConfig = cfg // store configuration at package level
|
migrationsConfig = cfg // store configuration at package level
|
||||||
|
|
||||||
@@ -106,6 +103,23 @@ func ModuleMigrations(cfg MigrationOpts) *app.Module {
|
|||||||
return migrationsModule
|
return migrationsModule
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ModuleMigrateBlank returns the migrate blank module that resets the database to a blank state.
|
||||||
|
func ModuleMigrateBlank() *app.Module {
|
||||||
|
if moduleMigrateBlank != nil {
|
||||||
|
panic("ModuleMigrateBlank initialized multiple times")
|
||||||
|
}
|
||||||
|
|
||||||
|
moduleMigrateBlank = app.NewModule(ModuleMigrateBlankName, app.ModuleOpts{
|
||||||
|
Setup: func(_ *app.Module) error {
|
||||||
|
_, err := MigrateToBlank()
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
Depends: []string{ModuleMigrationsName},
|
||||||
|
})
|
||||||
|
|
||||||
|
return moduleMigrateBlank
|
||||||
|
}
|
||||||
|
|
||||||
// ModuleAutoMigrate returns the auto-migration module with the provided configuration.
|
// ModuleAutoMigrate returns the auto-migration module with the provided configuration.
|
||||||
func ModuleAutoMigrate(enabled bool) *app.Module {
|
func ModuleAutoMigrate(enabled bool) *app.Module {
|
||||||
if autoMigrateModule != nil {
|
if autoMigrateModule != nil {
|
||||||
@@ -129,7 +143,7 @@ func setupMigrations(_ *app.Module) error {
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
if err := goose.SetDialect(string(migrationsConfig.Dialect)); err != nil {
|
if err := goose.SetDialect(string(migrationsConfig.Dialect)); err != nil {
|
||||||
slog.Error("Couldn't set database dialect for goose", "err", err)
|
migrationsModule.Logger().Error("Couldn't set database dialect for goose", "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,7 +153,7 @@ func setupMigrations(_ *app.Module) error {
|
|||||||
// Initialize the goose migration provider
|
// Initialize the goose migration provider
|
||||||
Migration, err = goose.NewProvider(migrationsConfig.Dialect, migrationsConfig.SQLO(), migrationsConfig.FS)
|
Migration, err = goose.NewProvider(migrationsConfig.Dialect, migrationsConfig.SQLO(), migrationsConfig.FS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Couldn't initialize goose migration provider", "err", err)
|
migrationsModule.Logger().Error("Couldn't initialize goose migration provider", "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +183,7 @@ func ApplyPendingMigrations(ctx context.Context, pendingCount int64) (int64, err
|
|||||||
var count int64
|
var count int64
|
||||||
for range pendingCount {
|
for range pendingCount {
|
||||||
res, err := Migration.UpByOne(ctx)
|
res, err := Migration.UpByOne(ctx)
|
||||||
if err := handleMigrationResults(res, err); err != nil {
|
if err := handleMigrationResults(migrationsModule.Logger(), res, err); err != nil {
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,12 +205,12 @@ func MigrateToBlank() (int64, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
slog.Info("Database versions", "current", current, "target", target)
|
moduleMigrateBlank.Logger().Info("Database versions", "current", current, "target", target)
|
||||||
|
|
||||||
var count int64
|
var count int64
|
||||||
for current > 0 {
|
for current > 0 {
|
||||||
res, err := Migration.Down(ctx)
|
res, err := Migration.Down(ctx)
|
||||||
if err := handleMigrationResults(res, err); err != nil {
|
if err := handleMigrationResults(moduleMigrateBlank.Logger(), res, err); err != nil {
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,7 +228,7 @@ func AutoMigrate() error {
|
|||||||
migrationCtx := context.Background()
|
migrationCtx := context.Background()
|
||||||
migrationCurrent, migrationTarget, err := Migration.GetVersions(migrationCtx)
|
migrationCurrent, migrationTarget, err := Migration.GetVersions(migrationCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Couldn't check for pending migrations", "err", err)
|
migrationsModule.Logger().Error("Couldn't check for pending migrations", "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,22 +238,22 @@ func AutoMigrate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if migrationCurrent >= migrationTarget {
|
if migrationCurrent >= migrationTarget {
|
||||||
slog.Info("No pending migrations", "version", migrationCurrent)
|
migrationsModule.Logger().Info("No pending migrations", "version", migrationCurrent)
|
||||||
} else if !autoMigrateEnabled {
|
} else if !autoMigrateEnabled {
|
||||||
slog.Error(
|
migrationsModule.Logger().Error(
|
||||||
"Pending migrations detected, but auto-migration is disabled. Please run `acrm migrate up` to apply them.",
|
"Pending migrations detected, but auto-migration is disabled. Please run `acrm migrate up` to apply them.",
|
||||||
migrationFields...,
|
migrationFields...,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
slog.Info("Pending migrations detected, applying them...", migrationFields...)
|
migrationsModule.Logger().Info("Pending migrations detected, applying them...", migrationFields...)
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
count, err := ApplyPendingMigrations(migrationCtx, migrationTarget-migrationCurrent)
|
count, err := ApplyPendingMigrations(migrationCtx, migrationTarget-migrationCurrent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Couldn't apply pending migrations", "current", migrationCurrent+count, "target", migrationTarget, "err", err)
|
migrationsModule.Logger().Error("Couldn't apply pending migrations", "current", migrationCurrent+count, "target", migrationTarget, "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
slog.Info("Applied pending migrations", "current", migrationTarget, "appliedCount", count, "duration", time.Since(now))
|
migrationsModule.Logger().Info("Applied pending migrations", "current", migrationTarget, "appliedCount", count, "duration", time.Since(now))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -247,7 +261,7 @@ func AutoMigrate() error {
|
|||||||
|
|
||||||
// handleMigrationResults is a helper function that prints various responses
|
// handleMigrationResults is a helper function that prints various responses
|
||||||
// based on a *goose.MigrationResult.
|
// based on a *goose.MigrationResult.
|
||||||
func handleMigrationResults(res *goose.MigrationResult, err error) error {
|
func handleMigrationResults(logger *slog.Logger, res *goose.MigrationResult, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -261,12 +275,12 @@ func handleMigrationResults(res *goose.MigrationResult, err error) error {
|
|||||||
|
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
fields = append(fields, "err", res.Error)
|
fields = append(fields, "err", res.Error)
|
||||||
slog.Error("Couldn't apply migration", fields...)
|
logger.Error("Couldn't apply migration", fields...)
|
||||||
return res.Error
|
return res.Error
|
||||||
} else if res.Empty {
|
} else if res.Empty {
|
||||||
slog.Warn("Applied empty migration", fields...)
|
logger.Warn("Applied empty migration", fields...)
|
||||||
} else {
|
} else {
|
||||||
slog.Info("Applied migration", fields...)
|
logger.Info("Applied migration", fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user