forked from kolide/goose
-
Notifications
You must be signed in to change notification settings - Fork 2
/
goose.go
75 lines (69 loc) · 1.64 KB
/
goose.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
package goose
import (
"database/sql"
"fmt"
)
var (
minVersion = int64(0)
maxVersion = int64((1 << 63) - 1)
)
// Client stores the migration state and preferences. Prefer interacting with
// the Goose API through a Client struct created with New rather than using the
// global Client and functions.
type Client struct {
// TableName is the name of the table used to store migration status
// for this client.
TableName string
// Dialect is the SqlDialect to use.
Dialect SqlDialect
// Migrations is the list of migrations.
Migrations Migrations
}
func New(tableName string, dialect SqlDialect) *Client {
return &Client{
TableName: tableName,
Dialect: dialect,
}
}
func Run(command string, db *sql.DB, dir string, args ...string) error {
switch command {
case "up":
if err := globalGoose.Up(db, dir); err != nil {
return err
}
case "up-by-one":
if err := globalGoose.UpByOne(db, dir); err != nil {
return err
}
case "create":
if len(args) == 0 {
return fmt.Errorf("create must be of form: goose [OPTIONS] DRIVER DBSTRING create NAME [go|sql]")
}
migrationType := "go"
if len(args) == 2 {
migrationType = args[1]
}
if err := Create(db, dir, args[0], migrationType); err != nil {
return err
}
case "down":
if err := globalGoose.Down(db, dir); err != nil {
return err
}
case "redo":
if err := globalGoose.Redo(db, dir); err != nil {
return err
}
case "status":
if err := globalGoose.Status(db, dir); err != nil {
return err
}
case "version":
if err := globalGoose.Version(db, dir); err != nil {
return err
}
default:
return fmt.Errorf("%q: no such command", command)
}
return nil
}