fix: unify goose Provider path and fail on pending migrations
Apply BasePath via fs.Sub, route CLI through Provider API, return ErrPendingMigrations when auto-migrate is disabled, use DownTo for blank. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+54
-46
@@ -5,14 +5,13 @@ import (
|
||||
"fmt"
|
||||
|
||||
"gitea.auvem.com/go-toolkit/appcli"
|
||||
"gitea.auvem.com/go-toolkit/dbx"
|
||||
"gitea.auvem.com/go-toolkit/migrate"
|
||||
"github.com/pressly/goose/v3"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// MigrateCmd returns the main migrate command.
|
||||
func MigrateCmd(sqlo dbx.SQLOFunc, directDeps appcli.DepFn, childDeps appcli.DepFn) *cli.Command {
|
||||
func MigrateCmd(sqlo migrate.SQLOFunc, directDeps appcli.DepFn, childDeps appcli.DepFn) *cli.Command {
|
||||
return appcli.NewCommand(&cli.Command{
|
||||
Name: "migrate",
|
||||
Usage: "Migrate the database",
|
||||
@@ -21,7 +20,7 @@ func MigrateCmd(sqlo dbx.SQLOFunc, directDeps appcli.DepFn, childDeps appcli.Dep
|
||||
}
|
||||
|
||||
// AllSubcommands returns all subcommands of the migrate command.
|
||||
func AllSubcommands(sqlo dbx.SQLOFunc, deps appcli.DepFn) []*cli.Command {
|
||||
func AllSubcommands(sqlo migrate.SQLOFunc, deps appcli.DepFn) []*cli.Command {
|
||||
return []*cli.Command{
|
||||
MigrateStatusCmd(sqlo, deps),
|
||||
MigrateCreateCmd(sqlo, deps),
|
||||
@@ -33,23 +32,27 @@ func AllSubcommands(sqlo dbx.SQLOFunc, deps appcli.DepFn) []*cli.Command {
|
||||
}
|
||||
}
|
||||
|
||||
func withProvider(ctx context.Context, fn func(context.Context, *goose.Provider) error) error {
|
||||
p, err := migrate.Provider()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fn(ctx, p)
|
||||
}
|
||||
|
||||
// MigrateStatusCmd returns a command to get database migration status.
|
||||
func MigrateStatusCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
func MigrateStatusCmd(sqlo migrate.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
return appcli.NewCommand(&cli.Command{
|
||||
Name: "status",
|
||||
Usage: "Get database migration status",
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
if err := goose.Status(sqlo(), migrate.MigrationsConfig().BasePath); err != nil {
|
||||
return fmt.Errorf("couldn't get migration status: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return migrate.PrintMigrationStatus(ctx)
|
||||
},
|
||||
}, deps)
|
||||
}
|
||||
|
||||
// MigrateCreateCmd returns a command to create a new migration.
|
||||
func MigrateCreateCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
// MigrateCreateCmd returns a command to create a new migration file.
|
||||
func MigrateCreateCmd(sqlo migrate.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
return appcli.NewCommand(&cli.Command{
|
||||
Name: "create",
|
||||
Usage: "Create a new migration",
|
||||
@@ -65,36 +68,38 @@ func MigrateCreateCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
},
|
||||
},
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
sequential := cmd.Bool("sequential")
|
||||
if sequential {
|
||||
goose.SetSequential(sequential)
|
||||
if cmd.Bool("sequential") {
|
||||
goose.SetSequential(true)
|
||||
}
|
||||
|
||||
if err := goose.Create(sqlo(), "migrations", cmd.StringArg("name"), cmd.StringArg("type")); err != nil {
|
||||
return fmt.Errorf("couldn't create migration: %v", err)
|
||||
cfg := migrate.MigrationsConfig()
|
||||
dir := cfg.BasePath
|
||||
if dir == "." || dir == "" {
|
||||
dir = "migrations"
|
||||
}
|
||||
if err := goose.Create(sqlo(), dir, cmd.StringArg("name"), cmd.StringArg("type")); err != nil {
|
||||
return fmt.Errorf("couldn't create migration: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}, deps)
|
||||
}
|
||||
|
||||
// MigrateUpCmd returns a command to apply all available database migrations.
|
||||
func MigrateUpCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
func MigrateUpCmd(sqlo migrate.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
return appcli.NewCommand(&cli.Command{
|
||||
Name: "up",
|
||||
Usage: "Apply all available database migrations",
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
if err := goose.Up(sqlo(), migrate.MigrationsConfig().BasePath); err != nil {
|
||||
return fmt.Errorf("couldn't apply migrations: %v", err)
|
||||
}
|
||||
return nil
|
||||
return withProvider(ctx, func(ctx context.Context, p *goose.Provider) error {
|
||||
_, err := p.Up(ctx)
|
||||
return err
|
||||
})
|
||||
},
|
||||
}, deps)
|
||||
}
|
||||
|
||||
// MigrateUpToCmd returns a command to apply all available database migrations up to a specific version.
|
||||
func MigrateUpToCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
// MigrateUpToCmd returns a command to apply migrations up to a specific version.
|
||||
func MigrateUpToCmd(sqlo migrate.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
return appcli.NewCommand(&cli.Command{
|
||||
Name: "up-to",
|
||||
Usage: "Apply all available database migrations up to a specific version",
|
||||
@@ -106,30 +111,30 @@ func MigrateUpToCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
},
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
version := cmd.Int64("version")
|
||||
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 nil
|
||||
return withProvider(ctx, func(ctx context.Context, p *goose.Provider) error {
|
||||
_, err := p.UpTo(ctx, version)
|
||||
return err
|
||||
})
|
||||
},
|
||||
}, deps)
|
||||
}
|
||||
|
||||
// MigrateDownCmd returns a command to rollback the most recent database migration.
|
||||
func MigrateDownCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
func MigrateDownCmd(sqlo migrate.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
return appcli.NewCommand(&cli.Command{
|
||||
Name: "down",
|
||||
Usage: "Rollback the most recent database migration",
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
if err := goose.Down(sqlo(), migrate.MigrationsConfig().BasePath); err != nil {
|
||||
return fmt.Errorf("couldn't rollback migration: %v", err)
|
||||
}
|
||||
return nil
|
||||
return withProvider(ctx, func(ctx context.Context, p *goose.Provider) error {
|
||||
_, err := p.Down(ctx)
|
||||
return err
|
||||
})
|
||||
},
|
||||
}, deps)
|
||||
}
|
||||
|
||||
// MigrateDownToCmd returns a command to rollback all database migrations down to a specific version.
|
||||
func MigrateDownToCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
// MigrateDownToCmd returns a command to rollback migrations down to a specific version.
|
||||
func MigrateDownToCmd(sqlo migrate.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
return appcli.NewCommand(&cli.Command{
|
||||
Name: "down-to",
|
||||
Usage: "Rollback all database migrations down to a specific version",
|
||||
@@ -141,24 +146,27 @@ func MigrateDownToCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
},
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
version := cmd.Int64("version")
|
||||
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 nil
|
||||
return withProvider(ctx, func(ctx context.Context, p *goose.Provider) error {
|
||||
_, err := p.DownTo(ctx, version)
|
||||
return err
|
||||
})
|
||||
},
|
||||
}, deps)
|
||||
}
|
||||
|
||||
// MigrateRedoCmd returns a command to rollback the most recent database migration and reapply it.
|
||||
func MigrateRedoCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
// MigrateRedoCmd returns a command to rollback and reapply the most recent migration.
|
||||
func MigrateRedoCmd(sqlo migrate.SQLOFunc, deps appcli.DepFn) *cli.Command {
|
||||
return appcli.NewCommand(&cli.Command{
|
||||
Name: "redo",
|
||||
Usage: "Rollback the most recent database migration and reapply it",
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
if err := goose.Redo(sqlo(), migrate.MigrationsConfig().BasePath); err != nil {
|
||||
return fmt.Errorf("couldn't redo migration: %v", err)
|
||||
}
|
||||
return nil
|
||||
return withProvider(ctx, func(ctx context.Context, p *goose.Provider) error {
|
||||
if _, err := p.Down(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := p.UpByOne(ctx)
|
||||
return err
|
||||
})
|
||||
},
|
||||
}, deps)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user