forked from pressly/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redo.go
44 lines (40 loc) · 855 Bytes
/
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
package goose
import (
"database/sql"
)
// Redo rolls back the most recently applied migration, then runs it again.
func Redo(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 = GetDBVersion(db); err != nil {
return err
}
}
current, err := migrations.Current(currentVersion)
if err != nil {
return err
}
current.noVersioning = option.noVersioning
if err := current.Down(db); err != nil {
return err
}
if err := current.Up(db); err != nil {
return err
}
return nil
}