From e70aa4675b0251729b08a41e96679fc993ccee4c Mon Sep 17 00:00:00 2001 From: Elijah Duffy Date: Mon, 9 Jun 2025 13:37:25 -0700 Subject: [PATCH] add support for config generator function --- migrate.go | 59 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/migrate.go b/migrate.go index 361c7cb..d59ca8d 100644 --- a/migrate.go +++ b/migrate.go @@ -47,7 +47,9 @@ var ( Migration *goose.Provider // 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 *app.Module @@ -72,28 +74,27 @@ var ( ) // MigrationsConfig returns the current configuration for the migrations module. -func MigrationsConfig() MigrationOpts { +func MigrationsConfig() *MigrationOpts { migrationsModule.RequireLoaded() // ensure the migrations module is loaded + // Since the module is loaded (setup is called), we can safely return the config. return migrationsConfig } // ModuleMigrations returns the migrations module with the provided configuration. 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 { - panic("ModuleMigrations initialized multiple times") + panic("ModuleMigrationsFn initialized multiple times") } - 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 configuration at package level + // Store configuration function at package level + migrationsConfigFn = cfgFn migrationsModule = app.NewModule(ModuleMigrationsName, app.ModuleOpts{ Setup: setupMigrations, @@ -139,23 +140,39 @@ func ModuleAutoMigrate(enabled bool) *app.Module { } // setupMigrations initializes the goose migration provider. -func setupMigrations(_ *app.Module) error { - var err error +func setupMigrations(mod *app.Module) error { + if migrationsConfigFn == nil { + panic("Migrations configuration is not set") + } - if err := goose.SetDialect(string(migrationsConfig.Dialect)); err != nil { - migrationsModule.Logger().Error("Couldn't set database dialect for goose", "err", err) + 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 + if err := goose.SetDialect(string(cfg.Dialect)); err != nil { + mod.Logger().Error("Couldn't set database dialect for goose", "err", err) return err } // Set base filesystem for goose migrations - goose.SetBaseFS(migrationsConfig.FS) + goose.SetBaseFS(cfg.FS) // 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 { - migrationsModule.Logger().Error("Couldn't initialize goose migration provider", "err", err) + mod.Logger().Error("Couldn't initialize goose migration provider", "err", err) return err } + mod.Logger().Info("Goose migration provider initialized", "dialect", cfg.Dialect, "basePath", cfg.BasePath, "fs", cfg.FS) return nil }