support logger auto call
This commit is contained in:
106
cmd/migrate.go
Normal file
106
cmd/migrate.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
// init dependencies
|
||||
_ "atom/database/migrations"
|
||||
_ "atom/providers"
|
||||
"atom/providers/logger"
|
||||
|
||||
"atom/container"
|
||||
"atom/contracts"
|
||||
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// migrateCmd represents the migrate command
|
||||
var migrateCmd = &cobra.Command{
|
||||
Use: "migrate",
|
||||
Short: "migrate database tables",
|
||||
Long: `migrate database tables`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(migrateCmd)
|
||||
migrateCmd.AddCommand(migrateUpCmd)
|
||||
migrateCmd.AddCommand(migrateDownCmd)
|
||||
|
||||
migrateCmd.PersistentFlags().StringVar(&migrateToId, "to", "", "migration to id")
|
||||
}
|
||||
|
||||
var migrateToId string
|
||||
|
||||
// MigrationInfo http service container
|
||||
type MigrationInfo struct {
|
||||
dig.In
|
||||
|
||||
DB *gorm.DB
|
||||
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 {
|
||||
logger.Infof("migrate up to [%s]", migrateToId)
|
||||
return m.MigrateTo(migrateToId)
|
||||
}
|
||||
return m.Migrate()
|
||||
})
|
||||
},
|
||||
PostRun: func(cmd *cobra.Command, args []string) {
|
||||
logger.Info("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 {
|
||||
logger.Infof("migrate down to [%s]", migrateToId)
|
||||
return m.RollbackTo(migrateToId)
|
||||
}
|
||||
return m.RollbackLast()
|
||||
})
|
||||
},
|
||||
PostRun: func(cmd *cobra.Command, args []string) {
|
||||
logger.Info("BINGO! migrate down done")
|
||||
},
|
||||
}
|
||||
|
||||
func sortedMigrations(ms []contracts.Migration) []*gormigrate.Migration {
|
||||
migrationKeys := []string{}
|
||||
migrationMaps := make(map[string]*gormigrate.Migration)
|
||||
for _, m := range ms {
|
||||
migrationKeys = append(migrationKeys, m.ID())
|
||||
migrationMaps[m.ID()] = &gormigrate.Migration{
|
||||
ID: m.ID(),
|
||||
Migrate: m.Up,
|
||||
Rollback: m.Down,
|
||||
}
|
||||
}
|
||||
sort.Strings(migrationKeys)
|
||||
|
||||
migrations := []*gormigrate.Migration{}
|
||||
for _, key := range migrationKeys {
|
||||
migrations = append(migrations, migrationMaps[key])
|
||||
}
|
||||
|
||||
return migrations
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package cmd
|
||||
|
||||
import (
|
||||
_ "atom/providers"
|
||||
"log"
|
||||
|
||||
"atom/container"
|
||||
"atom/services/http"
|
||||
@@ -18,7 +19,7 @@ var rootCmd = &cobra.Command{
|
||||
Long: `the app long description`,
|
||||
Version: fmt.Sprintf("\nVersion: %s\nGitHash: %s\nBuildAt: %s\n", utils.Version, utils.GitHash, utils.BuildAt),
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
// fmt.Println("using config file: ", utils.ShareConfigFile)
|
||||
log.Println("using config file: ", utils.ShareConfigFile)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return container.Container.Invoke(http.Serve)
|
||||
|
||||
Reference in New Issue
Block a user