-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration_options.go
124 lines (105 loc) · 2.76 KB
/
migration_options.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package golembic
import (
"context"
"database/sql"
"io/ioutil"
)
// OptPrevious sets the previous on a migration.
func OptPrevious(previous string) MigrationOption {
return func(m *Migration) error {
m.Previous = previous
return nil
}
}
// OptRevision sets the revision on a migration.
func OptRevision(revision string) MigrationOption {
return func(m *Migration) error {
if revision == "" {
return ErrMissingRevision
}
m.Revision = revision
return nil
}
}
// OptDescription sets the description on a migration.
func OptDescription(description string) MigrationOption {
return func(m *Migration) error {
m.Description = description
return nil
}
}
// OptMilestone sets the milestone flag on a migration.
func OptMilestone(milestone bool) MigrationOption {
return func(m *Migration) error {
m.Milestone = milestone
return nil
}
}
// OptUp sets the `up` function on a migration.
func OptUp(up UpMigration) MigrationOption {
return func(m *Migration) error {
if up == nil {
return ErrNilInterface
}
m.Up = up
return nil
}
}
// OptUpFromSQL returns an option that sets the `up` function to execute a
// SQL statement.
func OptUpFromSQL(statement string) MigrationOption {
up := func(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, statement)
return err
}
return func(m *Migration) error {
m.Up = up
return nil
}
}
// OptUpFromFile returns an option that sets the `up` function to execute a
// SQL statement that is stored in a file.
func OptUpFromFile(filename string) MigrationOption {
statement, err := ioutil.ReadFile(filename)
if err != nil {
return OptAlwaysError(err)
}
return OptUpFromSQL(string(statement))
}
// OptUpConn sets the non-transactional `up` function on a migration.
func OptUpConn(up UpMigrationConn) MigrationOption {
return func(m *Migration) error {
if up == nil {
return ErrNilInterface
}
m.UpConn = up
return nil
}
}
// OptUpConnFromSQL returns an option that sets the non-transctional `up`
// function to execute a SQL statement.
func OptUpConnFromSQL(statement string) MigrationOption {
up := func(ctx context.Context, conn *sql.Conn) error {
_, err := conn.ExecContext(ctx, statement)
return err
}
return func(m *Migration) error {
m.UpConn = up
return nil
}
}
// OptUpConnFromFile returns an option that sets the non-transctional `up`
// function to execute a SQL statement that is stored in a file.
func OptUpConnFromFile(filename string) MigrationOption {
statement, err := ioutil.ReadFile(filename)
if err != nil {
return OptAlwaysError(err)
}
return OptUpConnFromSQL(string(statement))
}
// OptAlwaysError returns an option that always returns an error.
func OptAlwaysError(err error) MigrationOption {
return func(m *Migration) error {
return err
}
}