-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmigrate.go
334 lines (266 loc) · 8.12 KB
/
migrate.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"database/sql"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
_ "github.com/mattn/go-sqlite3"
)
type MigrationRecord struct {
VersionId int64
TStamp time.Time
IsApplied int // was this a result of up() (1) or down() (0)
}
type Migration struct {
Version int64
Next int64 // next version, or -1 if none
Previous int64 // previous version, -1 if none
Source string // .go or .sql script
}
type MigrationSlice []Migration
// helpers so we can use pkg sort
func (s MigrationSlice) Len() int { return len(s) }
func (s MigrationSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s MigrationSlice) Less(i, j int) bool { return s[i].Version < s[j].Version }
type MigrationMap struct {
Migrations MigrationSlice // migrations, sorted according to Direction
Direction int // sort direction: 1 -> Up, 0 -> Down
}
func runMigrations(conf *DBConf, migrationsDir string, target int64, allowOutOfOrder bool) {
db, err := sql.Open(conf.Driver, conf.OpenStr)
if err != nil {
log.Fatal("couldn't open DB:", err)
}
defer db.Close()
current, e := ensureDBVersion(db)
if e != nil {
log.Fatalf("couldn't get DB version: %v", e)
}
var mm *MigrationMap
if allowOutOfOrder {
mm, err = collectAllUnappliedMigrations(migrationsDir, current, target, db)
} else {
mm, err = collectMigrations(migrationsDir, current, target)
}
if err != nil {
log.Fatal(err)
}
if len(mm.Migrations) == 0 {
fmt.Printf("goose: no migrations to run. current version: %d\n", current)
return
}
if allowOutOfOrder {
mm.Sort(true)
} else {
mm.Sort(current < target)
}
if conf.Env != "" {
fmt.Printf("goose: migrating db environment '%v', current version: %d, target: %d\n",
conf.Env, current, target)
} else {
fmt.Printf("goose: migrating db '%v', current version: %d, target: %d\n",
conf.OpenStr, current, target)
}
for _, m := range mm.Migrations {
var e error
switch filepath.Ext(m.Source) {
case ".go":
e = runGoMigration(conf, m.Source, m.Version, mm.Direction)
case ".sql":
e = runSQLMigration(db, m.Source, m.Version, mm.Direction)
}
if e != nil {
log.Fatalf("FAIL %v, quitting migration", e)
}
fmt.Println("OK ", filepath.Base(m.Source))
}
}
// collect all the valid looking migration scripts in the
// migrations folder, and key them by version
func collectMigrations(dirpath string, current, target int64) (mm *MigrationMap, err error) {
mm = &MigrationMap{}
// extract the numeric component of each migration,
// filter out any uninteresting files,
// and ensure we only have one file per migration version.
filepath.Walk(dirpath, func(name string, info os.FileInfo, err error) error {
if v, e := numericComponent(name); e == nil {
for _, m := range mm.Migrations {
if v == m.Version {
log.Fatalf("more than one file specifies the migration for version %d (%s and %s)",
v, m.Source, filepath.Join(dirpath, name))
}
}
if versionFilter(v, current, target) {
mm.Append(v, name)
}
}
return nil
})
return mm, nil
}
// collect all migrations that have not been run, including ones older than the current version
// This is only valid for migrating up
func collectAllUnappliedMigrations(dirpath string, current, target int64, db *sql.DB) (mm *MigrationMap, err error) {
mm = &MigrationMap{}
// extract the numeric component of each migration,
// filter out any uninteresting files,
// and ensure we only have one file per migration version.
filepath.Walk(dirpath, func(name string, info os.FileInfo, err error) error {
if v, e := numericComponent(name); e == nil {
for _, m := range mm.Migrations {
if v == m.Version {
log.Fatalf("more than one file specifies the migration for version %d (%s and %s)",
v, m.Source, filepath.Join(dirpath, name))
}
}
row := db.QueryRow("SELECT version_id, is_applied from goose_db_version where version_id=$1;", v)
var record MigrationRecord
err := row.Scan(&record.VersionId, &record.IsApplied)
if (err == sql.ErrNoRows) || (record.IsApplied == 0) {
mm.Append(v, name)
}
}
return nil
})
return mm, nil
}
func versionFilter(v, current, target int64) bool {
if target > current {
return v > current && v <= target
}
if target < current {
return v <= current && v > target
}
return false
}
func (mm *MigrationMap) Append(v int64, source string) {
mm.Migrations = append(mm.Migrations, Migration{
Version: v,
Next: -1,
Previous: -1,
Source: source,
})
}
func (mm *MigrationMap) Sort(direction bool) {
sort.Sort(mm.Migrations)
// set direction, and reverse order if need be
if direction {
mm.Direction = 1
} else {
mm.Direction = 0
}
if mm.Direction == 0 {
for i, j := 0, len(mm.Migrations)-1; i < j; i, j = i+1, j-1 {
mm.Migrations[i], mm.Migrations[j] = mm.Migrations[j], mm.Migrations[i]
}
}
// now that we're sorted in the appropriate direction,
// populate next and previous for each migration
for i, m := range mm.Migrations {
prev := int64(-1)
if i > 0 {
prev = mm.Migrations[i-1].Version
mm.Migrations[i-1].Next = m.Version
}
mm.Migrations[i].Previous = prev
}
}
// look for migration scripts with names in the form:
// XXX_descriptivename.ext
// where XXX specifies the version number
// and ext specifies the type of migration
func numericComponent(name string) (int64, error) {
base := filepath.Base(name)
if ext := filepath.Ext(base); ext != ".go" && ext != ".sql" {
return 0, errors.New("not a recognized migration file type")
}
idx := strings.Index(base, "_")
if idx < 0 {
return 0, errors.New("no separator found")
}
n, e := strconv.ParseInt(base[:idx], 10, 64)
if e == nil && n <= 0 {
return 0, errors.New("migration IDs must be greater than zero")
}
return n, e
}
// retrieve the current version for this DB.
// Create and initialize the DB version table if it doesn't exist.
func ensureDBVersion(db *sql.DB) (int64, error) {
rows, err := db.Query("SELECT version_id, is_applied from goose_db_version ORDER BY version_id DESC;")
if err != nil {
// XXX: cross platform method to detect failure reason
// for now, assume it was because the table didn't exist, and try to create it
return 0, createVersionTable(db)
}
defer rows.Close()
// The most recent record for each migration specifies
// whether it has been applied or rolled back.
// The first version we find that has been applied is the current version.
toSkip := make([]int64, 0)
for rows.Next() {
var row MigrationRecord
if err = rows.Scan(&row.VersionId, &row.IsApplied); err != nil {
log.Fatal("error scanning rows:", err)
}
// have we already marked this version to be skipped?
skip := false
for _, v := range toSkip {
if v == row.VersionId {
skip = true
break
}
}
// if version has been applied and not marked to be skipped, we're done
if row.IsApplied == 1 && !skip {
return row.VersionId, nil
}
// version is either not applied, or we've already seen a more
// recent version of it that was not applied.
if !skip {
toSkip = append(toSkip, row.VersionId)
}
}
panic("failure in ensureDBVersion()")
}
func createVersionTable(db *sql.DB) error {
txn, err := db.Begin()
if err != nil {
return err
}
// create the table and insert an initial value of 0
create := `CREATE TABLE goose_db_version (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
version_id integer NOT NULL,
is_applied integer NOT NULL,
tstamp text NULL default (datetime('now'))
);`
insert := "INSERT INTO goose_db_version (version_id, is_applied) VALUES (0, 1);"
for _, str := range []string{create, insert} {
if _, err := txn.Exec(str); err != nil {
txn.Rollback()
return err
}
}
return txn.Commit()
}
// wrapper for ensureDBVersion for callers that don't already have
// their own DB instance
func getDBVersion(conf *DBConf) int64 {
db, err := sql.Open(conf.Driver, conf.OpenStr)
if err != nil {
log.Fatal("couldn't open DB:", err)
}
defer db.Close()
version, err := ensureDBVersion(db)
if err != nil {
log.Fatalf("couldn't get DB version: %v", err)
}
return version
}