/
migrate.go
609 lines (575 loc) · 18 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// Copyright 2019-present Facebook Inc. 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 schema
import (
"context"
"crypto/md5"
"fmt"
"math"
"sort"
"github.com/facebookincubator/ent/dialect"
"github.com/facebookincubator/ent/dialect/sql"
"github.com/facebookincubator/ent/schema/field"
)
const (
// TypeTable defines the table name holding the type information.
TypeTable = "ent_types"
// MaxTypes defines the max number of types can be created when
// defining universal ids. The left 16-bits are reserved.
MaxTypes = math.MaxUint16
)
// MigrateOption allows for managing schema configuration using functional options.
type MigrateOption func(m *Migrate)
// WithGlobalUniqueID sets the universal ids options to the migration.
// Defaults to false.
func WithGlobalUniqueID(b bool) MigrateOption {
return func(m *Migrate) {
m.universalID = b
}
}
// WithDropColumn sets the columns dropping option to the migration.
// Defaults to false.
func WithDropColumn(b bool) MigrateOption {
return func(m *Migrate) {
m.dropColumns = b
}
}
// WithDropIndex sets the indexes dropping option to the migration.
// Defaults to false.
func WithDropIndex(b bool) MigrateOption {
return func(m *Migrate) {
m.dropIndexes = b
}
}
// WithFixture sets the foreign-key renaming option to the migration when upgrading
// ent from v0.1.0 (issue-#285). Defaults to true.
func WithFixture(b bool) MigrateOption {
return func(m *Migrate) {
m.withFixture = b
}
}
// Migrate runs the migrations logic for the SQL dialects.
type Migrate struct {
sqlDialect
universalID bool // global unique ids.
dropColumns bool // drop deleted columns.
dropIndexes bool // drop deleted indexes.
withFixture bool // with fks rename fixture.
typeRanges []string // types order by their range.
}
// NewMigrate create a migration structure for the given SQL driver.
func NewMigrate(d dialect.Driver, opts ...MigrateOption) (*Migrate, error) {
m := &Migrate{withFixture: true}
switch d.Dialect() {
case dialect.MySQL:
m.sqlDialect = &MySQL{Driver: d}
case dialect.SQLite:
m.sqlDialect = &SQLite{Driver: d}
case dialect.Postgres:
m.sqlDialect = &Postgres{Driver: d}
default:
return nil, fmt.Errorf("sql/schema: unsupported dialect %q", d.Dialect())
}
for _, opt := range opts {
opt(m)
}
return m, nil
}
// Create creates all schema resources in the database. It works in an "append-only"
// mode, which means, it only create tables, append column to tables or modifying column type.
//
// Column can be modified by turning into a NULL from NOT NULL, or having a type conversion not
// resulting data altering. From example, changing varchar(255) to varchar(120) is invalid, but
// changing varchar(120) to varchar(255) is valid. For more info, see the convert function below.
//
// Note that SQLite dialect does not support (this moment) the "append-only" mode describe above,
// since it's used only for testing.
func (m *Migrate) Create(ctx context.Context, tables ...*Table) error {
tx, err := m.Tx(ctx)
if err != nil {
return err
}
if err := m.init(ctx, tx); err != nil {
return rollback(tx, err)
}
if m.universalID {
if err := m.types(ctx, tx); err != nil {
return rollback(tx, err)
}
}
if err := m.create(ctx, tx, tables...); err != nil {
return rollback(tx, err)
}
return tx.Commit()
}
func (m *Migrate) create(ctx context.Context, tx dialect.Tx, tables ...*Table) error {
for _, t := range tables {
m.setupTable(t)
switch exist, err := m.tableExist(ctx, tx, t.Name); {
case err != nil:
return err
case exist:
curr, err := m.table(ctx, tx, t.Name)
if err != nil {
return err
}
if err := m.verify(ctx, tx, curr); err != nil {
return err
}
if err := m.fixture(ctx, tx, curr, t); err != nil {
return err
}
change, err := m.changeSet(curr, t)
if err != nil {
return err
}
if err := m.apply(ctx, tx, t.Name, change); err != nil {
return err
}
default: // !exist
query, args := m.tBuilder(t).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("create table %q: %v", t.Name, err)
}
// If global unique identifier is enabled and it's not
// a relation table, allocate a range for the table pk.
if m.universalID && len(t.PrimaryKey) == 1 {
if err := m.allocPKRange(ctx, tx, t); err != nil {
return err
}
}
// indexes.
for _, idx := range t.Indexes {
query, args := m.addIndex(idx, t.Name).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("create index %q: %v", idx.Name, err)
}
}
}
}
// Create foreign keys after tables were created/altered,
// because circular foreign-key constraints are possible.
for _, t := range tables {
if len(t.ForeignKeys) == 0 {
continue
}
fks := make([]*ForeignKey, 0, len(t.ForeignKeys))
for _, fk := range t.ForeignKeys {
exist, err := m.fkExist(ctx, tx, fk.Symbol)
if err != nil {
return err
}
if !exist {
fks = append(fks, fk)
}
}
if len(fks) == 0 {
continue
}
b := sql.Dialect(m.Dialect()).AlterTable(t.Name)
for _, fk := range fks {
b.AddForeignKey(fk.DSL())
}
query, args := b.Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("create foreign keys for %q: %v", t.Name, err)
}
}
return nil
}
// apply applies changes on the given table.
func (m *Migrate) apply(ctx context.Context, tx dialect.Tx, table string, change *changes) error {
// Constraints should be dropped before dropping columns, because if a column
// is a part of multi-column constraints (like, unique index), ALTER TABLE
// might fail if the intermediate state violates the constraints.
if m.dropIndexes {
if pr, ok := m.sqlDialect.(preparer); ok {
if err := pr.prepare(ctx, tx, change, table); err != nil {
return err
}
}
for _, idx := range change.index.drop {
if err := m.dropIndex(ctx, tx, idx, table); err != nil {
return fmt.Errorf("drop index of table %q: %v", table, err)
}
}
}
var drop []*Column
if m.dropColumns {
drop = change.column.drop
}
queries := m.alterColumns(table, change.column.add, change.column.modify, drop)
// If there's actual action to execute on ALTER TABLE.
for i := range queries {
query, args := queries[i].Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("alter table %q: %v", table, err)
}
}
for _, idx := range change.index.add {
query, args := m.addIndex(idx, table).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("create index %q: %v", table, err)
}
}
return nil
}
// changes to apply on existing table.
type changes struct {
// column changes.
column struct {
add []*Column
drop []*Column
modify []*Column
}
// index changes.
index struct {
add Indexes
drop Indexes
}
}
// dropColumn returns the dropped column by name (if any).
func (c *changes) dropColumn(name string) (*Column, bool) {
for _, col := range c.column.drop {
if col.Name == name {
return col, true
}
}
return nil, false
}
// changeSet returns a changes object to be applied on existing table.
// It fails if one of the changes is invalid.
func (m *Migrate) changeSet(curr, new *Table) (*changes, error) {
change := &changes{}
// pks.
if len(curr.PrimaryKey) != len(new.PrimaryKey) {
return nil, fmt.Errorf("cannot change primary key for table: %q", curr.Name)
}
sort.Slice(new.PrimaryKey, func(i, j int) bool { return new.PrimaryKey[i].Name < new.PrimaryKey[j].Name })
sort.Slice(curr.PrimaryKey, func(i, j int) bool { return curr.PrimaryKey[i].Name < curr.PrimaryKey[j].Name })
for i := range curr.PrimaryKey {
if curr.PrimaryKey[i].Name != new.PrimaryKey[i].Name {
return nil, fmt.Errorf("cannot change primary key for table: %q", curr.Name)
}
}
// Add or modify columns.
for _, c1 := range new.Columns {
// Ignore primary keys.
if c1.PrimaryKey() {
continue
}
switch c2, ok := curr.column(c1.Name); {
case !ok:
change.column.add = append(change.column.add, c1)
// Modify a non-unique column to unique.
case c1.Unique && !c2.Unique:
change.index.add.append(&Index{
Name: c1.Name,
Unique: true,
Columns: []*Column{c1},
columns: []string{c1.Name},
})
// Modify a unique column to non-unique.
case !c1.Unique && c2.Unique:
idx, ok := curr.index(c2.Name)
if !ok {
return nil, fmt.Errorf("missing index to drop for column %q", c2.Name)
}
change.index.drop.append(idx)
// Extending column types.
case m.cType(c1) != m.cType(c2):
if !c2.ConvertibleTo(c1) {
return nil, fmt.Errorf("changing column type for %q is invalid (%s != %s)", c1.Name, m.cType(c1), m.cType(c2))
}
fallthrough
// Change nullability of a column.
case c1.Nullable != c2.Nullable:
change.column.modify = append(change.column.modify, c1)
}
}
// Drop columns.
for _, c1 := range curr.Columns {
// If a column was dropped, multi-columns indexes that are associated with this column will
// no longer behave the same. Therefore, these indexes should be dropped too. There's no need
// to do it explicitly (here), because entc will remove them from the schema specification,
// and they will be dropped in the block below.
if _, ok := new.column(c1.Name); !ok {
change.column.drop = append(change.column.drop, c1)
}
}
// Add or modify indexes.
for _, idx1 := range new.Indexes {
switch idx2, ok := curr.index(idx1.Name); {
case !ok:
change.index.add.append(idx1)
// Changing index cardinality require drop and create.
case idx1.Unique != idx2.Unique:
change.index.drop.append(idx2)
change.index.add.append(idx1)
}
}
// Drop indexes.
for _, idx := range curr.Indexes {
_, ok1 := new.fk(idx.Name)
_, ok2 := new.index(idx.Name)
if !ok1 && !ok2 {
change.index.drop.append(idx)
}
}
return change, nil
}
// fixture is a special migration code for renaming foreign-key columns (issue-#285).
func (m *Migrate) fixture(ctx context.Context, tx dialect.Tx, curr, new *Table) error {
d, ok := m.sqlDialect.(fkRenamer)
if !m.withFixture || !ok {
return nil
}
rename := make(map[string]*Index)
for _, fk := range new.ForeignKeys {
ok, err := m.fkExist(ctx, tx, fk.Symbol)
if err != nil {
return fmt.Errorf("checking foreign-key existence %q: %v", fk.Symbol, err)
}
if !ok {
continue
}
column, err := m.fkColumn(ctx, tx, fk)
if err != nil {
return err
}
newcol := fk.Columns[0]
if column == newcol.Name {
continue
}
query, args := d.renameColumn(curr, &Column{Name: column}, newcol).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("rename column %q: %v", column, err)
}
prev, ok := curr.column(column)
if !ok {
continue
}
// Find all indexes that ~maybe need to be renamed.
for _, idx := range prev.indexes {
switch _, ok := new.index(idx.Name); {
// Ignore indexes that exist in the schema, PKs.
case ok || idx.primary:
// Index that was created implicitly for a unique
// column needs to be renamed to the column name.
case d.isImplicitIndex(idx, prev):
idx2 := &Index{Name: newcol.Name, Unique: true, Columns: []*Column{newcol}}
query, args := d.renameIndex(curr, idx, idx2).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("rename index %q: %v", prev.Name, err)
}
idx.Name = idx2.Name
default:
rename[idx.Name] = idx
}
}
// Update the name of the loaded column, so `changeSet` won't create it.
prev.Name = newcol.Name
}
// Go over the indexes that need to be renamed
// and find their ~identical in the new schema.
for _, idx := range rename {
Find:
// Find its ~identical in the new schema, and rename it
// if it doesn't exist.
for _, idx2 := range new.Indexes {
if _, ok := curr.index(idx2.Name); ok {
continue
}
if idx.sameAs(idx2) {
query, args := d.renameIndex(curr, idx, idx2).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("rename index %q: %v", idx.Name, err)
}
idx.Name = idx2.Name
break Find
}
}
}
return nil
}
// verify verifies that the auto-increment counter is correct for table with universal-id support.
func (m *Migrate) verify(ctx context.Context, tx dialect.Tx, t *Table) error {
vr, ok := m.sqlDialect.(verifyRanger)
if !ok || !m.universalID {
return nil
}
id := indexOf(m.typeRanges, t.Name)
if id == -1 {
return nil
}
return vr.verifyRange(ctx, tx, t, id<<32)
}
// types loads the type list from the database.
// If the table does not create, it will create one.
func (m *Migrate) types(ctx context.Context, tx dialect.Tx) error {
exists, err := m.tableExist(ctx, tx, TypeTable)
if err != nil {
return err
}
if !exists {
t := NewTable(TypeTable).
AddPrimary(&Column{Name: "id", Type: field.TypeInt, Increment: true}).
AddColumn(&Column{Name: "type", Type: field.TypeString, Unique: true})
query, args := m.tBuilder(t).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("create types table: %v", err)
}
return nil
}
rows := &sql.Rows{}
query, args := sql.Dialect(m.Dialect()).
Select("type").From(sql.Table(TypeTable)).OrderBy(sql.Asc("id")).Query()
if err := tx.Query(ctx, query, args, rows); err != nil {
return fmt.Errorf("query types table: %v", err)
}
defer rows.Close()
return sql.ScanSlice(rows, &m.typeRanges)
}
func (m *Migrate) allocPKRange(ctx context.Context, tx dialect.Tx, t *Table) error {
id := indexOf(m.typeRanges, t.Name)
// If the table re-created, re-use its range from
// the past. otherwise, allocate a new id-range.
if id == -1 {
if len(m.typeRanges) > MaxTypes {
return fmt.Errorf("max number of types exceeded: %d", MaxTypes)
}
query, args := sql.Dialect(m.Dialect()).
Insert(TypeTable).Columns("type").Values(t.Name).Query()
if err := tx.Exec(ctx, query, args, nil); err != nil {
return fmt.Errorf("insert into type: %v", err)
}
id = len(m.typeRanges)
m.typeRanges = append(m.typeRanges, t.Name)
}
// Set the id offset for table.
return m.setRange(ctx, tx, t, id<<32)
}
// fkColumn returns the column name of a foreign-key.
func (m *Migrate) fkColumn(ctx context.Context, tx dialect.Tx, fk *ForeignKey) (string, error) {
t1 := sql.Table("INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS t1").Unquote().As("t1")
t2 := sql.Table("INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t2").Unquote().As("t2")
query, args := sql.Dialect(m.Dialect()).
Select("column_name").
From(t1).
Join(t2).
On(t1.C("constraint_name"), t2.C("constraint_name")).
Where(sql.And(
sql.EQ(t2.C("constraint_type"), sql.Raw("'FOREIGN KEY'")),
sql.EQ(t2.C("table_schema"), m.sqlDialect.(fkRenamer).tableSchema()),
sql.EQ(t1.C("table_schema"), m.sqlDialect.(fkRenamer).tableSchema()),
sql.EQ(t2.C("constraint_name"), fk.Symbol),
)).
Query()
rows := &sql.Rows{}
if err := tx.Query(ctx, query, args, rows); err != nil {
return "", fmt.Errorf("reading foreign-key %q column: %v", fk.Symbol, err)
}
defer rows.Close()
column, err := sql.ScanString(rows)
if err != nil {
return "", fmt.Errorf("scanning foreign-key %q column: %v", fk.Symbol, err)
}
return column, nil
}
// setup ensures the table is configured properly, like table columns
// are linked to their indexes, and PKs columns are defined.
func (m *Migrate) setupTable(t *Table) {
if t.columns == nil {
t.columns = make(map[string]*Column, len(t.Columns))
}
for _, c := range t.Columns {
t.columns[c.Name] = c
}
for _, idx := range t.Indexes {
idx.Name = m.symbol(idx.Name)
for _, c := range idx.Columns {
c.indexes.append(idx)
}
}
for _, pk := range t.PrimaryKey {
c := t.columns[pk.Name]
c.Key = PrimaryKey
pk.Key = PrimaryKey
}
for _, fk := range t.ForeignKeys {
fk.Symbol = m.symbol(fk.Symbol)
for i := range fk.Columns {
fk.Columns[i].foreign = fk
}
}
}
// symbol makes sure the symbol length is not longer than the maxlength in the dialect.
func (m *Migrate) symbol(name string) string {
size := 64
if m.Dialect() == dialect.Postgres {
size = 63
}
if len(name) <= size {
return name
}
return fmt.Sprintf("%s_%x", name[:size-33], md5.Sum([]byte(name)))
}
// rollback calls to tx.Rollback and wraps the given error with the rollback error if occurred.
func rollback(tx dialect.Tx, err error) error {
err = fmt.Errorf("sql/schema: %v", err)
if rerr := tx.Rollback(); rerr != nil {
err = fmt.Errorf("%s: %v", err.Error(), rerr)
}
return err
}
// exist checks if the given COUNT query returns a value >= 1.
func exist(ctx context.Context, tx dialect.Tx, query string, args ...interface{}) (bool, error) {
rows := &sql.Rows{}
if err := tx.Query(ctx, query, args, rows); err != nil {
return false, fmt.Errorf("reading schema information %v", err)
}
defer rows.Close()
n, err := sql.ScanInt(rows)
if err != nil {
return false, err
}
return n > 0, nil
}
func indexOf(a []string, s string) int {
for i := range a {
if a[i] == s {
return i
}
}
return -1
}
type sqlDialect interface {
dialect.Driver
init(context.Context, dialect.Tx) error
table(context.Context, dialect.Tx, string) (*Table, error)
tableExist(context.Context, dialect.Tx, string) (bool, error)
fkExist(context.Context, dialect.Tx, string) (bool, error)
setRange(context.Context, dialect.Tx, *Table, int) error
dropIndex(context.Context, dialect.Tx, *Index, string) error
// table, column and index builder per dialect.
cType(*Column) string
tBuilder(*Table) *sql.TableBuilder
addIndex(*Index, string) *sql.IndexBuilder
alterColumns(table string, add, modify, drop []*Column) sql.Queries
}
type preparer interface {
prepare(context.Context, dialect.Tx, *changes, string) error
}
// fkRenamer is used by the fixture migration (to solve #285),
// and it's implemented by the different dialects for renaming FKs.
type fkRenamer interface {
tableSchema() sql.Querier
isImplicitIndex(*Index, *Column) bool
renameIndex(*Table, *Index, *Index) sql.Querier
renameColumn(*Table, *Column, *Column) sql.Querier
}
// verifyRanger wraps the method for verifying global-id range correctness.
type verifyRanger interface {
verifyRange(context.Context, dialect.Tx, *Table, int) error
}