Files
migrate/cli/cli.go
2025-06-05 14:59:05 -07:00

164 lines
5.3 KiB
Go

package migratecli
import (
"context"
"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 {
return appcli.NewCommand(&cli.Command{
Name: "migrate",
Usage: "Migrate the database",
Commands: AllSubcommands(sqlo, childDeps),
}, directDeps)
}
// AllSubcommands returns all subcommands of the migrate command.
func AllSubcommands(sqlo dbx.SQLOFunc, deps appcli.DepFn) []*cli.Command {
return []*cli.Command{
MigrateStatusCmd(sqlo, deps),
MigrateCreateCmd(sqlo, deps),
MigrateUpCmd(sqlo, deps),
MigrateUpToCmd(sqlo, deps),
MigrateDownCmd(sqlo, deps),
MigrateDownToCmd(sqlo, deps),
MigrateRedoCmd(sqlo, deps),
}
}
// MigrateStatusCmd returns a command to get database migration status.
func MigrateStatusCmd(sqlo dbx.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
},
}, deps)
}
// MigrateCreateCmd returns a command to create a new migration.
func MigrateCreateCmd(sqlo dbx.SQLOFunc, deps appcli.DepFn) *cli.Command {
return appcli.NewCommand(&cli.Command{
Name: "create",
Usage: "Create a new migration",
Arguments: []cli.Argument{
&cli.StringArg{Name: "name", UsageText: "Name of the migration"},
&cli.StringArg{Name: "type", UsageText: "Type of the migration (e.g., sql, go)"},
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "sequential",
Usage: "Create migration in sequential mode",
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
sequential := cmd.Bool("sequential")
if sequential {
goose.SetSequential(sequential)
}
if err := goose.Create(sqlo(), "migrations", cmd.StringArg("name"), cmd.StringArg("type")); err != nil {
return fmt.Errorf("couldn't create migration: %v", err)
}
return nil
},
}, deps)
}
// MigrateUpCmd returns a command to apply all available database migrations.
func MigrateUpCmd(sqlo dbx.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
},
}, 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 {
return appcli.NewCommand(&cli.Command{
Name: "up-to",
Usage: "Apply all available database migrations up to a specific version",
Arguments: []cli.Argument{
&cli.Int64Arg{
Name: "version",
UsageText: "Target version to migrate up to",
},
},
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
},
}, deps)
}
// MigrateDownCmd returns a command to rollback the most recent database migration.
func MigrateDownCmd(sqlo dbx.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
},
}, 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 {
return appcli.NewCommand(&cli.Command{
Name: "down-to",
Usage: "Rollback all database migrations down to a specific version",
Arguments: []cli.Argument{
&cli.Int64Arg{
Name: "version",
UsageText: "Target version to rollback to",
},
},
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
},
}, 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 {
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
},
}, deps)
}