forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
65 lines (55 loc) · 1.5 KB
/
migrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cmd
import (
"fmt"
"os"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/engine"
"github.com/yaoapp/yao/share"
)
var name string
var force bool = false
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: L("Update database schema"),
Long: L("Update database schema"),
Run: func(cmd *cobra.Command, args []string) {
defer func() {
err := exception.Catch(recover())
if err != nil {
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
}
}()
Boot()
if !force && config.Conf.Mode == "production" {
fmt.Println(color.WhiteString(L("TRY:")), color.GreenString("%s migrate --force", share.BUILDNAME))
exception.New(L("Migrate is not allowed on production mode."), 403).Throw()
}
// 加载数据模型
err := engine.Load(config.Conf)
if err != nil {
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
os.Exit(1)
}
if name != "" {
mod, has := gou.Models[name]
if has {
mod.Migrate(true)
}
return
}
// Do Stuff Here
for _, mod := range gou.Models {
fmt.Println(color.GreenString(L("Update schema model: %s (%s) "), mod.Name, mod.MetaData.Table.Name))
mod.Migrate(true)
}
fmt.Println(color.GreenString(L("✨DONE✨")))
},
}
func init() {
migrateCmd.PersistentFlags().StringVarP(&name, "name", "n", "", L("Model name"))
migrateCmd.PersistentFlags().BoolVarP(&force, "force", "", false, L("Force migrate"))
}