dialect agnostic approach
This commit is contained in:
15
migrate.go
15
migrate.go
@@ -2,6 +2,7 @@ package migrate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"time"
|
||||
@@ -13,6 +14,13 @@ import (
|
||||
|
||||
// MigrationsOpts define the options for the migrations module.
|
||||
type MigrationOpts struct {
|
||||
// SQLO is the SQL database handle used for migrations. REQUIRED.
|
||||
SQLO *sql.DB
|
||||
|
||||
// Dialect is the database dialect used for migrations (e.g., "mysql", "postgres").
|
||||
// REQUIRED. Must match the dialect used in dbx.
|
||||
Dialect goose.Dialect
|
||||
|
||||
// FS is the filesystem where migration files are stored.
|
||||
FS fs.FS
|
||||
|
||||
@@ -82,6 +90,9 @@ func ModuleMigrations(cfg MigrationOpts) *app.Module {
|
||||
panic("ModuleMigrations 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
|
||||
}
|
||||
@@ -121,13 +132,13 @@ func ModuleAutoMigrate(enabled bool) *app.Module {
|
||||
func setupMigrations(_ *app.Module) error {
|
||||
var err error
|
||||
|
||||
if err := goose.SetDialect("mysql"); err != nil {
|
||||
if err := goose.SetDialect(string(migrationsConfig.Dialect)); err != nil {
|
||||
slog.Error("Couldn't set database dialect for goose", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Initialize the goose migration provider
|
||||
Migration, err = goose.NewProvider(goose.DialectMySQL, dbx.SQLO(), migrationsConfig.FS)
|
||||
Migration, err = goose.NewProvider(migrationsConfig.Dialect, migrationsConfig.SQLO, migrationsConfig.FS)
|
||||
if err != nil {
|
||||
slog.Error("Couldn't initialize goose migration provider", "err", err)
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user