-
Notifications
You must be signed in to change notification settings - Fork 230
/
migrate.go
429 lines (390 loc) · 11.6 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package migrate
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"text/template"
"time"
"ariga.io/atlas/sql/schema"
)
type (
// A Plan defines a planned changeset that its execution brings the database to
// the new desired state. Additional information is calculated by the different
// drivers to indicate if the changeset is transactional (can be rolled-back) and
// reversible (a down file can be generated to it).
Plan struct {
// Name of the plan. Provided by the user or auto-generated.
Name string
// Reversible describes if the changeset is reversible.
Reversible bool
// Transactional describes if the changeset is transactional.
Transactional bool
// Changes defines the list of changeset in the plan.
Changes []*Change
}
// A Change of migration.
Change struct {
// Cmd or statement to execute.
Cmd string
// Args for placeholder parameters in the statement above.
Args []interface{}
// A Comment describes the change.
Comment string
// Reverse contains the "reversed statement" if
// command is reversible.
Reverse string
// The Source that caused this change, or nil.
Source schema.Change
}
)
type (
// The Driver interface must be implemented by the different dialects to support database
// migration authoring/planning and applying. ExecQuerier, Inspector and Differ, provide
// basic schema primitives for inspecting database schemas, calculate the difference between
// schema elements, and executing raw SQL statements. The PlanApplier interface wraps the
// methods for generating migration plan for applying the actual changes on the database.
Driver interface {
schema.Differ
schema.ExecQuerier
schema.Inspector
PlanApplier
}
// PlanApplier wraps the methods for planning and applying changes
// on the database.
PlanApplier interface {
// PlanChanges returns a migration plan for applying the given changeset.
PlanChanges(context.Context, string, []schema.Change) (*Plan, error)
// ApplyChanges is responsible for applying the given changeset.
// An error may return from ApplyChanges if the driver is unable
// to execute a change.
ApplyChanges(context.Context, []schema.Change) error
}
// StateReader wraps the method for reading a database/schema state.
// The types below provides a few builtin options for reading a state
// from a migration directory, a static object (e.g. a parsed file).
//
// In next Go version, the State will be replaced with the following
// union type `interface { Realm | Schema }`.
StateReader interface {
ReadState(ctx context.Context) (*schema.Realm, error)
}
// The StateReaderFunc type is an adapter to allow the use of
// ordinary functions as state readers.
StateReaderFunc func(ctx context.Context) (*schema.Realm, error)
)
// ReadState calls f(ctx).
func (f StateReaderFunc) ReadState(ctx context.Context) (*schema.Realm, error) {
return f(ctx)
}
// ErrNoPlan is returned by Plan when there is no change between the two states.
var ErrNoPlan = errors.New("sql/migrate: no plan for matched states")
// Realm returns a state reader for the static Realm object.
func Realm(r *schema.Realm) StateReader {
return StateReaderFunc(func(context.Context) (*schema.Realm, error) {
return r, nil
})
}
// Schema returns a state reader for the static Schema object.
func Schema(s *schema.Schema) StateReader {
return StateReaderFunc(func(context.Context) (*schema.Realm, error) {
r := &schema.Realm{Schemas: []*schema.Schema{s}}
if s.Realm != nil {
r.Attrs = s.Realm.Attrs
}
s.Realm = r
return r, nil
})
}
type (
// Dir represents a versioned migration directory.
Dir struct {
fs fs.FS
conn Driver
pattern string
templates []struct{ N, T *template.Template }
}
// DirOption allows configuring the Dir
// using functional options.
DirOption func(*Dir) error
)
// NewDir creates a new workspace directory based
// on the given configuration options.
func NewDir(opts ...DirOption) (*Dir, error) {
d := &Dir{
fs: dirFS{
dir: "migrations",
FS: os.DirFS("migrations"),
},
pattern: "*.sql",
}
for _, opt := range opts {
if err := opt(d); err != nil {
return nil, err
}
}
if len(d.templates) == 0 {
d.templates = []struct{ N, T *template.Template }{defaultTemplate}
}
return d, nil
}
type (
// dirFS wraps the os.DirFS with additional writing capabilities.
dirFS struct {
fs.FS
dir string
}
// FileRemoveWriter wraps the WriteFile and RemoveFile methods
// to allow editing the migration directory on development.
FileRemoveWriter interface {
RemoveFile(name string) error
WriteFile(name string, data []byte, perm fs.FileMode) error
}
)
// WriteFile implements the FileWriter interface.
func (d *dirFS) WriteFile(name string, data []byte, perm fs.FileMode) error {
return os.WriteFile(filepath.Join(d.dir, name), data, perm)
}
// RemoveFile implements the FileWriter interface.
func (d *dirFS) RemoveFile(name string) error {
return os.Remove(filepath.Join(d.dir, name))
}
// DirFS configures the FS used by the migration directory.
func DirFS(fs fs.FS) DirOption {
return func(d *Dir) error {
d.fs = fs
return nil
}
}
// DirPath configures the FS used by the migration directory
// to point the given OS directory.
func DirPath(path string) DirOption {
return func(d *Dir) error {
fi, err := os.Stat(path)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("sql/migrate: %q not a dir", path)
}
d.fs = &dirFS{FS: os.DirFS(path), dir: path}
return nil
}
}
// DirGlob configures the glob/pattern for reading
// migration files from the directory. For example:
//
// migrate.NewDir(
// migrate.DirPath("migrations"),
// migrate.DirGlob("*.up.sql"),
// )
//
func DirGlob(pattern string) DirOption {
return func(d *Dir) error {
d.pattern = pattern
return nil
}
}
// DirConn provides a Driver connection to a database. It is usually connected to
// an ephemeral database for emulating migration changes on it and calculating the
// "current state" to be compared with the "desired state".
func DirConn(conn Driver) DirOption {
return func(d *Dir) error {
d.conn = conn
return nil
}
}
var (
// TemplateFuncs defines the global functions available for the templates.
TemplateFuncs = template.FuncMap{
"hasSuffix": strings.HasSuffix,
"timestamp": func() int64 {
return time.Now().Unix()
},
}
defaultTemplate = struct {
N, T *template.Template
}{
N: template.Must(template.New("name").
Funcs(TemplateFuncs).
Parse("{{timestamp}}_{{.Name}}.sql")),
T: template.Must(template.New("name").
Funcs(TemplateFuncs).
Parse(`
{{- range $c := .Changes }}
{{- $cmd := $c.Cmd }}
{{- if not (hasSuffix $c.Cmd ";") }}
{{- $cmd = print $cmd ";" }}
{{- end }}
{{- println $cmd }}
{{- end }}`)),
}
)
// DirTemplates configures template files for writing
// the Plan object to the migration directory.
//
// migrate.NewDir(
// migrate.DirPath("migrations"),
// migrate.DirTemplates("{{timestamp}}{{.Name}}.up.sql", "{{range c := .Changes}}{{println c.Cmd}}{{end}}"),
// )
//
// migrate.NewDir(
// migrate.DirPath("migrations"),
// migrate.DirTemplates(
// "{{timestamp}}{{.Name}}.up.sql", "{{range $c := .Changes}}{{println $c.Cmd}}{{end}}",
// "{{timestamp}}{{.Name}}.down.sql", "{{range $c := .Changes}}{{println $c.Reverse}}{{end}}",
// ),
// )
//
func DirTemplates(nameFileTmpl ...string) DirOption {
return func(d *Dir) error {
if n := len(nameFileTmpl); n == 0 || n%2 == 1 {
return fmt.Errorf("odd or zero argument count")
}
for i := 0; i < len(nameFileTmpl); i += 2 {
if err := d.addTemplate(nameFileTmpl[i], nameFileTmpl[i+1]); err != nil {
return err
}
}
return nil
}
}
// ReadState reads the current database/realm state that is stored in the migration directory.
// The given emulator driver is used for playing all migration files against to it.
func (d *Dir) ReadState(ctx context.Context) (*schema.Realm, error) {
realm, _, err := d.readStateOf(ctx, -1)
return realm, err
}
// Plan calculates the migration Plan required for moving from the directory state to
// the next state (to). A StateReader, can be another directory, static schema elements
// or a Driver connection.
func (d *Dir) Plan(ctx context.Context, name string, to StateReader) (*Plan, error) {
current, err := d.ReadState(ctx)
if err != nil {
return nil, err
}
desired, err := to.ReadState(ctx)
if err != nil {
return nil, err
}
changes, err := d.conn.RealmDiff(current, desired)
if err != nil {
return nil, err
}
if len(changes) == 0 {
return nil, ErrNoPlan
}
return d.conn.PlanChanges(ctx, name, changes)
}
// WritePlan writes the given plan to the directory
// based on the given Write configuration.
func (d *Dir) WritePlan(p *Plan) error {
rw, ok := d.fs.(FileRemoveWriter)
if !ok {
return fmt.Errorf("fs.FS does not support editing: %T", d.fs)
}
for _, t := range d.templates {
var b bytes.Buffer
if err := t.N.Execute(&b, p); err != nil {
return err
}
if b.String() == "" {
return errors.New("file name cannot be empty")
}
name := b.String()
b.Reset()
if err := t.T.Execute(&b, p); err != nil {
return err
}
if err := rw.WriteFile(name, b.Bytes(), 0644); err != nil {
return err
}
}
return nil
}
// Compact compacts the first n migration files into one. If n < 0, all files are selected.
func (d *Dir) Compact(ctx context.Context, name string, n int) error {
rw, ok := d.fs.(FileRemoveWriter)
if !ok {
return fmt.Errorf("fs.FS does not support editing: %T", d.fs)
}
desired, files, err := d.readStateOf(ctx, n)
if err != nil {
return err
}
changes, err := d.conn.RealmDiff(&schema.Realm{}, desired)
if err != nil {
return err
}
if len(changes) == 0 {
return nil
}
for _, c := range changes {
// Add the "IF NOT EXISTS" clause to the schema
// creation as it is usually created manually.
if c, ok := c.(*schema.AddSchema); ok {
c.Extra = append(c.Extra, &schema.IfNotExists{})
}
}
plan, err := d.conn.PlanChanges(ctx, name, changes)
if err != nil {
return err
}
for _, f := range files {
if err := rw.RemoveFile(f); err != nil {
return fmt.Errorf("remove file: %q: %w", f, err)
}
}
return d.WritePlan(plan)
}
// readStateOf of first n files. If n < 0, all files are selected.
func (d *Dir) readStateOf(ctx context.Context, n int) (*schema.Realm, []string, error) {
files, err := fs.Glob(d.fs, d.pattern)
if err != nil {
return nil, nil, err
}
switch {
case n == 0 || n >= len(files):
return nil, nil, fmt.Errorf("sql/migrate: invalid number for selected files: %d", n)
case n > 0:
files = files[:n]
}
// Files are expected to be sorted lexicographically.
sort.Slice(files, func(i, j int) bool {
return files[i] < files[j]
})
for _, f := range files {
buf, err := fs.ReadFile(d.fs, f)
if err != nil {
return nil, nil, fmt.Errorf("sql/migrate: scan migration script %q: %w", f, err)
}
if _, err := d.conn.ExecContext(ctx, string(buf)); err != nil {
return nil, nil, fmt.Errorf("sql/migrate: execute migration script %q: %w", f, err)
}
}
realm, err := d.conn.InspectRealm(ctx, nil)
if err != nil {
return nil, nil, err
}
return realm, files, nil
}
func (d *Dir) addTemplate(nameTmpl, fileTmpl string) error {
nameT, err := template.New("name").Funcs(TemplateFuncs).Parse(nameTmpl)
if err != nil {
return err
}
fileT, err := template.New("file").Funcs(TemplateFuncs).Parse(fileTmpl)
if err != nil {
return err
}
d.templates = append(d.templates, struct{ N, T *template.Template }{N: nameT, T: fileT})
return nil
}