dialect agnostic approach

This commit is contained in:
Elijah Duffy
2025-06-05 13:52:55 -07:00
parent b201306152
commit 4d251ffbcf
3 changed files with 63 additions and 52 deletions

View File

@@ -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