fix issues

This commit is contained in:
yanghao05
2023-04-28 18:13:55 +08:00
parent ebcd25f776
commit e0e48fe83a
10 changed files with 192 additions and 161 deletions

View File

@@ -13,7 +13,54 @@ import (
"gorm.io/gorm"
)
func WithMigration(rootCmd *cobra.Command) *cobra.Command {
func withMigrationCommand(rootCmd *cobra.Command) *cobra.Command {
// migrateUpCmd represents the migrateUp command
var migrateUpCmd = &cobra.Command{
Use: "up",
Short: "migrate up database tables",
Long: `migrate up database tables`,
RunE: func(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(mi MigrationInfo) error {
m := gormigrate.New(mi.DB, gormigrate.DefaultOptions, sortedMigrations(mi.Migrations))
if len(migrateToId) > 0 {
log.Printf("migrate up to [%s]\n", migrateToId)
return m.MigrateTo(migrateToId)
}
return m.Migrate()
})
},
PostRun: func(cmd *cobra.Command, args []string) {
log.Println("BINGO! migrate up done")
},
}
// migrateDownCmd represents the migrateDown command
var migrateDownCmd = &cobra.Command{
Use: "down",
Short: "migrate down database tables",
Long: `migrate down database tables`,
RunE: func(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(mi MigrationInfo) error {
m := gormigrate.New(mi.DB, gormigrate.DefaultOptions, sortedMigrations(mi.Migrations))
if len(migrateToId) > 0 {
log.Printf("migrate down to [%s]\n", migrateToId)
return m.RollbackTo(migrateToId)
}
return m.RollbackLast()
})
},
PostRun: func(cmd *cobra.Command, args []string) {
log.Println("BINGO! migrate down done")
},
}
// migrateCmd represents the migrate command
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "migrate database tables",
Long: `migrate database tables`,
}
rootCmd.AddCommand(migrateCmd)
migrateCmd.AddCommand(migrateUpCmd)
migrateCmd.AddCommand(migrateDownCmd)
@@ -23,13 +70,6 @@ func WithMigration(rootCmd *cobra.Command) *cobra.Command {
return rootCmd
}
// migrateCmd represents the migrate command
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "migrate database tables",
Long: `migrate database tables`,
}
var migrateToId string
// MigrationInfo http service container
@@ -40,47 +80,6 @@ type MigrationInfo struct {
Migrations []contracts.Migration `group:"migrations"`
}
// migrateUpCmd represents the migrateUp command
var migrateUpCmd = &cobra.Command{
Use: "up",
Short: "migrate up database tables",
Long: `migrate up database tables`,
RunE: func(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(mi MigrationInfo) error {
m := gormigrate.New(mi.DB, gormigrate.DefaultOptions, sortedMigrations(mi.Migrations))
if len(migrateToId) > 0 {
log.Printf("migrate up to [%s]\n", migrateToId)
return m.MigrateTo(migrateToId)
}
return m.Migrate()
})
},
PostRun: func(cmd *cobra.Command, args []string) {
log.Println("BINGO! migrate up done")
},
}
// migrateDownCmd represents the migrateDown command
var migrateDownCmd = &cobra.Command{
Use: "down",
Short: "migrate down database tables",
Long: `migrate down database tables`,
RunE: func(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(mi MigrationInfo) error {
m := gormigrate.New(mi.DB, gormigrate.DefaultOptions, sortedMigrations(mi.Migrations))
if len(migrateToId) > 0 {
log.Printf("migrate down to [%s]\n", migrateToId)
return m.RollbackTo(migrateToId)
}
return m.RollbackLast()
})
},
PostRun: func(cmd *cobra.Command, args []string) {
log.Println("BINGO! migrate down done")
},
}
func sortedMigrations(ms []contracts.Migration) []*gormigrate.Migration {
migrationKeys := []string{}
migrationMaps := make(map[string]*gormigrate.Migration)