-
Notifications
You must be signed in to change notification settings - Fork 4
/
gloat.go
81 lines (65 loc) · 2.11 KB
/
gloat.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
package gloat
import "database/sql"
// Gloat glues all the components needed to apply and revert
// migrations.
type Gloat struct {
// Source is an incoming source of migrations. It can be File System or
// embedded migrations with go-bindata, etc.
Source Source
// Store is the place where we store the applied migration versions. Can
// be one of the builtin database store or custom file storage, etc.
Store Store
// Executor applies migrations and marks the newly applied migration
// versions in the Store.
Executor Executor
}
// Unapplied returns the unapplied migrations in the current gloat.
func (c *Gloat) Unapplied() (Migrations, error) {
return UnappliedMigrations(c.Store, c.Source)
}
// Current returns the latest applied migration. Even if no error is returned,
// the current migration can be nil.
//
// This is the case when the last applied migration is no longer available from
// the source or there are no migrations to begin with.
func (c *Gloat) Current() (*Migration, error) {
appliedMigrations, err := c.Store.Collect()
if err != nil {
return nil, err
}
currentMigration := appliedMigrations.Current()
if currentMigration == nil {
return nil, nil
}
availableMigrations, err := c.Source.Collect()
if err != nil {
return nil, err
}
for i := len(availableMigrations) - 1; i >= 0; i-- {
migration := availableMigrations[i]
if migration.Version == currentMigration.Version {
return migration, nil
}
}
return nil, nil
}
// Apply applies a migration.
func (c *Gloat) Apply(migration *Migration) error {
return c.Executor.Up(migration, c.Store)
}
// Revert rollbacks a migration.
func (c *Gloat) Revert(migration *Migration) error {
return c.Executor.Down(migration, c.Store)
}
// SQLExecer is an interface compatible with sql.Tx.Exec. Can be passed as
// nil on non-SQL stores.
type SQLExecer interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
}
// SQLTransactor is usually satisfied by *sql.DB, but can be used by wrappers
// around it.
type SQLTransactor interface {
SQLExecer
Begin() (*sql.Tx, error)
}