Revert "add support for config generator function"

This reverts commit e70aa4675b.
This commit is contained in:
Elijah Duffy
2025-06-09 13:37:58 -07:00
parent e70aa4675b
commit 0ab40996a1

View File

@@ -47,9 +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
migrationsConfigFn func(*app.Module) *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
@@ -74,27 +72,28 @@ 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
// Since the module is loaded (setup is called), we can safely return the config.
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 {
return ModuleMigrationsFn(func(_ *app.Module) *MigrationOpts {
return &cfg
})
}
// ModuleMigrationsFn returns the migration module with the provided configuration function.
func ModuleMigrationsFn(cfgFn func(*app.Module) *MigrationOpts) *app.Module {
if migrationsModule != nil { if migrationsModule != nil {
panic("ModuleMigrationsFn initialized multiple times") panic("ModuleMigrations initialized multiple times")
} }
// Store configuration function at package level if cfg.SQLO == nil {
migrationsConfigFn = cfgFn panic("Migration SQL handle (SQLO) must be set in the configuration")
}
if cfg.BasePath == "" {
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
migrationsModule = app.NewModule(ModuleMigrationsName, app.ModuleOpts{ migrationsModule = app.NewModule(ModuleMigrationsName, app.ModuleOpts{
Setup: setupMigrations, Setup: setupMigrations,
@@ -140,39 +139,23 @@ func ModuleAutoMigrate(enabled bool) *app.Module {
} }
// setupMigrations initializes the goose migration provider. // setupMigrations initializes the goose migration provider.
func setupMigrations(mod *app.Module) error { func setupMigrations(_ *app.Module) error {
if migrationsConfigFn == nil {
panic("Migrations configuration is not set")
}
cfg := migrationsConfigFn(mod)
if cfg.SQLO == nil {
panic("Migration SQL handle (SQLO) must be set in the configuration")
}
if cfg.BasePath == "" {
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 the configuration at package level
var err error var err error
if err := goose.SetDialect(string(cfg.Dialect)); err != nil {
mod.Logger().Error("Couldn't set database dialect for goose", "err", err) if err := goose.SetDialect(string(migrationsConfig.Dialect)); err != nil {
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(cfg.FS) goose.SetBaseFS(migrationsConfig.FS)
// Initialize the goose migration provider // Initialize the goose migration provider
Migration, err = goose.NewProvider(cfg.Dialect, cfg.SQLO(), cfg.FS) Migration, err = goose.NewProvider(migrationsConfig.Dialect, migrationsConfig.SQLO(), migrationsConfig.FS)
if err != nil { if err != nil {
mod.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
} }