-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmigration.go
46 lines (37 loc) · 987 Bytes
/
migration.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
package migration
import "database/sql"
// Migration is a migration interface. A migration can apply itself, rollback
// itself and has a unique name.
type Migration interface {
Apply(tx *sql.Tx) error
Rollback(tx *sql.Tx) error
Name() string
}
// Struct is a simple implementation of the Migration interface.
type Struct struct {
NameString string
ApplyFunc func(tx *sql.Tx) error
RollbackFunc func(tx *sql.Tx) error
}
// Apply implements Migration for Struct.
func (s Struct) Apply(tx *sql.Tx) error {
return s.ApplyFunc(tx)
}
// Rollback implements Migration for Struct.
func (s Struct) Rollback(tx *sql.Tx) error {
return s.RollbackFunc(tx)
}
// Name implements Migration for Struct.
func (s Struct) Name() string {
return s.NameString
}
var _ Migration = Struct{}
// FindByName finds a migration by name.
func FindByName(migrations []Migration, name string) Migration {
for _, m := range migrations {
if m.Name() == name {
return m
}
}
return nil
}