forked from robinjoseph08/go-pg-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrations.go
74 lines (60 loc) · 1.51 KB
/
migrations.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
// Package migrations provides a robust mechanism for registering, creating, and
// running migrations using go-pg-pg.
package migrations
import (
"errors"
"time"
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
)
// Errors that can be returned from Run.
var (
ErrAlreadyLocked = errors.New("migration table is already locked")
ErrCreateRequiresName = errors.New("migration name is required for create")
)
// MigrationOptions allows settings to be configured on a per-migration basis.
type MigrationOptions struct {
DisableTransaction bool
}
type migration struct {
tableName struct{} `sql:"migrations,alias:migrations"`
ID int32
Name string
Batch int32
CompletedAt time.Time
Up func(orm.DB) error `sql:"-"`
Down func(orm.DB) error `sql:"-"`
DisableTransaction bool `sql:"-"`
}
type lock struct {
tableName struct{} `sql:"migration_lock,alias:migration_lock"`
ID string
IsLocked bool `sql:",notnull"`
}
const lockID = "lock"
// Run takes in a directory and an argument slice and runs the appropriate command.
func Run(db *pg.DB, directory string, args []string) error {
err := ensureMigrationTables(db)
if err != nil {
return err
}
cmd := ""
if len(args) > 1 {
cmd = args[1]
}
switch cmd {
case "migrate":
return migrate(db, directory)
case "create":
if len(args) < 3 {
return ErrCreateRequiresName
}
name := args[2]
return create(directory, name)
case "rollback":
return rollback(db, directory)
default:
help(directory)
return nil
}
}