-
Notifications
You must be signed in to change notification settings - Fork 522
/
down.go
107 lines (97 loc) · 2.87 KB
/
down.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
package goose
import (
"context"
"database/sql"
"fmt"
)
// Down rolls back a single migration from the current version.
func Down(db *sql.DB, dir string, opts ...OptionsFunc) error {
ctx := context.Background()
return DownContext(ctx, db, dir, opts...)
}
// DownContext rolls back a single migration from the current version.
func DownContext(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
}
if option.noVersioning {
if len(migrations) == 0 {
return nil
}
currentVersion := migrations[len(migrations)-1].Version
// Migrate only the latest migration down.
return downToNoVersioning(ctx, db, migrations, currentVersion-1)
}
currentVersion, err := GetDBVersionContext(ctx, db)
if err != nil {
return err
}
current, err := migrations.Current(currentVersion)
if err != nil {
return fmt.Errorf("migration %v: %w", currentVersion, err)
}
return current.DownContext(ctx, db)
}
// DownTo rolls back migrations to a specific version.
func DownTo(db *sql.DB, dir string, version int64, opts ...OptionsFunc) error {
ctx := context.Background()
return DownToContext(ctx, db, dir, version, opts...)
}
// DownToContext rolls back migrations to a specific version.
func DownToContext(ctx context.Context, db *sql.DB, dir string, version int64, opts ...OptionsFunc) error {
option := &options{}
for _, f := range opts {
f(option)
}
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return err
}
if option.noVersioning {
return downToNoVersioning(ctx, db, migrations, version)
}
for {
currentVersion, err := GetDBVersionContext(ctx, db)
if err != nil {
return err
}
if currentVersion == 0 {
log.Printf("goose: no migrations to run. current version: %d\n", currentVersion)
return nil
}
current, err := migrations.Current(currentVersion)
if err != nil {
log.Printf("goose: migration file not found for current version (%d), error: %s\n", currentVersion, err)
return err
}
if current.Version <= version {
log.Printf("goose: no migrations to run. current version: %d\n", currentVersion)
return nil
}
if err = current.DownContext(ctx, db); err != nil {
return err
}
}
}
// downToNoVersioning applies down migrations down to, but not including, the
// target version.
func downToNoVersioning(ctx context.Context, db *sql.DB, migrations Migrations, version int64) error {
var finalVersion int64
for i := len(migrations) - 1; i >= 0; i-- {
if version >= migrations[i].Version {
finalVersion = migrations[i].Version
break
}
migrations[i].noVersioning = true
if err := migrations[i].DownContext(ctx, db); err != nil {
return err
}
}
log.Printf("goose: down to current file version: %d\n", finalVersion)
return nil
}