forked from db-journey/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
309 lines (271 loc) · 7.72 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
package migrate
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"path"
"strconv"
"strings"
"time"
"github.com/db-journey/migrate/direction"
"github.com/db-journey/migrate/driver"
"github.com/db-journey/migrate/file"
pipep "github.com/db-journey/migrate/pipe"
)
// Up applies all available migrations.
func Up(pipe chan interface{}, url, migrationsPath string) {
d, files, versions, err := initDriverAndReadMigrationFilesAndGetVersions(url, migrationsPath)
defer func() {
if err != nil {
pipe <- err
}
if d != nil {
if err = d.Close(); err != nil {
pipe <- err
}
}
go pipep.Close(pipe, nil)
}()
if err != nil {
return
}
applyMigrationFiles, err := files.Pending(versions)
if err != nil {
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
}
}
}
}
// 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, versions, err := initDriverAndReadMigrationFilesAndGetVersions(url, migrationsPath)
defer func() {
if err != nil {
pipe <- err
}
if d != nil {
if err = d.Close(); err != nil {
pipe <- err
}
}
go pipep.Close(pipe, nil)
}()
if err != nil {
return
}
applyMigrationFiles, err := files.Applied(versions)
if err != nil {
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
}
}
}
}
// 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
}
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
}
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, versions, err := initDriverAndReadMigrationFilesAndGetVersions(url, migrationsPath)
defer func() {
if err != nil {
pipe <- err
}
if d != nil {
if err = d.Close(); err != nil {
pipe <- err
}
}
go pipep.Close(pipe, nil)
}()
if err != nil {
return
}
applyMigrationFiles, err := files.Relative(relativeN, versions)
if err != nil {
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
}
}
}
}
// 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 file.Version, err error) {
d, err := driver.New(url)
if err != nil {
return 0, err
}
return d.Version()
}
// Versions returns applied versions.
func Versions(url, migrationsPath string) (versions file.Versions, err error) {
d, err := driver.New(url)
if err != nil {
return file.Versions{}, err
}
return d.Versions()
}
// Create creates new migration files on disk.
func Create(url, migrationsPath, name string) (*file.MigrationFile, error) {
d, files, _, err := initDriverAndReadMigrationFilesAndGetVersions(url, migrationsPath)
if err != nil {
return nil, err
}
versionStr := time.Now().UTC().Format("20060102150405")
v, _ := strconv.ParseUint(versionStr, 10, 64)
version := file.Version(v)
filenamef := "%d_%s.%s.%s"
name = strings.Replace(name, " ", "_", -1)
// if latest version has the same timestamp, increment version
if len(files) > 0 {
latest := files[len(files)-1].Version
if latest >= version {
version = latest + 1
}
}
mfile := &file.MigrationFile{
Version: version,
UpFile: &file.File{
Path: migrationsPath,
FileName: fmt.Sprintf(filenamef, version, name, "up", d.FilenameExtension()),
Name: name,
Content: []byte(""),
Direction: direction.Up,
},
DownFile: &file.File{
Path: migrationsPath,
FileName: fmt.Sprintf(filenamef, version, 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
}
// initDriverAndReadMigrationFilesAndGetVersionsAndGetVersion is a small helper
// function that is common to most of the migration funcs.
func initDriverAndReadMigrationFilesAndGetVersions(url, migrationsPath string) (driver.Driver, file.MigrationFiles, file.Versions, error) {
d, err := driver.New(url)
if err != nil {
return nil, nil, file.Versions{}, 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, file.Versions{}, err
}
versions, err := d.Versions()
if err != nil {
d.Close() // TODO what happens with errors from this func?
return nil, nil, file.Versions{}, err
}
return d, files, versions, 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
}