add support for config generator function

This commit is contained in:
Elijah Duffy
2025-06-09 13:37:25 -07:00
parent f3a25af624
commit e70aa4675b

View File

@@ -47,7 +47,9 @@ 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
@@ -72,28 +74,27 @@ 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("ModuleMigrations initialized multiple times") panic("ModuleMigrationsFn initialized multiple times")
} }
if cfg.SQLO == nil { // Store configuration function at package level
panic("Migration SQL handle (SQLO) must be set in the configuration") migrationsConfigFn = cfgFn
}
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,
@@ -139,23 +140,39 @@ 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 {
var err error if migrationsConfigFn == nil {
panic("Migrations configuration is not set")
}
if err := goose.SetDialect(string(migrationsConfig.Dialect)); err != nil { cfg := migrationsConfigFn(mod)
migrationsModule.Logger().Error("Couldn't set database dialect for goose", "err", err) 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
if err := goose.SetDialect(string(cfg.Dialect)); err != nil {
mod.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) mod.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
} }