/
migrate.go
142 lines (132 loc) · 3.16 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package airtrack
import (
"bufio"
"fmt"
"github.com/afk11/airtrack/pkg/config"
"github.com/afk11/airtrack/pkg/db/migrations"
"github.com/pkg/errors"
"os"
)
type (
// MigrateUpCmd - executes migrate up
MigrateUpCmd struct {
Config string `help:"Configuration file path"`
Force bool `help:"Proceed with task without user confirmation'"`
}
// MigrateDownCmd - executes migrate down
MigrateDownCmd struct {
Config string `help:"Configuration file path"`
Force bool `help:"Proceed with task without user confirmation'"`
}
// MigrateStepsCmd - executes n migration steps
MigrateStepsCmd struct {
Config string `help:"Configuration file path"`
Force bool `help:"Proceed with task without user confirmation'"`
N int `help:"how many migrations up (if positive), or down (if negative)"`
}
)
// Run - triggers up migrations
func (c *MigrateUpCmd) Run() error {
// Call the Run() method of the selected parsed command.
cfg, err := config.ReadConfigFromFile(c.Config)
if err != nil {
panic(err)
}
loc, err := cfg.GetTimeLocation()
if err != nil {
return err
}
m, err := migrations.InitMigrations(&cfg.Database, loc)
if err != nil {
return err
}
if !c.Force {
c, err := prompt("migration to latest")
if err != nil {
return err
} else if !c {
return errors.Errorf("task cancelled by user")
}
}
err = m.Up() // run your migrations and handle the errors above of course
if err != nil {
return err
}
return nil
}
// Run - triggers down migrations
func (c *MigrateDownCmd) Run() error {
// Call the Run() method of the selected parsed command.
cfg, err := config.ReadConfigFromFile(c.Config)
if err != nil {
panic(err)
}
loc, err := cfg.GetTimeLocation()
if err != nil {
return err
}
m, err := migrations.InitMigrations(&cfg.Database, loc)
if err != nil {
return err
}
if !c.Force {
c, err := prompt("rollback")
if err != nil {
return err
} else if !c {
return errors.Errorf("task cancelled by user")
}
}
err = m.Down() // run your migrations and handle the errors above of course
if err != nil {
return err
}
return nil
}
// Run - triggers migration n steps
func (c *MigrateStepsCmd) Run() error {
if c.N == 0 {
return errors.Errorf("cannot set n=0 (stay where we are)")
}
// Call the Run() method of the selected parsed command.
cfg, err := config.ReadConfigFromFile(c.Config)
if err != nil {
panic(err)
}
loc, err := cfg.GetTimeLocation()
if err != nil {
return err
}
m, err := migrations.InitMigrations(&cfg.Database, loc)
if err != nil {
return err
}
if !c.Force {
var action string
if c.N > 0 {
action = "migrate %d steps forward"
} else {
action = "rollback %d stages"
}
c, err := prompt(fmt.Sprintf(action, c.N))
if err != nil {
return err
} else if !c {
return errors.Errorf("task cancelled by user")
}
}
err = m.Steps(c.N) // run your migrations and handle the errors above of course
if err != nil {
return err
}
return nil
}
func prompt(action string) (bool, error) {
var reader = bufio.NewReader(os.Stdin)
fmt.Printf("Proceed with %s (y/N): \n", action)
r, _, err := reader.ReadRune()
if err != nil {
return false, err
}
return r == 'y', nil
}