package cmd import ( "context" "fmt" "kpt-pasture/config" "kpt-pasture/store/kptstore" "gitee.com/xuyiping_admin/pkg/cmd" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "gitee.com/xuyiping_admin/pkg/xerr" "gitee.com/xuyiping_admin/pkg/xstore/database/migrator" "github.com/spf13/cobra" "go.uber.org/zap" ) func init() { cmd.Instant(JobCmd, "db:migrate", taskDBMigrate, "数据库迁移-执行完立刻退出") } var JobCmd = &cobra.Command{ Use: "job", Short: "内部一次性脚本", Run: func(cmd *cobra.Command, args []string) { fmt.Println("job called") }, } // Migration record type Migration struct { Id int `xorm:"INT AUTOINCR NOTNULL PK"` Name string `xorm:"VARCHAR(1024) NOTNULL UNIQUE"` } // TableName migrations func (Migration) TableName() string { return "migrations" } func taskDBMigrate(ctx context.Context, args []string) error { cfg := config.Options() zaplog.Info("boot migrate") db := kptstore.MustMigrateStore(cfg) if err := migrator.AutoMigrateFiles(db, "../migrator"); err != nil { zaplog.Error("taskDBMigrate", zap.Any("err", err)) panic(xerr.WithStack(err)) } sqlDB, _ := db.DB() defer sqlDB.Close() zaplog.Info("migrate ok") return nil }