-
Notifications
You must be signed in to change notification settings - Fork 4
/
migrate.go
345 lines (295 loc) · 8.23 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
335
336
337
338
339
340
341
342
343
344
345
// Package migrate provides simple migration functions Up, Down, and To, as well as a Migrator.
// Up, Down, and To are one-liner convenience functions that use default Options.
// If you need custom Options, use New.
package migrate
import (
"context"
"database/sql"
"errors"
"fmt"
"io/fs"
"regexp"
)
var (
upMatcher = regexp.MustCompile(`^([\w-]+).up.sql$`)
downMatcher = regexp.MustCompile(`^([\w-]+).down.sql`)
tableMatcher = regexp.MustCompile(`^[\w.]+$`)
)
// Up from the current version.
func Up(ctx context.Context, db *sql.DB, fsys fs.FS) error {
m := New(Options{DB: db, FS: fsys})
return m.MigrateUp(ctx)
}
// Down from the current version.
func Down(ctx context.Context, db *sql.DB, fsys fs.FS) error {
m := New(Options{DB: db, FS: fsys})
return m.MigrateDown(ctx)
}
// To the given version.
func To(ctx context.Context, db *sql.DB, fsys fs.FS, version string) error {
m := New(Options{DB: db, FS: fsys})
return m.MigrateTo(ctx, version)
}
// callback that can be run before and after each migration.
type callback = func(ctx context.Context, tx *sql.Tx, version string) error
type Migrator struct {
after callback
before callback
db *sql.DB
fs fs.FS
table string
}
// Options for New. DB and FS are always required.
type Options struct {
After callback
Before callback
DB *sql.DB
FS fs.FS
Table string
}
// New Migrator with Options.
// If Options.Table is not set, defaults to "migrations". The table name must match ^[\w.]+$ .
// New panics on illegal options.
func New(opts Options) *Migrator {
if opts.DB == nil || opts.FS == nil {
panic("DB and FS must be set")
}
if opts.Table == "" {
opts.Table = "migrations"
}
if !tableMatcher.MatchString(opts.Table) {
panic("illegal table name " + opts.Table + ", must match " + tableMatcher.String())
}
return &Migrator{
after: opts.After,
before: opts.Before,
db: opts.DB,
fs: opts.FS,
table: opts.Table,
}
}
// MigrateUp from the current version.
func (m *Migrator) MigrateUp(ctx context.Context) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("error migrating up: %w", err)
}
}()
if err := m.createMigrationsTable(ctx); err != nil {
return err
}
currentVersion, err := m.getCurrentVersion(ctx)
if err != nil {
return err
}
names, err := m.getFilenames(upMatcher)
if err != nil {
return err
}
for _, name := range names {
thisVersion := upMatcher.ReplaceAllString(name, "$1")
if thisVersion <= currentVersion {
continue
}
if err := m.apply(ctx, name, thisVersion); err != nil {
return err
}
}
return nil
}
// MigrateDown from the current version.
func (m *Migrator) MigrateDown(ctx context.Context) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("error migrating down: %w", err)
}
}()
if err := m.createMigrationsTable(ctx); err != nil {
return err
}
currentVersion, err := m.getCurrentVersion(ctx)
if err != nil {
return err
}
names, err := m.getFilenames(downMatcher)
if err != nil {
return err
}
for i := len(names) - 1; i >= 0; i-- {
thisVersion := downMatcher.ReplaceAllString(names[i], "$1")
if thisVersion > currentVersion {
continue
}
nextVersion := ""
if i > 0 {
nextVersion = downMatcher.ReplaceAllString(names[i-1], "$1")
}
if err := m.apply(ctx, names[i], nextVersion); err != nil {
return err
}
}
return nil
}
func (m *Migrator) MigrateTo(ctx context.Context, version string) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("error migrating to: %w", err)
}
}()
if version == "" {
return m.MigrateDown(ctx)
}
if err := m.createMigrationsTable(ctx); err != nil {
return err
}
currentVersion, err := m.getCurrentVersion(ctx)
if err != nil {
return err
}
if currentVersion == version {
return nil
}
var matcher *regexp.Regexp
if version > currentVersion {
matcher = upMatcher
} else {
matcher = downMatcher
}
names, err := m.getFilenames(matcher)
if err != nil {
return err
}
foundVersion := false
for _, name := range names {
thisVersion := matcher.ReplaceAllString(name, "$1")
if thisVersion == version {
foundVersion = true
}
}
if !foundVersion {
return errors.New("error finding version " + version)
}
switch {
case version > currentVersion:
for _, name := range names {
thisVersion := matcher.ReplaceAllString(name, "$1")
if thisVersion <= currentVersion {
continue
}
if thisVersion > version {
break
}
if err := m.apply(ctx, name, thisVersion); err != nil {
return err
}
}
case version < currentVersion:
for i := len(names) - 1; i >= 0; i-- {
thisVersion := matcher.ReplaceAllString(names[i], "$1")
if thisVersion > currentVersion {
continue
}
if thisVersion <= version {
break
}
nextVersion := matcher.ReplaceAllString(names[i-1], "$1")
if err := m.apply(ctx, names[i], nextVersion); err != nil {
return err
}
}
}
return nil
}
// apply a file identified by name and update to version.
func (m *Migrator) apply(ctx context.Context, name, version string) error {
content, err := fs.ReadFile(m.fs, name)
if err != nil {
return fmt.Errorf("error reading migration file %v: %w", name, err)
}
return m.inTransaction(ctx, func(tx *sql.Tx) error {
if m.before != nil {
if err := m.before(ctx, tx, version); err != nil {
return fmt.Errorf("error in 'before' callback when applying version %v from %v: %w", version, name, err)
}
}
// Normally we wouldn't just string interpolate the version like this,
// but because we know the version has been matched against the regexes, we know it's safe.
if _, err := tx.ExecContext(ctx, `update `+m.table+` set version = '`+version+`'`); err != nil {
return fmt.Errorf("error updating version to %v: %w", version, err)
}
if _, err := tx.ExecContext(ctx, string(content)); err != nil {
return fmt.Errorf("error running migration %v from %v: %w", version, name, err)
}
if m.after != nil {
if err := m.after(ctx, tx, version); err != nil {
return fmt.Errorf("error in 'after' callback when applying version %v from %v: %w", version, name, err)
}
}
return nil
})
}
// getFilenames alphabetically where the name matches the given matcher.
func (m *Migrator) getFilenames(matcher *regexp.Regexp) ([]string, error) {
var names []string
entries, err := fs.ReadDir(m.fs, ".")
if err != nil {
return names, err
}
for _, entry := range entries {
if !matcher.MatchString(entry.Name()) {
continue
}
names = append(names, entry.Name())
}
return names, nil
}
// createMigrationsTable if it does not exist already, and insert the empty version if it's empty.
func (m *Migrator) createMigrationsTable(ctx context.Context) error {
return m.inTransaction(ctx, func(tx *sql.Tx) error {
if _, err := tx.ExecContext(ctx, `create table if not exists `+m.table+` (version text not null)`); err != nil {
return fmt.Errorf("error creating migrations table %v: %w", m.table, err)
}
var exists bool
if err := tx.QueryRowContext(ctx, `select exists (select * from `+m.table+`)`).Scan(&exists); err != nil {
return err
}
if !exists {
if _, err := tx.ExecContext(ctx, `insert into `+m.table+` values ('')`); err != nil {
return err
}
}
return nil
})
}
// getCurrentVersion from the migrations table.
func (m *Migrator) getCurrentVersion(ctx context.Context) (string, error) {
var version string
if err := m.db.QueryRowContext(ctx, `select version from `+m.table+``).Scan(&version); err != nil {
return "", fmt.Errorf("error getting current migration version: %w", err)
}
return version, nil
}
func (m *Migrator) inTransaction(ctx context.Context, callback func(tx *sql.Tx) error) (err error) {
tx, err := m.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("error beginning transaction: %w", err)
}
defer func() {
if rec := recover(); rec != nil {
err = rollback(tx, fmt.Errorf("panic: %v", rec))
}
}()
if err := callback(tx); err != nil {
return rollback(tx, err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("error committing transaction: %w", err)
}
return nil
}
func rollback(tx *sql.Tx, err error) error {
if txErr := tx.Rollback(); txErr != nil {
return fmt.Errorf("error rolling back transaction after error (transaction error: %v), original error: %w", txErr, err)
}
return err
}