use dbx.SQLOFunc instead of sql.DB

This commit is contained in:
Elijah Duffy
2025-06-05 14:22:15 -07:00
parent 4d251ffbcf
commit f35bbfcbad
2 changed files with 20 additions and 21 deletions

View File

@@ -2,18 +2,18 @@ package migratecli
import ( import (
"context" "context"
"database/sql"
"fmt" "fmt"
"gitea.auvem.com/go-toolkit/app" "gitea.auvem.com/go-toolkit/app"
"gitea.auvem.com/go-toolkit/appcli" "gitea.auvem.com/go-toolkit/appcli"
"gitea.auvem.com/go-toolkit/dbx"
"gitea.auvem.com/go-toolkit/migrate" "gitea.auvem.com/go-toolkit/migrate"
"github.com/pressly/goose/v3" "github.com/pressly/goose/v3"
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
) )
// MigrateCmd returns the main migrate command. // MigrateCmd returns the main migrate command.
func MigrateCmd(sqlo *sql.DB, directDeps []*app.Module, childDeps []*app.Module) *cli.Command { func MigrateCmd(sqlo dbx.SQLOFunc, directDeps []*app.Module, childDeps []*app.Module) *cli.Command {
return appcli.NewCommand(&cli.Command{ return appcli.NewCommand(&cli.Command{
Name: "migrate", Name: "migrate",
Usage: "Migrate the database", Usage: "Migrate the database",
@@ -22,7 +22,7 @@ func MigrateCmd(sqlo *sql.DB, directDeps []*app.Module, childDeps []*app.Module)
} }
// AllSubcommands returns all subcommands of the migrate command. // AllSubcommands returns all subcommands of the migrate command.
func AllSubcommands(sqlo *sql.DB, deps ...*app.Module) []*cli.Command { func AllSubcommands(sqlo dbx.SQLOFunc, deps ...*app.Module) []*cli.Command {
return []*cli.Command{ return []*cli.Command{
MigrateStatusCmd(sqlo, deps...), MigrateStatusCmd(sqlo, deps...),
MigrateCreateCmd(sqlo, deps...), MigrateCreateCmd(sqlo, deps...),
@@ -35,12 +35,12 @@ func AllSubcommands(sqlo *sql.DB, deps ...*app.Module) []*cli.Command {
} }
// MigrateStatusCmd returns a command to get database migration status. // MigrateStatusCmd returns a command to get database migration status.
func MigrateStatusCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command { func MigrateStatusCmd(sqlo dbx.SQLOFunc, deps ...*app.Module) *cli.Command {
return appcli.NewCommand(&cli.Command{ return appcli.NewCommand(&cli.Command{
Name: "status", Name: "status",
Usage: "Get database migration status", Usage: "Get database migration status",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if err := goose.Status(sqlo, migrate.MigrationsConfig().BasePath); err != nil { if err := goose.Status(sqlo(), migrate.MigrationsConfig().BasePath); err != nil {
return fmt.Errorf("couldn't get migration status: %v", err) return fmt.Errorf("couldn't get migration status: %v", err)
} }
@@ -50,7 +50,7 @@ func MigrateStatusCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command {
} }
// MigrateCreateCmd returns a command to create a new migration. // MigrateCreateCmd returns a command to create a new migration.
func MigrateCreateCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command { func MigrateCreateCmd(sqlo dbx.SQLOFunc, deps ...*app.Module) *cli.Command {
return appcli.NewCommand(&cli.Command{ return appcli.NewCommand(&cli.Command{
Name: "create", Name: "create",
Usage: "Create a new migration", Usage: "Create a new migration",
@@ -70,7 +70,7 @@ func MigrateCreateCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command {
goose.SetSequential(sequential) goose.SetSequential(sequential)
} }
if err := goose.Create(sqlo, "migrations", cmd.StringArg("name"), cmd.StringArg("type")); err != nil { if err := goose.Create(sqlo(), "migrations", cmd.StringArg("name"), cmd.StringArg("type")); err != nil {
return fmt.Errorf("couldn't create migration: %v", err) return fmt.Errorf("couldn't create migration: %v", err)
} }
@@ -80,12 +80,12 @@ func MigrateCreateCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command {
} }
// MigrateUpCmd returns a command to apply all available database migrations. // MigrateUpCmd returns a command to apply all available database migrations.
func MigrateUpCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command { func MigrateUpCmd(sqlo dbx.SQLOFunc, deps ...*app.Module) *cli.Command {
return appcli.NewCommand(&cli.Command{ return appcli.NewCommand(&cli.Command{
Name: "up", Name: "up",
Usage: "Apply all available database migrations", Usage: "Apply all available database migrations",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if err := goose.Up(sqlo, migrate.MigrationsConfig().BasePath); err != nil { if err := goose.Up(sqlo(), migrate.MigrationsConfig().BasePath); err != nil {
return fmt.Errorf("couldn't apply migrations: %v", err) return fmt.Errorf("couldn't apply migrations: %v", err)
} }
return nil return nil
@@ -94,7 +94,7 @@ func MigrateUpCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command {
} }
// MigrateUpToCmd returns a command to apply all available database migrations up to a specific version. // MigrateUpToCmd returns a command to apply all available database migrations up to a specific version.
func MigrateUpToCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command { func MigrateUpToCmd(sqlo dbx.SQLOFunc, deps ...*app.Module) *cli.Command {
return appcli.NewCommand(&cli.Command{ return appcli.NewCommand(&cli.Command{
Name: "up-to", Name: "up-to",
Usage: "Apply all available database migrations up to a specific version", Usage: "Apply all available database migrations up to a specific version",
@@ -106,7 +106,7 @@ func MigrateUpToCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command {
}, },
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
version := cmd.Int64("version") version := cmd.Int64("version")
if err := goose.UpTo(sqlo, migrate.MigrationsConfig().BasePath, version); err != nil { if err := goose.UpTo(sqlo(), migrate.MigrationsConfig().BasePath, version); err != nil {
return fmt.Errorf("couldn't apply migrations to target version %d: %v", version, err) return fmt.Errorf("couldn't apply migrations to target version %d: %v", version, err)
} }
return nil return nil
@@ -115,12 +115,12 @@ func MigrateUpToCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command {
} }
// MigrateDownCmd returns a command to rollback the most recent database migration. // MigrateDownCmd returns a command to rollback the most recent database migration.
func MigrateDownCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command { func MigrateDownCmd(sqlo dbx.SQLOFunc, deps ...*app.Module) *cli.Command {
return appcli.NewCommand(&cli.Command{ return appcli.NewCommand(&cli.Command{
Name: "down", Name: "down",
Usage: "Rollback the most recent database migration", Usage: "Rollback the most recent database migration",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if err := goose.Down(sqlo, migrate.MigrationsConfig().BasePath); err != nil { if err := goose.Down(sqlo(), migrate.MigrationsConfig().BasePath); err != nil {
return fmt.Errorf("couldn't rollback migration: %v", err) return fmt.Errorf("couldn't rollback migration: %v", err)
} }
return nil return nil
@@ -129,7 +129,7 @@ func MigrateDownCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command {
} }
// MigrateDownToCmd returns a command to rollback all database migrations down to a specific version. // MigrateDownToCmd returns a command to rollback all database migrations down to a specific version.
func MigrateDownToCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command { func MigrateDownToCmd(sqlo dbx.SQLOFunc, deps ...*app.Module) *cli.Command {
return appcli.NewCommand(&cli.Command{ return appcli.NewCommand(&cli.Command{
Name: "down-to", Name: "down-to",
Usage: "Rollback all database migrations down to a specific version", Usage: "Rollback all database migrations down to a specific version",
@@ -141,7 +141,7 @@ func MigrateDownToCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command {
}, },
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
version := cmd.Int64("version") version := cmd.Int64("version")
if err := goose.DownTo(sqlo, migrate.MigrationsConfig().BasePath, version); err != nil { if err := goose.DownTo(sqlo(), migrate.MigrationsConfig().BasePath, version); err != nil {
return fmt.Errorf("couldn't rollback migrations to target version %d: %v", version, err) return fmt.Errorf("couldn't rollback migrations to target version %d: %v", version, err)
} }
return nil return nil
@@ -150,12 +150,12 @@ func MigrateDownToCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command {
} }
// MigrateRedoCmd returns a command to rollback the most recent database migration and reapply it. // MigrateRedoCmd returns a command to rollback the most recent database migration and reapply it.
func MigrateRedoCmd(sqlo *sql.DB, deps ...*app.Module) *cli.Command { func MigrateRedoCmd(sqlo dbx.SQLOFunc, deps ...*app.Module) *cli.Command {
return appcli.NewCommand(&cli.Command{ return appcli.NewCommand(&cli.Command{
Name: "redo", Name: "redo",
Usage: "Rollback the most recent database migration and reapply it", Usage: "Rollback the most recent database migration and reapply it",
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
if err := goose.Redo(sqlo, migrate.MigrationsConfig().BasePath); err != nil { if err := goose.Redo(sqlo(), migrate.MigrationsConfig().BasePath); err != nil {
return fmt.Errorf("couldn't redo migration: %v", err) return fmt.Errorf("couldn't redo migration: %v", err)
} }
return nil return nil

View File

@@ -2,7 +2,6 @@ package migrate
import ( import (
"context" "context"
"database/sql"
"io/fs" "io/fs"
"log/slog" "log/slog"
"time" "time"
@@ -14,8 +13,8 @@ import (
// MigrationsOpts define the options for the migrations module. // MigrationsOpts define the options for the migrations module.
type MigrationOpts struct { type MigrationOpts struct {
// SQLO is the SQL database handle used for migrations. REQUIRED. // SQLO is the SQL database handle getter used for migrations. REQUIRED.
SQLO *sql.DB SQLO dbx.SQLOFunc
// Dialect is the database dialect used for migrations (e.g., "mysql", "postgres"). // Dialect is the database dialect used for migrations (e.g., "mysql", "postgres").
// REQUIRED. Must match the dialect used in dbx. // REQUIRED. Must match the dialect used in dbx.
@@ -138,7 +137,7 @@ func setupMigrations(_ *app.Module) error {
} }
// Initialize the goose migration provider // Initialize the goose migration provider
Migration, err = goose.NewProvider(migrationsConfig.Dialect, migrationsConfig.SQLO, migrationsConfig.FS) Migration, err = goose.NewProvider(migrationsConfig.Dialect, migrationsConfig.SQLO(), migrationsConfig.FS)
if err != nil { if err != nil {
slog.Error("Couldn't initialize goose migration provider", "err", err) slog.Error("Couldn't initialize goose migration provider", "err", err)
return err return err