add migrate command builders
This commit is contained in:
165
cmd/migrate.go
Normal file
165
cmd/migrate.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package migratecmd
|
||||
|
||||
import (
|
||||
"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 {
|
||||
cmd := &cobra.Command{
|
||||
Use: "migrate",
|
||||
Short: "Migrate the database",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cmd.Help()
|
||||
},
|
||||
}
|
||||
|
||||
cmd.AddCommand(AllSubcommands()...)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// AllSubcommands returns all subcommands of the migrate command.
|
||||
func AllSubcommands() []*cobra.Command {
|
||||
return []*cobra.Command{
|
||||
MigrateStatusCmd(),
|
||||
MigrateCreateCmd(),
|
||||
MigrateUpCmd(),
|
||||
MigrateUpToCmd(),
|
||||
MigrateDownCmd(),
|
||||
MigrateDownToCmd(),
|
||||
MigrateRedoCmd(),
|
||||
}
|
||||
}
|
||||
|
||||
// MigrateStatusCmd returns a command to get database migration status.
|
||||
func MigrateStatusCmd() *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 {
|
||||
fmt.Printf("Error: Couldn't get migration status: %v\n", err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// MigrateCreateCmd returns a command to create a new migration.
|
||||
func MigrateCreateCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "create [NAME] [TYPE]",
|
||||
Short: "Create a new migration",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
sequential, err := cmd.Flags().GetBool("sequential")
|
||||
if err != nil {
|
||||
fmt.Printf("Error: Failed to get 'sequential' flag: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if sequential {
|
||||
goose.SetSequential(sequential)
|
||||
}
|
||||
|
||||
if err := goose.Create(dbx.SQLO(), "migrations", args[0], args[1]); err != nil {
|
||||
fmt.Printf("Error: Couldn't create migration: %v\n", err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolP("sequential", "s", false, "Create a sequential migration")
|
||||
return cmd
|
||||
}
|
||||
|
||||
// MigrateUpCmd returns a command to apply all available database migrations.
|
||||
func MigrateUpCmd() *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 {
|
||||
fmt.Printf("Error: Couldn't apply migrations: %v\n", err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// MigrateUpToCmd returns a command to apply all available database migrations up to a specific version.
|
||||
func MigrateUpToCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "up-to [VERSION]",
|
||||
Short: "Apply all available database migrations up to a specific version",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
version, err := strconv.ParseInt(args[0], 10, 0)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: Couldn't parse version: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := goose.UpTo(dbx.SQLO(), migrate.MigrationsConfig().BasePath, version); err != nil {
|
||||
fmt.Printf("Error: Couldn't apply migrations to target version %d: %v\n", version, err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// MigrateDownCmd returns a command to rollback the most recent database migration.
|
||||
func MigrateDownCmd() *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 {
|
||||
fmt.Printf("Error: Couldn't rollback migration: %v\n", err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// MigrateDownToCmd returns a command to rollback all database migrations down to a specific version.
|
||||
func MigrateDownToCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "down-to [VERSION]",
|
||||
Short: "Rollback all database migrations down to a specific version",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
version, err := strconv.ParseInt(args[0], 10, 0)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: Couldn't parse version: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := goose.DownTo(dbx.SQLO(), migrate.MigrationsConfig().BasePath, version); err != nil {
|
||||
fmt.Printf("Error: Couldn't rollback migrations to target version %d: %v\n", version, err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// MigrateRedoCmd returns a command to rollback the most recent database migration and reapply it.
|
||||
func MigrateRedoCmd() *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 {
|
||||
fmt.Printf("Error: Couldn't redo migration: %v\n", err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user