-
-
Notifications
You must be signed in to change notification settings - Fork 246
/
migration_info.go
76 lines (66 loc) · 1.69 KB
/
migration_info.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
package pop
import "fmt"
// Migration handles the data for a given database migration
type Migration struct {
// Path to the migration (./migrations/123_create_widgets.up.sql)
Path string
// Version of the migration (123)
Version string
// Name of the migration (create_widgets)
Name string
// Direction of the migration (up)
Direction string
// Type of migration (sql)
Type string
// DB type (all|postgres|mysql...)
DBType string
// Runner function to run/execute the migration
Runner func(Migration, *Connection) error
}
// Run the migration. Returns an error if there is
// no mf.Runner defined.
func (mf Migration) Run(c *Connection) error {
if mf.Runner == nil {
return fmt.Errorf("no runner defined for %s", mf.Path)
}
return mf.Runner(mf, c)
}
// Migrations is a collection of Migration
type Migrations []Migration
func (mfs Migrations) Len() int {
return len(mfs)
}
func (mfs Migrations) Swap(i, j int) {
mfs[i], mfs[j] = mfs[j], mfs[i]
}
func (mfs *Migrations) Filter(f func(mf Migration) bool) {
vsf := make(Migrations, 0)
for _, v := range *mfs {
if f(v) {
vsf = append(vsf, v)
}
}
*mfs = vsf
}
type (
UpMigrations struct {
Migrations
}
DownMigrations struct {
Migrations
}
)
func (mfs UpMigrations) Less(i, j int) bool {
if mfs.Migrations[i].Version == mfs.Migrations[j].Version {
// force "all" to the back
return mfs.Migrations[i].DBType != "all"
}
return mfs.Migrations[i].Version < mfs.Migrations[j].Version
}
func (mfs DownMigrations) Less(i, j int) bool {
if mfs.Migrations[i].Version == mfs.Migrations[j].Version {
// force "all" to the back
return mfs.Migrations[i].DBType != "all"
}
return mfs.Migrations[i].Version > mfs.Migrations[j].Version
}