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

@@ -1,17 +1,17 @@
package migratecmd
import (
"database/sql"
"fmt"
"strconv"
"gitea.auvem.com/go-toolkit/dbx"
"gitea.auvem.com/go-toolkit/migrate"
"github.com/pressly/goose/v3"
"github.com/spf13/cobra"
)
// MigrateCmd returns the main migrate command.
func MigrateCmd() *cobra.Command {
func MigrateCmd(sqlo *sql.DB) *cobra.Command {
cmd := &cobra.Command{
Use: "migrate",
Short: "Migrate the database",
@@ -20,31 +20,31 @@ func MigrateCmd() *cobra.Command {
},
}
cmd.AddCommand(AllSubcommands()...)
cmd.AddCommand(AllSubcommands(sqlo)...)
return cmd
}
// AllSubcommands returns all subcommands of the migrate command.
func AllSubcommands() []*cobra.Command {
func AllSubcommands(sqlo *sql.DB) []*cobra.Command {
return []*cobra.Command{
MigrateStatusCmd(),
MigrateCreateCmd(),
MigrateUpCmd(),
MigrateUpToCmd(),
MigrateDownCmd(),
MigrateDownToCmd(),
MigrateRedoCmd(),
MigrateStatusCmd(sqlo),
MigrateCreateCmd(sqlo),
MigrateUpCmd(sqlo),
MigrateUpToCmd(sqlo),
MigrateDownCmd(sqlo),
MigrateDownToCmd(sqlo),
MigrateRedoCmd(sqlo),
}
}
// MigrateStatusCmd returns a command to get database migration status.
func MigrateStatusCmd() *cobra.Command {
func MigrateStatusCmd(sqlo *sql.DB) *cobra.Command {
return &cobra.Command{
Use: "status",
Short: "Get database migration status",
Run: func(cmd *cobra.Command, args []string) {
if err := goose.Status(dbx.SQLO(), migrate.MigrationsConfig().BasePath); err != nil {
if err := goose.Status(sqlo, migrate.MigrationsConfig().BasePath); err != nil {
fmt.Printf("Error: Couldn't get migration status: %v\n", err)
return
}
@@ -53,7 +53,7 @@ func MigrateStatusCmd() *cobra.Command {
}
// MigrateCreateCmd returns a command to create a new migration.
func MigrateCreateCmd() *cobra.Command {
func MigrateCreateCmd(sqlo *sql.DB) *cobra.Command {
cmd := &cobra.Command{
Use: "create [NAME] [TYPE]",
Short: "Create a new migration",
@@ -69,7 +69,7 @@ func MigrateCreateCmd() *cobra.Command {
goose.SetSequential(sequential)
}
if err := goose.Create(dbx.SQLO(), "migrations", args[0], args[1]); err != nil {
if err := goose.Create(sqlo, "migrations", args[0], args[1]); err != nil {
fmt.Printf("Error: Couldn't create migration: %v\n", err)
return
}
@@ -81,12 +81,12 @@ func MigrateCreateCmd() *cobra.Command {
}
// MigrateUpCmd returns a command to apply all available database migrations.
func MigrateUpCmd() *cobra.Command {
func MigrateUpCmd(sqlo *sql.DB) *cobra.Command {
return &cobra.Command{
Use: "up",
Short: "Apply all available database migrations",
Run: func(cmd *cobra.Command, args []string) {
if err := goose.Up(dbx.SQLO(), migrate.MigrationsConfig().BasePath); err != nil {
if err := goose.Up(sqlo, migrate.MigrationsConfig().BasePath); err != nil {
fmt.Printf("Error: Couldn't apply migrations: %v\n", err)
return
}
@@ -95,7 +95,7 @@ func MigrateUpCmd() *cobra.Command {
}
// MigrateUpToCmd returns a command to apply all available database migrations up to a specific version.
func MigrateUpToCmd() *cobra.Command {
func MigrateUpToCmd(sqlo *sql.DB) *cobra.Command {
return &cobra.Command{
Use: "up-to [VERSION]",
Short: "Apply all available database migrations up to a specific version",
@@ -107,7 +107,7 @@ func MigrateUpToCmd() *cobra.Command {
return
}
if err := goose.UpTo(dbx.SQLO(), migrate.MigrationsConfig().BasePath, version); err != nil {
if err := goose.UpTo(sqlo, migrate.MigrationsConfig().BasePath, version); err != nil {
fmt.Printf("Error: Couldn't apply migrations to target version %d: %v\n", version, err)
return
}
@@ -116,12 +116,12 @@ func MigrateUpToCmd() *cobra.Command {
}
// MigrateDownCmd returns a command to rollback the most recent database migration.
func MigrateDownCmd() *cobra.Command {
func MigrateDownCmd(sqlo *sql.DB) *cobra.Command {
return &cobra.Command{
Use: "down",
Short: "Rollback the most recent database migration",
Run: func(cmd *cobra.Command, args []string) {
if err := goose.Down(dbx.SQLO(), migrate.MigrationsConfig().BasePath); err != nil {
if err := goose.Down(sqlo, migrate.MigrationsConfig().BasePath); err != nil {
fmt.Printf("Error: Couldn't rollback migration: %v\n", err)
return
}
@@ -130,7 +130,7 @@ func MigrateDownCmd() *cobra.Command {
}
// MigrateDownToCmd returns a command to rollback all database migrations down to a specific version.
func MigrateDownToCmd() *cobra.Command {
func MigrateDownToCmd(sqlo *sql.DB) *cobra.Command {
return &cobra.Command{
Use: "down-to [VERSION]",
Short: "Rollback all database migrations down to a specific version",
@@ -142,7 +142,7 @@ func MigrateDownToCmd() *cobra.Command {
return
}
if err := goose.DownTo(dbx.SQLO(), migrate.MigrationsConfig().BasePath, version); err != nil {
if err := goose.DownTo(sqlo, migrate.MigrationsConfig().BasePath, version); err != nil {
fmt.Printf("Error: Couldn't rollback migrations to target version %d: %v\n", version, err)
return
}
@@ -151,12 +151,12 @@ func MigrateDownToCmd() *cobra.Command {
}
// MigrateRedoCmd returns a command to rollback the most recent database migration and reapply it.
func MigrateRedoCmd() *cobra.Command {
func MigrateRedoCmd(sqlo *sql.DB) *cobra.Command {
return &cobra.Command{
Use: "redo",
Short: "Rollback the most recent database migration and reapply it",
Run: func(cmd *cobra.Command, args []string) {
if err := goose.Redo(dbx.SQLO(), migrate.MigrationsConfig().BasePath); err != nil {
if err := goose.Redo(sqlo, migrate.MigrationsConfig().BasePath); err != nil {
fmt.Printf("Error: Couldn't redo migration: %v\n", err)
return
}