testFlow/cli/db.go
Wyle.Gong-巩文昕 67b0ad2723 init
2025-04-22 16:42:48 +08:00

30 lines
764 B
Go

//
// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-02-27 19:32:09
// Distributed under terms of the MIT license.
//
package main
import "app/cfg"
var cmdDB = CMD.SubCommand("db", "database operations")
var cmdMigrate = cmdDB.SubCommand("migrate", "migrate database")
func init() {
cmdMigrate.Command = func() error {
// create table without constraints
cfg.DB().DisableForeignKeyConstraintWhenMigrating = true
err := cfg.DB().AutoMigrate(cfg.ObjList...)
if err != nil {
return err
}
// create constraints
cfg.DB().DisableForeignKeyConstraintWhenMigrating = false
return cfg.DB().AutoMigrate(cfg.ObjList...)
}
cmdDB.SubCommand("drop", "drop database").Command = func() error {
return cfg.DB().Migrator().DropTable(cfg.ObjList...)
}
}