Revert "add support for config generator function"
This reverts commit e70aa4675b.
This commit is contained in:
59
migrate.go
59
migrate.go
@@ -47,9 +47,7 @@ var (
|
||||
Migration *goose.Provider
|
||||
|
||||
// migrationsConfig stores the configuration for the migrations module.
|
||||
migrationsConfig *MigrationOpts
|
||||
|
||||
migrationsConfigFn func(*app.Module) *MigrationOpts
|
||||
migrationsConfig MigrationOpts
|
||||
|
||||
// migrationsModule is the singleton module instance for the migrations subsystem.
|
||||
migrationsModule *app.Module
|
||||
@@ -74,27 +72,28 @@ 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("ModuleMigrationsFn initialized multiple times")
|
||||
panic("ModuleMigrations initialized multiple times")
|
||||
}
|
||||
|
||||
// Store configuration function at package level
|
||||
migrationsConfigFn = cfgFn
|
||||
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
|
||||
|
||||
migrationsModule = app.NewModule(ModuleMigrationsName, app.ModuleOpts{
|
||||
Setup: setupMigrations,
|
||||
@@ -140,39 +139,23 @@ func ModuleAutoMigrate(enabled bool) *app.Module {
|
||||
}
|
||||
|
||||
// setupMigrations initializes the goose migration provider.
|
||||
func setupMigrations(mod *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
|
||||
|
||||
func setupMigrations(_ *app.Module) 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
|
||||
}
|
||||
|
||||
// Set base filesystem for goose migrations
|
||||
goose.SetBaseFS(cfg.FS)
|
||||
goose.SetBaseFS(migrationsConfig.FS)
|
||||
|
||||
// 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 {
|
||||
mod.Logger().Error("Couldn't initialize goose migration provider", "err", err)
|
||||
migrationsModule.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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user