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
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
3
go.mod
3
go.mod
@@ -6,6 +6,7 @@ require (
|
||||
gitea.auvem.com/go-toolkit/app v0.0.0-20250530224140-b760b035b4d1
|
||||
gitea.auvem.com/go-toolkit/dbx v0.0.0-20250530232843-55cc3ffd8364
|
||||
github.com/pressly/goose/v3 v3.24.3
|
||||
github.com/spf13/cobra v1.9.1
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -15,6 +16,7 @@ require (
|
||||
github.com/go-jet/jet/v2 v2.13.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.9.2 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
@@ -23,6 +25,7 @@ require (
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/segmentio/ksuid v1.0.4 // indirect
|
||||
github.com/sethvargo/go-retry v0.3.0 // indirect
|
||||
github.com/spf13/pflag v1.0.6 // indirect
|
||||
github.com/stretchr/testify v1.10.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/sync v0.14.0 // indirect
|
||||
|
||||
14
go.sum
14
go.sum
@@ -4,6 +4,8 @@ gitea.auvem.com/go-toolkit/app v0.0.0-20250530224140-b760b035b4d1 h1:22A/8PcixfY
|
||||
gitea.auvem.com/go-toolkit/app v0.0.0-20250530224140-b760b035b4d1/go.mod h1:a7ENpOxndUdONE6oZ9MZAvG1ba2uq01x/LtcnDkpOj8=
|
||||
gitea.auvem.com/go-toolkit/dbx v0.0.0-20250530232843-55cc3ffd8364 h1:xadjfyFYYoyy8d6AtqiNSv8OtMtav53vMpg3mVRM+10=
|
||||
gitea.auvem.com/go-toolkit/dbx v0.0.0-20250530232843-55cc3ffd8364/go.mod h1:3CYeto5wVq0fcABgssDYycXGbR7ibNiN66p1HpxBdds=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
@@ -18,7 +20,12 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
@@ -29,16 +36,22 @@ github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGA
|
||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pressly/goose/v3 v3.24.3 h1:DSWWNwwggVUsYZ0X2VitiAa9sKuqtBfe+Jr9zFGwWlM=
|
||||
github.com/pressly/goose/v3 v3.24.3/go.mod h1:v9zYL4xdViLHCUUJh/mhjnm6JrK7Eul8AS93IxiZM4E=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
|
||||
github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
|
||||
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
|
||||
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
|
||||
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
||||
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
||||
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
@@ -53,6 +66,7 @@ golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
|
||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
modernc.org/libc v1.65.0 h1:e183gLDnAp9VJh6gWKdTy0CThL9Pt7MfcR/0bgb7Y1Y=
|
||||
|
||||
Reference in New Issue
Block a user