Skip to content

Commit aab1974

Browse files
jbenetStebalien
authored andcommitted
fs-repo-migrations can now revert
1 parent 4e8e0b4 commit aab1974

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

main.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func GetIpfsDir() (string, error) {
6565
return "", err
6666
}
6767

68-
func runMigration(n int) error {
69-
fmt.Printf("===> Running migration %d to %d...\n", n, n+1)
68+
func runMigration(from int, to int, backward bool) error {
69+
fmt.Printf("===> Running migration %d to %d...\n", from, to)
7070
path, err := GetIpfsDir()
7171
if err != nil {
7272
return err
@@ -76,18 +76,30 @@ func runMigration(n int) error {
7676
opts.Path = path
7777
opts.Verbose = true
7878

79-
err = migrations[n].Apply(opts)
79+
if to > from {
80+
err = migrations[from].Apply(opts)
81+
} else if to < from {
82+
err = migrations[to].Revert(opts)
83+
} else {
84+
// catch this earlier. expected invariant violated.
85+
err = fmt.Errorf("attempt to run migration to same version")
86+
}
8087
if err != nil {
81-
return fmt.Errorf("migration %d to %d failed: %s", n, n+1, err)
88+
return fmt.Errorf("migration %d to %d failed: %s", from, to, err)
8289
}
83-
fmt.Printf("===> Migration %d to %d succeeded!\n", n, n+1)
90+
fmt.Printf("===> Migration %d to %d succeeded!\n", from, to)
8491
return nil
8592
}
8693

8794
func doMigrate(from, to int) error {
88-
cur := from
89-
for ; cur < to; cur++ {
90-
err := runMigration(cur)
95+
backward := from > to
96+
step := 1
97+
if backward {
98+
step = -1
99+
}
100+
101+
for cur := from; cur != to; cur += step {
102+
err := runMigration(cur, cur + step, backward)
91103
if err != nil {
92104
return err
93105
}
@@ -158,8 +170,8 @@ func main() {
158170
os.Exit(1)
159171
}
160172

161-
if vnum >= *target {
162-
fmt.Println("ipfs migration: already at or above target version number")
173+
if vnum == *target {
174+
fmt.Println("ipfs migration: already at target version number")
163175
return
164176
}
165177

0 commit comments

Comments
 (0)