-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathup.go
83 lines (64 loc) · 1.84 KB
/
up.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
package rockhopper
import (
"context"
)
func UpBySteps(ctx context.Context, db *DB, m *Migration, steps int, callbacks ...func(m *Migration)) error {
for ; steps > 0 && m != nil; m = m.Next {
descMigration("upgrading", m)
if err := m.Up(ctx, db); err != nil {
return err
}
for _, cb := range callbacks {
cb(m)
}
steps--
}
return nil
}
func Upgrade(ctx context.Context, db *DB, migrations MigrationSlice) error {
migrationMap := migrations.MapByPackage()
for _, pkgMigrations := range migrationMap {
pkgMigrations = pkgMigrations.Sort().Connect()
_, lastAppliedMigration, err := db.FindLastAppliedMigration(ctx, pkgMigrations)
if err != nil {
return err
}
startMigration := pkgMigrations.Head()
if lastAppliedMigration != nil {
startMigration = lastAppliedMigration.Next
}
err = Up(ctx, db, startMigration, 0, func(m *Migration) {
// log.Infof("migration %d is applied", m.Version)
})
if err != nil {
return err
}
}
return nil
}
// UpgradeFromGo runs the migration upgrades from the registered go-code migration
// parameter packageNames is the package list you want to filter
func UpgradeFromGo(ctx context.Context, db *DB, packageNames ...string) error {
var migrations MigrationSlice
for _, migration := range registeredGoMigrations {
migrations = append(migrations, migration)
}
return Upgrade(ctx, db, migrations.FilterPackage(packageNames))
}
// Up executes the Up methods from the given migration object
// and continues the upgrades to the latest migration.
func Up(ctx context.Context, db *DB, m *Migration, to int64, callbacks ...func(m *Migration)) error {
for ; m != nil; m = m.Next {
if to > 0 && m.Version > to {
break
}
descMigration("upgrading", m)
if err := m.Up(ctx, db); err != nil {
return err
}
for _, cb := range callbacks {
cb(m)
}
}
return nil
}