Files
mp-qvyun/backend_v1/cmd/migrate.go
2024-11-28 23:18:11 +08:00

41 lines
864 B
Go

package cmd
import (
"context"
"git.ipao.vip/rogeecn/mp-qvyun/conf"
"git.ipao.vip/rogeecn/mp-qvyun/database"
"github.com/gofiber/fiber/v3/log"
"github.com/pkg/errors"
"github.com/pressly/goose/v3"
"github.com/spf13/cobra"
)
func CommandMigrate(root *cobra.Command) {
cmd := &cobra.Command{
Use: "migrate",
Short: "migrate",
RunE: commandMigrate,
}
root.AddCommand(cmd)
}
func commandMigrate(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
args = append(args, "up")
}
db, err := database.GetDB(conf.C.Database)
if err != nil {
return errors.Wrap(err, "get db")
}
action, args := args[0], args[1:]
log.Infof("migration action: %s args: %+v", action, args)
goose.SetBaseFS(database.MigrationFS)
goose.SetTableName("migrations")
return goose.RunContext(context.Background(), action, db, "migrations", args...)
}