-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
399 lines (325 loc) · 8.36 KB
/
db.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package dbmate
import (
"database/sql"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"time"
)
// DefaultMigrationsDir specifies default directory to find migration files
var DefaultMigrationsDir = "./db/migrations"
// DefaultProject specifies the default name to associate with the migrations
var DefaultProject = "default"
// DB allows dbmate actions to be performed on a specified database
type DB struct {
DatabaseURL *url.URL
MigrationsDir string
Project string
}
// NewDB initializes a new dbmate database
func NewDB(databaseURL *url.URL) *DB {
return &DB{
DatabaseURL: databaseURL,
MigrationsDir: DefaultMigrationsDir,
Project: DefaultProject,
}
}
// GetDriver loads the required database driver
func (db *DB) GetDriver() (Driver, error) {
return GetDriver(db.DatabaseURL.Scheme)
}
// Up creates the database (if necessary) and runs migrations
func (db *DB) Up(lockTimeoutSecs int) error {
drv, err := db.GetDriver()
if err != nil {
return err
}
// create database if it does not already exist
// skip this step if we cannot determine status
// (e.g. user does not have list database permission)
exists, err := drv.DatabaseExists(db.DatabaseURL)
if err == nil && !exists {
if err := drv.CreateDatabase(db.DatabaseURL); err != nil {
return err
}
}
// migrate
return db.Migrate(lockTimeoutSecs)
}
// Create creates the current database
func (db *DB) Create() error {
drv, err := db.GetDriver()
if err != nil {
return err
}
return drv.CreateDatabase(db.DatabaseURL)
}
// Drop drops the current database (if it exists)
func (db *DB) Drop() error {
drv, err := db.GetDriver()
if err != nil {
return err
}
return drv.DropDatabase(db.DatabaseURL)
}
// RecordOnly will record without applying all unapplied filesystem migrations
func (db *DB) RecordOnly() error {
re := regexp.MustCompile(`^\d.*\.sql$`)
files, err := findMigrationFiles(db.MigrationsDir, re)
if err != nil {
return err
}
if len(files) == 0 {
return fmt.Errorf("no migration files found")
}
drv, sqlDB, err := db.openDatabaseForMigration()
if err != nil {
return err
}
defer mustClose(sqlDB)
if err := drv.Lock(sqlDB); err != nil {
return err
}
defer drv.Unlock(sqlDB)
applied, err := drv.SelectMigrations(sqlDB, -1, db.Project)
if err != nil {
return err
}
for _, filename := range files {
ver := migrationVersion(filename)
if ok := applied[ver]; ok {
continue
}
err = doTransaction(sqlDB, func(tx Transaction) error {
if err := drv.InsertMigration(tx, ver, db.Project); err != nil {
return err
}
return nil
})
}
return nil
}
const migrationTemplate = "-- migrate:up\n\n\n-- migrate:down\n\n"
// New creates a new migration file
func (db *DB) New(name string) error {
// new migration name
timestamp := time.Now().UTC().Format("20060102150405")
if name == "" {
return fmt.Errorf("please specify a name for the new migration")
}
name = fmt.Sprintf("%s_%s.sql", timestamp, name)
// create migrations dir if missing
if err := os.MkdirAll(db.MigrationsDir, 0755); err != nil {
return fmt.Errorf("unable to create directory `%s`", db.MigrationsDir)
}
// check file does not already exist
path := filepath.Join(db.MigrationsDir, name)
fmt.Printf("Creating migration: %s\n", path)
if _, err := os.Stat(path); !os.IsNotExist(err) {
return fmt.Errorf("file already exists")
}
// write new migration
file, err := os.Create(path)
if err != nil {
return err
}
defer mustClose(file)
_, err = file.WriteString(migrationTemplate)
if err != nil {
return err
}
return nil
}
func doTransaction(db *sql.DB, txFunc func(Transaction) error) error {
tx, err := db.Begin()
if err != nil {
return err
}
if err := txFunc(tx); err != nil {
if err1 := tx.Rollback(); err1 != nil {
return err1
}
return err
}
return tx.Commit()
}
func (db *DB) openDatabaseForMigration() (Driver, *sql.DB, error) {
drv, err := db.GetDriver()
if err != nil {
return nil, nil, err
}
sqlDB, err := drv.Open(db.DatabaseURL)
if err != nil {
return nil, nil, err
}
if err := drv.CreateMigrationsTable(sqlDB); err != nil {
mustClose(sqlDB)
return nil, nil, err
}
return drv, sqlDB, nil
}
// Migrate migrates database to the latest version
func (db *DB) Migrate(lockTimeoutSecs int) error {
re := regexp.MustCompile(`^\d.*\.sql$`)
files, err := findMigrationFiles(db.MigrationsDir, re)
if err != nil {
return err
}
if len(files) == 0 {
return fmt.Errorf("no migration files found")
}
drv, sqlDB, err := db.openDatabaseForMigration()
if err != nil {
return err
}
defer mustClose(sqlDB)
return RunInLock(drv, sqlDB, lockTimeoutSecs, func(driver Driver, sqlDB *sql.DB) error {
alreadyApplied, err := driver.SelectMigrations(sqlDB, -1, db.Project)
if err != nil {
return err
}
for _, filename := range files {
ver := migrationVersion(filename)
if ok := alreadyApplied[ver]; ok {
continue
}
fmt.Printf("Applying: %s\n", filename)
migration, err := parseMigration(filepath.Join(db.MigrationsDir, filename))
if err != nil {
return err
}
// begin transaction
err = doTransaction(sqlDB, func(tx Transaction) error {
// run actual migration
if _, err := tx.Exec(migration["up"]); err != nil {
return err
}
// record migration
if err := drv.InsertMigration(tx, ver, db.Project); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
}
return nil
})
}
func findMigrationFiles(dir string, re *regexp.Regexp) ([]string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("could not find migrations directory `%s`", dir)
}
matches := []string{}
for _, file := range files {
if file.IsDir() {
continue
}
name := file.Name()
if !re.MatchString(name) {
continue
}
matches = append(matches, name)
}
sort.Strings(matches)
return matches, nil
}
func findMigrationFile(dir string, ver string) (string, error) {
if ver == "" {
panic("migration version is required")
}
ver = regexp.QuoteMeta(ver)
re := regexp.MustCompile(fmt.Sprintf(`^%s.*\.sql$`, ver))
files, err := findMigrationFiles(dir, re)
if err != nil {
return "", err
}
if len(files) == 0 {
return "", fmt.Errorf("can't find migration file: %s*.sql", ver)
}
return files[0], nil
}
func migrationVersion(filename string) string {
return regexp.MustCompile(`^\d+`).FindString(filename)
}
// parseMigration reads a migration file into a map with up/down keys
// implementation is similar to regexp.Split()
func parseMigration(path string) (map[string]string, error) {
// read migration file into string
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
contents := string(data)
// split string on our trigger comment
separatorRegexp := regexp.MustCompile(`(?m)^-- migrate:(.*)$`)
matches := separatorRegexp.FindAllStringSubmatchIndex(contents, -1)
migrations := map[string]string{}
direction := ""
beg := 0
end := 0
for _, match := range matches {
end = match[0]
if direction != "" {
// write previous direction to output map
migrations[direction] = contents[beg:end]
}
// each match records the start of a new direction
direction = contents[match[2]:match[3]]
beg = match[1]
}
// write final direction to output map
migrations[direction] = contents[beg:]
return migrations, nil
}
// Rollback rolls back the most recent migration
func (db *DB) Rollback() error {
drv, sqlDB, err := db.openDatabaseForMigration()
if err != nil {
return err
}
defer mustClose(sqlDB)
applied, err := drv.SelectMigrations(sqlDB, 1, db.Project)
if err != nil {
return err
}
// grab most recent applied migration (applied has len=1)
latest := ""
for ver := range applied {
latest = ver
}
if latest == "" {
return fmt.Errorf("can't rollback: no migrations have been applied")
}
filename, err := findMigrationFile(db.MigrationsDir, latest)
if err != nil {
return err
}
fmt.Printf("Rolling back: %s\n", filename)
migration, err := parseMigration(filepath.Join(db.MigrationsDir, filename))
if err != nil {
return err
}
// begin transaction
err = doTransaction(sqlDB, func(tx Transaction) error {
// rollback migration
if _, err := tx.Exec(migration["down"]); err != nil {
return err
}
// remove migration record
if err := drv.DeleteMigration(tx, latest); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}