-
Notifications
You must be signed in to change notification settings - Fork 22
/
rollback.go
78 lines (65 loc) · 1.71 KB
/
rollback.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
package migrations
import (
"fmt"
"sort"
"github.com/go-pg/pg/v10"
)
func (m *migrator) rollback() error {
// sort the registered migrations by name (which will sort by the
// timestamp in their names)
sort.Slice(migrations, func(i, j int) bool {
return migrations[i].Name > migrations[j].Name
})
// look at the migrations table to see the already run migrations
completed, err := m.getCompletedMigrations()
if err != nil {
return err
}
// acquire the migration lock from the migrations_lock table
err = m.acquireLock()
if err != nil {
return err
}
defer m.releaseLock()
batch, err := m.getLastBatchNumber()
if err != nil {
return err
}
// if no migrations have been run yet, exit early
if batch == 0 {
fmt.Println("No migrations have been run yet")
return nil
}
rollback := getMigrationsForBatch(completed, batch)
rollback = filterMigrations(migrations, rollback, true)
fmt.Printf("Rolling back batch %d with %d migration(s)...\n", batch, len(rollback))
for _, mig := range rollback {
var err error
if mig.DisableTransaction {
err = mig.Down(m.db)
} else {
err = m.db.RunInTransaction(m.db.Context(), func(tx *pg.Tx) error {
return mig.Down(tx)
})
}
if err != nil {
return fmt.Errorf("%s: %s", mig.Name, err)
}
_, err = m.db.
Exec(fmt.Sprintf("DELETE FROM %q WHERE name = ?", escapeTableName(m.opts.MigrationsTableName)), mig.Name)
if err != nil {
return fmt.Errorf("%s: %s", mig.Name, err)
}
fmt.Printf("Finished rolling back %q\n", mig.Name)
}
return nil
}
func getMigrationsForBatch(migrations []*migration, batch int32) []*migration {
var m []*migration
for _, mig := range migrations {
if mig.Batch == batch {
m = append(m, mig)
}
}
return m
}