-
Notifications
You must be signed in to change notification settings - Fork 8
/
migrate.go
378 lines (340 loc) · 8.86 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package migrate
import (
"context"
"errors"
"fmt"
"io/ioutil"
"path"
"strconv"
"strings"
"time"
"github.com/db-journey/migrate/v2/direction"
"github.com/db-journey/migrate/v2/driver"
"github.com/db-journey/migrate/v2/file"
)
// Option for New
type Option func(h *Handle) error
// WithHooks allows to add pre/post migration hooks.
func WithHooks(pre, post func(f file.File) error) Option {
return func(h *Handle) error {
h.preHook = pre
h.postHook = post
return nil
}
}
// Handle encapsulates migrations functionality
type Handle struct {
drv driver.Driver
migrationsPath string
locked bool
fatalErr error
preHook, postHook func(f file.File) error
}
// Open migrations Handle
func Open(url, migrationsPath string, opts ...Option) (*Handle, error) {
d, err := driver.New(url)
if err != nil {
return nil, err
}
return New(d, migrationsPath, opts...)
}
// New migrations Handle
func New(drv driver.Driver, migrationsPath string, opts ...Option) (*Handle, error) {
if drv == nil {
return nil, errors.New("driver can't be nil")
}
h := &Handle{
drv: drv,
migrationsPath: migrationsPath,
}
for _, configure := range opts {
err := configure(h)
if err != nil {
return nil, err
}
}
return h, nil
}
// Up applies all available migrations.
func (m *Handle) Up(ctx context.Context) error {
return m.locking(ctx, func() error {
files, versions, err := m.readFilesAndGetVersions()
if err != nil {
return err
}
applyMigrationFiles, err := files.Pending(versions)
if err != nil {
return err
}
for _, f := range applyMigrationFiles {
err = m.drvMigrate(ctx, f)
if err != nil {
return err
}
}
return nil
})
}
// Down rolls back all migrations.
func (m *Handle) Down(ctx context.Context) error {
return m.locking(ctx, func() error {
files, versions, err := m.readFilesAndGetVersions()
if err != nil {
return err
}
applyMigrationFiles, err := files.Applied(versions)
if err != nil {
return err
}
for _, f := range applyMigrationFiles {
err = m.drvMigrate(ctx, f)
if err != nil {
break
}
}
return err
})
}
// Redo rolls back the most recently applied migration, then runs it again.
func (m *Handle) Redo(ctx context.Context) error {
return m.locking(ctx, func() error {
err := m.Migrate(ctx, -1)
if err != nil {
return err
}
return m.Migrate(ctx, +1)
})
}
// Reset runs the Down and Up migration function.
func (m *Handle) Reset(ctx context.Context) error {
return m.locking(ctx, func() error {
err := m.Down(ctx)
if err != nil {
return err
}
return m.Up(ctx)
})
}
// Migrate applies relative +n/-n migrations.
func (m *Handle) Migrate(ctx context.Context, relativeN int) error {
return m.locking(ctx, func() error {
files, versions, err := m.readFilesAndGetVersions()
if err != nil {
return err
}
applyMigrationFiles, err := files.Relative(relativeN, versions)
if err != nil {
return err
}
for _, f := range applyMigrationFiles {
err = m.drvMigrate(ctx, f)
if err != nil {
break
}
}
return err
})
}
// Version returns the current migration version.
func (m *Handle) Version(ctx context.Context) (version file.Version, err error) {
unlock, err := m.lock(ctx)
if err != nil {
return 0, err
}
defer unlock()
return m.drv.Version()
}
// Versions returns applied versions.
func (m *Handle) Versions(ctx context.Context) (versions file.Versions, err error) {
unlock, err := m.lock(ctx)
if err != nil {
return nil, err
}
defer unlock()
return m.drv.Versions()
}
// PendingMigrations returns list of pending migration files
func (m *Handle) PendingMigrations(ctx context.Context) (file.Files, error) {
unlock, err := m.lock(ctx)
if err != nil {
return nil, err
}
defer unlock()
files, versions, err := m.readFilesAndGetVersions()
if err != nil {
return nil, err
}
return files.Pending(versions)
}
// Create creates new migration files on disk.
func (m *Handle) Create(name string) (*file.MigrationFile, error) {
files, _, err := m.readFilesAndGetVersions()
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: m.migrationsPath,
FileName: fmt.Sprintf(filenamef, version, name, "up", driver.FileExtension(m.drv)),
Name: name,
Content: driver.FileTemplate(m.drv),
Direction: direction.Up,
},
DownFile: &file.File{
Path: m.migrationsPath,
FileName: fmt.Sprintf(filenamef, version, name, "down", driver.FileExtension(m.drv)),
Name: name,
Content: driver.FileTemplate(m.drv),
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
}
// ApplyVersion applies specific version.
func (m *Handle) ApplyVersion(ctx context.Context, version file.Version) error {
return m.migrateVersion(ctx, version, direction.Up)
}
// RollbackVersion runs `down` migration for specific version.
func (m *Handle) RollbackVersion(ctx context.Context, version file.Version) error {
return m.migrateVersion(ctx, version, direction.Down)
}
// Close database connection
func (m *Handle) Close() error {
return m.drv.Close()
}
// migrateVersion runs up or down migration (depends on given direction)
// for given version.
func (m *Handle) migrateVersion(ctx context.Context, version file.Version, d direction.Direction) error {
if d != direction.Up && d != direction.Down {
return fmt.Errorf("invalid direction: %v", d)
}
return m.locking(ctx, func() error {
files, versions, err := m.readFilesAndGetVersions()
if err != nil {
return err
}
if d == direction.Up && versions.Contains(version) {
return fmt.Errorf("version %d is already applied", version)
}
if d == direction.Down && !versions.Contains(version) {
return fmt.Errorf("version %d is not applied", version)
}
var migration *file.File
for _, f := range files {
if f.Version == version {
if migration = getFileForDirection(f, d); migration != nil {
return m.drvMigrate(ctx, *migration)
}
break
}
}
// XXX: for some reason govet complains about Direction, so .String() is explicit
return fmt.Errorf("no `%s` migration file for version %d", d.String(), version)
})
}
func (m *Handle) lock(ctx context.Context) (unlock func(), err error) {
if m.fatalErr != nil {
return nil, m.fatalErr
}
if m.locked {
return func() {}, nil
}
select {
case err := <-drvLockChan(m.drv):
if err != nil {
return nil, err
}
case <-ctx.Done():
return nil, ctx.Err()
}
m.locked = true
return m.unlock, nil
}
func (m *Handle) unlock() {
err := driver.Unlock(m.drv)
if err == nil {
m.locked = false
return
}
m.Close()
m.fatalErr = fmt.Errorf("connection closed, this handle is no longer usable - failed to unlock database after last session: %s", err)
}
func (m *Handle) locking(ctx context.Context, f func() error) error {
unlock, err := m.lock(ctx)
if err != nil {
return err
}
defer unlock()
return f()
}
func (m *Handle) drvMigrate(ctx context.Context, f file.File) error {
select {
case <-ctx.Done():
return fmt.Errorf("interrupted before applying version %d: %s", f.Version, ctx.Err())
default:
err := runHookIfNotNil(m.preHook, "pre", f)
if err != nil {
return err
}
err = m.drv.Migrate(f)
if err != nil {
return err
}
return runHookIfNotNil(m.postHook, "post", f)
}
}
// readFilesAndGetVersions is a small helper
// function that is common to most of the migration funcs.
func (m *Handle) readFilesAndGetVersions() (file.MigrationFiles, file.Versions, error) {
files, err := file.ReadMigrationFiles(m.migrationsPath, file.FilenameRegex(driver.FileExtension(m.drv)))
if err != nil {
return nil, file.Versions{}, err
}
versions, err := m.drv.Versions()
return files, versions, err
}
func runHookIfNotNil(hook func(f file.File) error, name string, f file.File) error {
if hook == nil {
return nil
}
err := hook(f)
if err != nil {
return fmt.Errorf("%s-hook for migration %q failed: %s", name, f.FileName, err)
}
return nil
}
func getFileForDirection(m file.MigrationFile, d direction.Direction) *file.File {
if d == direction.Up {
return m.UpFile
}
return m.DownFile
}
func drvLockChan(drv driver.Driver) <-chan error {
ret := make(chan error)
go func() {
if err := driver.Lock(drv); err != nil {
ret <- err
}
close(ret)
}()
return ret
}