forked from mattes/migrate
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
242 lines (211 loc) · 5.12 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Package main is the CLI.
// You can use the CLI via Terminal.
// import "github.com/codeship/migrate/migrate" for usage within Go.
package main
import (
"flag"
"fmt"
"os"
"strconv"
"time"
"github.com/fatih/color"
_ "github.com/codeship/migrate/driver/bash"
_ "github.com/codeship/migrate/driver/cassandra"
_ "github.com/codeship/migrate/driver/mysql"
_ "github.com/codeship/migrate/driver/postgres"
_ "github.com/codeship/migrate/driver/sqlite3"
"github.com/codeship/migrate/file"
"github.com/codeship/migrate/migrate"
"github.com/codeship/migrate/migrate/direction"
pipep "github.com/codeship/migrate/pipe"
)
var url = flag.String("url", os.Getenv("MIGRATE_URL"), "")
var migrationsPath = flag.String("path", "", "")
var version = flag.Bool("version", false, "Show migrate version")
func main() {
flag.Usage = func() {
helpCmd()
}
flag.Parse()
command := flag.Arg(0)
if *version {
fmt.Println(Version)
os.Exit(0)
}
if *migrationsPath == "" {
*migrationsPath, _ = os.Getwd()
}
switch command {
case "create":
verifyMigrationsPath(*migrationsPath)
name := flag.Arg(1)
if name == "" {
fmt.Println("Please specify name.")
os.Exit(1)
}
migrationFile, err := migrate.Create(*url, *migrationsPath, name)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Version %v migration files created in %v:\n", migrationFile.Version, *migrationsPath)
fmt.Println(migrationFile.UpFile.FileName)
fmt.Println(migrationFile.DownFile.FileName)
case "migrate":
verifyMigrationsPath(*migrationsPath)
relativeN := flag.Arg(1)
relativeNInt, err := strconv.Atoi(relativeN)
if err != nil {
fmt.Println("Unable to parse param <n>.")
os.Exit(1)
}
timerStart = time.Now()
pipe := pipep.New()
go migrate.Migrate(pipe, *url, *migrationsPath, relativeNInt)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(1)
}
case "goto":
verifyMigrationsPath(*migrationsPath)
toVersion := flag.Arg(1)
toVersionInt, err := strconv.Atoi(toVersion)
if err != nil || toVersionInt < 0 {
fmt.Println("Unable to parse param <v>.")
os.Exit(1)
}
currentVersion, err := migrate.Version(*url, *migrationsPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
relativeNInt := toVersionInt - int(currentVersion)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Migrate(pipe, *url, *migrationsPath, relativeNInt)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(1)
}
case "up":
verifyMigrationsPath(*migrationsPath)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Up(pipe, *url, *migrationsPath)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(1)
}
case "down":
verifyMigrationsPath(*migrationsPath)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Down(pipe, *url, *migrationsPath)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(1)
}
case "redo":
verifyMigrationsPath(*migrationsPath)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Redo(pipe, *url, *migrationsPath)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(1)
}
case "reset":
verifyMigrationsPath(*migrationsPath)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Reset(pipe, *url, *migrationsPath)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(1)
}
case "version":
verifyMigrationsPath(*migrationsPath)
version, err := migrate.Version(*url, *migrationsPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(version)
default:
fallthrough
case "help":
helpCmd()
}
}
func writePipe(pipe chan interface{}) (ok bool) {
okFlag := true
if pipe != nil {
for {
select {
case item, more := <-pipe:
if !more {
return okFlag
} else {
switch item.(type) {
case string:
fmt.Println(item.(string))
case error:
c := color.New(color.FgRed)
c.Println(item.(error).Error(), "\n")
okFlag = false
case file.File:
f := item.(file.File)
c := color.New(color.FgBlue)
if f.Direction == direction.Up {
c.Print(">")
} else if f.Direction == direction.Down {
c.Print("<")
}
fmt.Printf(" %s\n", f.FileName)
default:
text := fmt.Sprint(item)
fmt.Println(text)
}
}
}
}
}
return okFlag
}
func verifyMigrationsPath(path string) {
if path == "" {
fmt.Println("Please specify path")
os.Exit(1)
}
}
var timerStart time.Time
func printTimer() {
diff := time.Now().Sub(timerStart).Seconds()
if diff > 60 {
fmt.Printf("\n%.4f minutes\n", diff/60)
} else {
fmt.Printf("\n%.4f seconds\n", diff)
}
}
func helpCmd() {
os.Stderr.WriteString(
`usage: migrate [-path=<path>] -url=<url> <command> [<args>]
Commands:
create <name> Create a new migration
up Apply all -up- migrations
down Apply all -down- migrations
reset Down followed by Up
redo Roll back most recent migration, then apply it again
version Show current migration version
migrate <n> Apply migrations -n|+n
goto <v> Migrate to version v
help Show this help
'-path' defaults to current working directory.
`)
}