-
Notifications
You must be signed in to change notification settings - Fork 523
/
redo.go
51 lines (46 loc) · 1.12 KB
/
redo.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
package goose
import (
"context"
"database/sql"
)
// Redo rolls back the most recently applied migration, then runs it again.
func Redo(db *sql.DB, dir string, opts ...OptionsFunc) error {
ctx := context.Background()
return RedoContext(ctx, db, dir, opts...)
}
// RedoContext rolls back the most recently applied migration, then runs it again.
func RedoContext(ctx context.Context, db *sql.DB, dir string, opts ...OptionsFunc) error {
option := &options{}
for _, f := range opts {
f(option)
}
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return err
}
var (
currentVersion int64
)
if option.noVersioning {
if len(migrations) == 0 {
return nil
}
currentVersion = migrations[len(migrations)-1].Version
} else {
if currentVersion, err = GetDBVersionContext(ctx, db); err != nil {
return err
}
}
current, err := migrations.Current(currentVersion)
if err != nil {
return err
}
current.noVersioning = option.noVersioning
if err := current.DownContext(ctx, db); err != nil {
return err
}
if err := current.UpContext(ctx, db); err != nil {
return err
}
return nil
}