-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_rollback_command.go
75 lines (60 loc) · 1.61 KB
/
migrate_rollback_command.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
66
67
68
69
70
71
72
73
74
75
package console
import (
"strconv"
_ "github.com/go-sql-driver/mysql"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/gookit/color"
"github.com/yafgo/framework/contracts/console"
"github.com/yafgo/framework/contracts/console/command"
)
type MigrateRollbackCommand struct {
}
// Signature The name and signature of the console command.
func (receiver *MigrateRollbackCommand) Signature() string {
return "migrate:rollback"
}
// Description The console command description.
func (receiver *MigrateRollbackCommand) Description() string {
return "Rollback the database migrations"
}
// Extend The console command extend.
func (receiver *MigrateRollbackCommand) Extend() command.Extend {
return command.Extend{
Category: "migrate",
Flags: []command.Flag{
{
Name: "step",
Value: "1",
Usage: "rollback steps",
},
},
}
}
// Handle Execute the console command.
func (receiver *MigrateRollbackCommand) Handle(ctx console.Context) error {
m, err := getMigrate()
if err != nil {
return err
}
if m == nil {
color.Yellowln("Please fill database config first")
return nil
}
stepString := "-" + ctx.Option("step")
step, err := strconv.Atoi(stepString)
if err != nil {
color.Redln("Migration failed: invalid step", ctx.Option("step"))
return nil
}
if err := m.Steps(step); err != nil && err != migrate.ErrNoChange && err != migrate.ErrNilVersion {
switch err.(type) {
case migrate.ErrShortLimit:
default:
color.Redln("Migration failed:", err.Error())
return nil
}
}
color.Greenln("Migration rollback success")
return nil
}