-
Notifications
You must be signed in to change notification settings - Fork 8
/
kamimai.go
101 lines (80 loc) · 1.82 KB
/
kamimai.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
package kamimai
import (
"database/sql"
"github.com/eure/kamimai/core"
)
// Version represents kamimai's semantic version.
const Version = "v0.4.4"
// Current returns the current migration version.
func Current(c *core.Config) (uint64, error) {
// driver
driver := core.GetDriver(c.Driver())
if err := driver.Open(c.Dsn()); err != nil {
return 0, err
}
// current version
return driver.Version().Current()
}
// Sync applies non-applied migration files.
func Sync(c *core.Config) error {
// driver
driver := core.GetDriver(c.Driver())
if err := driver.Open(c.Dsn()); err != nil {
return err
}
// current version
current, err := driver.Version().Current()
if err != nil {
return err
}
// create a service
svc := core.NewService(c).
WithVersion(current).
WithDriver(driver)
return driver.Transaction(func(tx *sql.Tx) error {
// Sync all migrations
return svc.Sync()
})
}
// Up applies up migration files.
func Up(c *core.Config) error {
// driver
driver := core.GetDriver(c.Driver())
if err := driver.Open(c.Dsn()); err != nil {
return err
}
// current version
current, err := driver.Version().Current()
if err != nil {
return err
}
// create a service
svc := core.NewService(c).
WithVersion(current).
WithDriver(driver)
return driver.Transaction(func(tx *sql.Tx) error {
// Up migrations
return svc.Up()
})
}
// Down applies down migration files.
func Down(c *core.Config) error {
// driver
driver := core.GetDriver(c.Driver())
if err := driver.Open(c.Dsn()); err != nil {
return err
}
// current version
current, err := driver.Version().Current()
if err != nil {
return err
}
// create a service
svc := core.NewService(c).
WithVersion(current).
WithDriver(driver)
return driver.Transaction(func(tx *sql.Tx) error {
// Down migrations
return svc.Down()
})
}