store opts in a pointer & add logging

This commit is contained in:
Elijah Duffy
2025-06-09 13:39:31 -07:00
parent 0ab40996a1
commit ab7e65456c

View File

@@ -47,7 +47,7 @@ var (
Migration *goose.Provider Migration *goose.Provider
// migrationsConfig stores the configuration for the migrations module. // migrationsConfig stores the configuration for the migrations module.
migrationsConfig MigrationOpts migrationsConfig *MigrationOpts
// migrationsModule is the singleton module instance for the migrations subsystem. // migrationsModule is the singleton module instance for the migrations subsystem.
migrationsModule *app.Module migrationsModule *app.Module
@@ -72,13 +72,13 @@ var (
) )
// MigrationsConfig returns the current configuration for the migrations module. // MigrationsConfig returns the current configuration for the migrations module.
func MigrationsConfig() MigrationOpts { func MigrationsConfig() *MigrationOpts {
migrationsModule.RequireLoaded() // ensure the migrations module is loaded migrationsModule.RequireLoaded() // ensure the migrations module is loaded
return migrationsConfig return migrationsConfig
} }
// ModuleMigrations returns the migrations module with the provided configuration. // ModuleMigrations returns the migrations module with the provided configuration.
func ModuleMigrations(cfg MigrationOpts) *app.Module { func ModuleMigrations(cfg *MigrationOpts) *app.Module {
if migrationsModule != nil { if migrationsModule != nil {
panic("ModuleMigrations initialized multiple times") panic("ModuleMigrations initialized multiple times")
} }
@@ -92,7 +92,6 @@ func ModuleMigrations(cfg MigrationOpts) *app.Module {
if cfg.FS == nil { if cfg.FS == nil {
panic("Migration filesystem (FS) must be set in the configuration") panic("Migration filesystem (FS) must be set in the configuration")
} }
migrationsConfig = cfg // store configuration at package level migrationsConfig = cfg // store configuration at package level
migrationsModule = app.NewModule(ModuleMigrationsName, app.ModuleOpts{ migrationsModule = app.NewModule(ModuleMigrationsName, app.ModuleOpts{
@@ -139,23 +138,24 @@ func ModuleAutoMigrate(enabled bool) *app.Module {
} }
// setupMigrations initializes the goose migration provider. // setupMigrations initializes the goose migration provider.
func setupMigrations(_ *app.Module) error { func setupMigrations(mod *app.Module) error {
cfg := migrationsConfig
var err error var err error
if err := goose.SetDialect(string(cfg.Dialect)); err != nil {
if err := goose.SetDialect(string(migrationsConfig.Dialect)); err != nil {
migrationsModule.Logger().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
} }
// Set base filesystem for goose migrations // Set base filesystem for goose migrations
goose.SetBaseFS(migrationsConfig.FS) goose.SetBaseFS(cfg.FS)
// Initialize the goose migration provider // Initialize the goose migration provider
Migration, err = goose.NewProvider(migrationsConfig.Dialect, migrationsConfig.SQLO(), migrationsConfig.FS) Migration, err = goose.NewProvider(cfg.Dialect, cfg.SQLO(), cfg.FS)
if err != nil { if err != nil {
migrationsModule.Logger().Error("Couldn't initialize goose migration provider", "err", err) migrationsModule.Logger().Error("Couldn't initialize goose migration provider", "err", err)
return err return err
} }
mod.Logger().Info("Goose migration provider initialized", "dialect", cfg.Dialect, "basePath", cfg.BasePath, "fs", cfg.FS)
return nil return nil
} }