forked from mattes/migrate
/
migrate.go
317 lines (285 loc) · 8.05 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Package migrate is imported by other Go code.
// It is the entry point to all migration functions.
package migrate
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"path"
"strconv"
"strings"
"github.com/mattes/migrate/driver"
"github.com/mattes/migrate/file"
"github.com/mattes/migrate/migrate/direction"
pipep "github.com/mattes/migrate/pipe"
)
// Up applies all available migrations
func Up(pipe chan interface{}, url, migrationsPath string) {
d, files, version, err := initDriverAndReadMigrationFilesAndGetVersion(url, migrationsPath)
if err != nil {
go pipep.Close(pipe, err)
return
}
applyMigrationFiles, err := files.ToLastFrom(version)
if err != nil {
if err2 := d.Close(); err2 != nil {
pipe <- err2
}
go pipep.Close(pipe, err)
return
}
if len(applyMigrationFiles) > 0 {
for _, f := range applyMigrationFiles {
pipe1 := pipep.New()
go d.Migrate(f, pipe1)
if ok := pipep.WaitAndRedirect(pipe1, pipe, handleInterrupts()); !ok {
break
}
}
if err := d.Close(); err != nil {
pipe <- err
}
go pipep.Close(pipe, nil)
return
} else {
if err := d.Close(); err != nil {
pipe <- err
}
go pipep.Close(pipe, nil)
return
}
}
// UpSync is synchronous version of Up
func UpSync(url, migrationsPath string) (err []error, ok bool) {
pipe := pipep.New()
go Up(pipe, url, migrationsPath)
err = pipep.ReadErrors(pipe)
return err, len(err) == 0
}
// Down rolls back all migrations
func Down(pipe chan interface{}, url, migrationsPath string) {
d, files, version, err := initDriverAndReadMigrationFilesAndGetVersion(url, migrationsPath)
if err != nil {
go pipep.Close(pipe, err)
return
}
applyMigrationFiles, err := files.ToFirstFrom(version)
if err != nil {
if err2 := d.Close(); err2 != nil {
pipe <- err2
}
go pipep.Close(pipe, err)
return
}
if len(applyMigrationFiles) > 0 {
for _, f := range applyMigrationFiles {
pipe1 := pipep.New()
go d.Migrate(f, pipe1)
if ok := pipep.WaitAndRedirect(pipe1, pipe, handleInterrupts()); !ok {
break
}
}
if err2 := d.Close(); err2 != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
} else {
if err2 := d.Close(); err2 != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
}
}
// DownSync is synchronous version of Down
func DownSync(url, migrationsPath string) (err []error, ok bool) {
pipe := pipep.New()
go Down(pipe, url, migrationsPath)
err = pipep.ReadErrors(pipe)
return err, len(err) == 0
}
// Redo rolls back the most recently applied migration, then runs it again.
func Redo(pipe chan interface{}, url, migrationsPath string) {
pipe1 := pipep.New()
go Migrate(pipe1, url, migrationsPath, -1)
if ok := pipep.WaitAndRedirect(pipe1, pipe, handleInterrupts()); !ok {
go pipep.Close(pipe, nil)
return
} else {
go Migrate(pipe, url, migrationsPath, +1)
}
}
// RedoSync is synchronous version of Redo
func RedoSync(url, migrationsPath string) (err []error, ok bool) {
pipe := pipep.New()
go Redo(pipe, url, migrationsPath)
err = pipep.ReadErrors(pipe)
return err, len(err) == 0
}
// Reset runs the down and up migration function
func Reset(pipe chan interface{}, url, migrationsPath string) {
pipe1 := pipep.New()
go Down(pipe1, url, migrationsPath)
if ok := pipep.WaitAndRedirect(pipe1, pipe, handleInterrupts()); !ok {
go pipep.Close(pipe, nil)
return
} else {
go Up(pipe, url, migrationsPath)
}
}
// ResetSync is synchronous version of Reset
func ResetSync(url, migrationsPath string) (err []error, ok bool) {
pipe := pipep.New()
go Reset(pipe, url, migrationsPath)
err = pipep.ReadErrors(pipe)
return err, len(err) == 0
}
// Migrate applies relative +n/-n migrations
func Migrate(pipe chan interface{}, url, migrationsPath string, relativeN int) {
d, files, version, err := initDriverAndReadMigrationFilesAndGetVersion(url, migrationsPath)
if err != nil {
go pipep.Close(pipe, err)
return
}
applyMigrationFiles, err := files.From(version, relativeN)
if err != nil {
if err2 := d.Close(); err2 != nil {
pipe <- err2
}
go pipep.Close(pipe, err)
return
}
if len(applyMigrationFiles) > 0 && relativeN != 0 {
for _, f := range applyMigrationFiles {
pipe1 := pipep.New()
go d.Migrate(f, pipe1)
if ok := pipep.WaitAndRedirect(pipe1, pipe, handleInterrupts()); !ok {
break
}
}
if err2 := d.Close(); err2 != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
}
if err2 := d.Close(); err2 != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
}
// MigrateSync is synchronous version of Migrate
func MigrateSync(url, migrationsPath string, relativeN int) (err []error, ok bool) {
pipe := pipep.New()
go Migrate(pipe, url, migrationsPath, relativeN)
err = pipep.ReadErrors(pipe)
return err, len(err) == 0
}
// Version returns the current migration version
func Version(url, migrationsPath string) (version uint64, err error) {
d, err := driver.New(url)
if err != nil {
return 0, err
}
return d.Version()
}
// Create creates new migration files on disk
func Create(url, migrationsPath, name string) (*file.MigrationFile, error) {
d, err := driver.New(url)
if err != nil {
return nil, err
}
files, err := file.ReadMigrationFiles(migrationsPath, file.FilenameRegex(d.FilenameExtension()))
if err != nil {
return nil, err
}
version := uint64(0)
if len(files) > 0 {
lastFile := files[len(files)-1]
version = lastFile.Version
}
version += 1
versionStr := strconv.FormatUint(version, 10)
length := 4 // TODO(mattes) check existing files and try to guess length
if len(versionStr)%length != 0 {
versionStr = strings.Repeat("0", length-len(versionStr)%length) + versionStr
}
filenamef := "%s_%s.%s.%s"
name = strings.Replace(name, " ", "_", -1)
mfile := &file.MigrationFile{
Version: version,
UpFile: &file.File{
Path: migrationsPath,
FileName: fmt.Sprintf(filenamef, versionStr, name, "up", d.FilenameExtension()),
Name: name,
Content: []byte(""),
Direction: direction.Up,
},
DownFile: &file.File{
Path: migrationsPath,
FileName: fmt.Sprintf(filenamef, versionStr, name, "down", d.FilenameExtension()),
Name: name,
Content: []byte(""),
Direction: direction.Down,
},
}
if err := ioutil.WriteFile(path.Join(mfile.UpFile.Path, mfile.UpFile.FileName), mfile.UpFile.Content, 0644); err != nil {
return nil, err
}
if err := ioutil.WriteFile(path.Join(mfile.DownFile.Path, mfile.DownFile.FileName), mfile.DownFile.Content, 0644); err != nil {
return nil, err
}
return mfile, nil
}
// initDriverAndReadMigrationFilesAndGetVersion is a small helper
// function that is common to most of the migration funcs
func initDriverAndReadMigrationFilesAndGetVersion(url, migrationsPath string) (driver.Driver, *file.MigrationFiles, uint64, error) {
d, err := driver.New(url)
if err != nil {
return nil, nil, 0, err
}
files, err := file.ReadMigrationFiles(migrationsPath, file.FilenameRegex(d.FilenameExtension()))
if err != nil {
d.Close() // TODO what happens with errors from this func?
return nil, nil, 0, err
}
version, err := d.Version()
if err != nil {
d.Close() // TODO what happens with errors from this func?
return nil, nil, 0, err
}
return d, &files, version, nil
}
// NewPipe is a convenience function for pipe.New().
// This is helpful if the user just wants to import this package and nothing else.
func NewPipe() chan interface{} {
return pipep.New()
}
// interrupts is an internal variable that holds the state of
// interrupt handling
var interrupts = true
// Graceful enables interrupts checking. Once the first ^C is received
// it will finish the currently running migration and abort execution
// of the next migration. If ^C is received twice, it will stop
// execution immediately.
func Graceful() {
interrupts = true
}
// NonGraceful disables interrupts checking. The first received ^C will
// stop execution immediately.
func NonGraceful() {
interrupts = false
}
// interrupts returns a signal channel if interrupts checking is
// enabled. nil otherwise.
func handleInterrupts() chan os.Signal {
if interrupts {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
return c
}
return nil
}