-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmigrate.go
52 lines (48 loc) · 966 Bytes
/
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
package main
import (
"fmt"
"github.com/EduOJ/backend/base/log"
"github.com/EduOJ/backend/database"
)
func doMigrate() {
if len(args) == 1 {
readConfig()
initGorm()
log.Fatal("Migrate succeed!")
} else {
switch args[1] {
case "help":
fmt.Println(`Usage:
./backend migrate <command>
command: The command to run. default: migrate
Commands:
migrate: run migrations.
rollback: rollback all migrations.
rollback_last: rollback last migration.`)
case "migrate":
readConfig()
initGorm()
log.Fatal("Migrate succeed!")
case "rollback":
readConfig()
initGorm(false)
m := database.GetMigration()
err := m.RollbackTo("start")
if err != nil {
log.Error(err)
} else {
log.Fatal("Migrate succeed!")
}
case "rollback_last":
readConfig()
initGorm(false)
m := database.GetMigration()
err := m.RollbackLast()
if err != nil {
log.Error(err)
} else {
log.Fatal("Migrate succeed!")
}
}
}
}