Skip to content

Commit 9ebb691

Browse files
authored
Reuse table level foreign key references in create_constraint from create_table (#640)
This PR reuses the same object from constraint definition of `create_table`. Also, match type gets its dedicated definition, as it can be reused in multiple places as well. Extracted from #628
1 parent d141492 commit 9ebb691

File tree

9 files changed

+153
-126
lines changed

9 files changed

+153
-126
lines changed

pkg/migrations/op_create_constraint_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ func TestCreateConstraint(t *testing.T) {
420420
Table: "reports",
421421
Type: "foreign_key",
422422
Columns: []string{"users_id", "users_zip"},
423-
References: &migrations.OpCreateConstraintReferences{
423+
References: &migrations.TableForeignKeyReference{
424424
Table: "users",
425425
Columns: []string{"id", "zip"},
426426
},
@@ -769,7 +769,7 @@ func TestCreateConstraintValidation(t *testing.T) {
769769
Table: "users",
770770
Columns: []string{"name"},
771771
Type: "foreign_key",
772-
References: &migrations.OpCreateConstraintReferences{
772+
References: &migrations.TableForeignKeyReference{
773773
Table: "missing_table",
774774
Columns: []string{"id"},
775775
},

pkg/migrations/op_create_table_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ func TestCreateTable(t *testing.T) {
817817
Name: "fk_owners",
818818
Type: migrations.ConstraintTypeForeignKey,
819819
Columns: []string{"owner_id"},
820-
References: &migrations.ConstraintReferences{
820+
References: &migrations.TableForeignKeyReference{
821821
Table: "owners",
822822
Columns: []string{"id"},
823823
OnDelete: migrations.ForeignKeyActionCASCADE,
@@ -947,7 +947,7 @@ func TestCreateTable(t *testing.T) {
947947
Type: migrations.ConstraintTypeForeignKey,
948948
Columns: []string{"owner_id", "owner_city_id"},
949949
Deferrable: true,
950-
References: &migrations.ConstraintReferences{
950+
References: &migrations.TableForeignKeyReference{
951951
Table: "owners",
952952
Columns: []string{"id", "city"},
953953
OnDelete: migrations.ForeignKeyActionSETDEFAULT,

pkg/migrations/op_drop_multicolumn_constraint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func TestDropMultiColumnConstraint(t *testing.T) {
319319
Table: "reports",
320320
Type: migrations.OpCreateConstraintTypeForeignKey,
321321
Columns: []string{"user_id", "user_zip"},
322-
References: &migrations.OpCreateConstraintReferences{
322+
References: &migrations.TableForeignKeyReference{
323323
Table: "users",
324324
Columns: []string{"id", "zip"},
325325
},

pkg/migrations/types.go

Lines changed: 41 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sql2pgroll/alter_table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func convertAlterTableAddForeignKeyConstraint(stmt *pgq.AlterTableStmt, constrai
213213
Up: migs,
214214
Down: migs,
215215
Name: constraint.GetConname(),
216-
References: &migrations.OpCreateConstraintReferences{
216+
References: &migrations.TableForeignKeyReference{
217217
Columns: foreignColumns,
218218
OnDelete: onDelete,
219219
Table: foreignTable,

pkg/sql2pgroll/create_table.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func convertConstraint(c *pgq.Constraint) (*migrations.Constraint, error) {
248248
var nullsNotDistinct bool
249249
var checkExpr string
250250
var err error
251-
var references *migrations.ConstraintReferences
251+
var references *migrations.TableForeignKeyReference
252252
var exclude *migrations.ConstraintExclude
253253

254254
columns := make([]string, len(c.Keys))
@@ -275,14 +275,14 @@ func convertConstraint(c *pgq.Constraint) (*migrations.Constraint, error) {
275275
for i, node := range c.PkAttrs {
276276
referencedColumns[i] = node.GetString_().Sval
277277
}
278-
matchType := migrations.ConstraintReferencesMatchTypeSIMPLE
278+
matchType := migrations.ForeignKeyMatchTypeSIMPLE
279279
switch c.FkMatchtype {
280280
case "p":
281-
matchType = migrations.ConstraintReferencesMatchTypePARTIAL
281+
matchType = migrations.ForeignKeyMatchTypePARTIAL
282282
case "f":
283-
matchType = migrations.ConstraintReferencesMatchTypeFULL
283+
matchType = migrations.ForeignKeyMatchTypeFULL
284284
case "s":
285-
matchType = migrations.ConstraintReferencesMatchTypeSIMPLE
285+
matchType = migrations.ForeignKeyMatchTypeSIMPLE
286286
}
287287
columnsToSet := make([]string, len(c.FkDelSetCols))
288288
onDelete := migrations.ForeignKeyActionNOACTION
@@ -301,7 +301,7 @@ func convertConstraint(c *pgq.Constraint) (*migrations.Constraint, error) {
301301
columns[i] = node.GetString_().Sval
302302
}
303303

304-
references = &migrations.ConstraintReferences{
304+
references = &migrations.TableForeignKeyReference{
305305
Table: referencedTable,
306306
Columns: referencedColumns,
307307
MatchType: matchType,

pkg/sql2pgroll/expect/add_foreign_key.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func AddForeignKeyOp1WithOnDelete(onDelete migrations.ForeignKeyAction) *migrati
1111
return &migrations.OpCreateConstraint{
1212
Columns: []string{"a", "b"},
1313
Name: "fk_bar_cd",
14-
References: &migrations.OpCreateConstraintReferences{
14+
References: &migrations.TableForeignKeyReference{
1515
Columns: []string{"c", "d"},
1616
OnDelete: onDelete,
1717
Table: "bar",
@@ -32,7 +32,7 @@ func AddForeignKeyOp1WithOnDelete(onDelete migrations.ForeignKeyAction) *migrati
3232
var AddForeignKeyOp2 = &migrations.OpCreateConstraint{
3333
Columns: []string{"a"},
3434
Name: "fk_bar_c",
35-
References: &migrations.OpCreateConstraintReferences{
35+
References: &migrations.TableForeignKeyReference{
3636
Columns: []string{"c"},
3737
OnDelete: migrations.ForeignKeyActionNOACTION,
3838
Table: "bar",
@@ -50,7 +50,7 @@ var AddForeignKeyOp2 = &migrations.OpCreateConstraint{
5050
var AddForeignKeyOp3 = &migrations.OpCreateConstraint{
5151
Columns: []string{"a"},
5252
Name: "fk_bar_c",
53-
References: &migrations.OpCreateConstraintReferences{
53+
References: &migrations.TableForeignKeyReference{
5454
Columns: []string{"c"},
5555
OnDelete: migrations.ForeignKeyActionNOACTION,
5656
Table: "schema_a.bar",

pkg/sql2pgroll/expect/create_table.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,13 @@ var CreateTableOp29 = &migrations.OpCreateTable{
483483
Deferrable: false,
484484
InitiallyDeferred: false,
485485
NoInherit: false,
486-
References: &migrations.ConstraintReferences{
486+
References: &migrations.TableForeignKeyReference{
487487
Table: "bar",
488488
Columns: []string{"b"},
489489
OnDelete: migrations.ForeignKeyActionNOACTION,
490490
OnDeleteSetColumns: []string{},
491491
OnUpdate: migrations.ForeignKeyActionNOACTION,
492-
MatchType: migrations.ConstraintReferencesMatchTypeSIMPLE,
492+
MatchType: migrations.ForeignKeyMatchTypeSIMPLE,
493493
},
494494
},
495495
},
@@ -512,13 +512,13 @@ var CreateTableOp30 = &migrations.OpCreateTable{
512512
Deferrable: false,
513513
InitiallyDeferred: false,
514514
NoInherit: false,
515-
References: &migrations.ConstraintReferences{
515+
References: &migrations.TableForeignKeyReference{
516516
Table: "bar",
517517
Columns: []string{"b"},
518518
OnDelete: migrations.ForeignKeyActionSETNULL,
519519
OnDeleteSetColumns: []string{},
520520
OnUpdate: migrations.ForeignKeyActionCASCADE,
521-
MatchType: migrations.ConstraintReferencesMatchTypeSIMPLE,
521+
MatchType: migrations.ForeignKeyMatchTypeSIMPLE,
522522
},
523523
},
524524
},
@@ -546,13 +546,13 @@ var CreateTableOp31 = &migrations.OpCreateTable{
546546
Deferrable: false,
547547
InitiallyDeferred: false,
548548
NoInherit: false,
549-
References: &migrations.ConstraintReferences{
549+
References: &migrations.TableForeignKeyReference{
550550
Table: "bar",
551551
Columns: []string{"c", "d"},
552552
OnDelete: migrations.ForeignKeyActionSETNULL,
553553
OnDeleteSetColumns: []string{"b"},
554554
OnUpdate: migrations.ForeignKeyActionNOACTION,
555-
MatchType: migrations.ConstraintReferencesMatchTypeSIMPLE,
555+
MatchType: migrations.ForeignKeyMatchTypeSIMPLE,
556556
},
557557
},
558558
},
@@ -585,13 +585,13 @@ var CreateTableOp32 = &migrations.OpCreateTable{
585585
Deferrable: false,
586586
InitiallyDeferred: false,
587587
NoInherit: false,
588-
References: &migrations.ConstraintReferences{
588+
References: &migrations.TableForeignKeyReference{
589589
Table: "bar",
590590
Columns: []string{"d", "e", "f"},
591591
OnDelete: migrations.ForeignKeyActionSETNULL,
592592
OnDeleteSetColumns: []string{},
593593
OnUpdate: migrations.ForeignKeyActionCASCADE,
594-
MatchType: migrations.ConstraintReferencesMatchTypeSIMPLE,
594+
MatchType: migrations.ForeignKeyMatchTypeSIMPLE,
595595
},
596596
},
597597
},
@@ -624,13 +624,13 @@ var CreateTableOp33 = &migrations.OpCreateTable{
624624
Deferrable: false,
625625
InitiallyDeferred: false,
626626
NoInherit: false,
627-
References: &migrations.ConstraintReferences{
627+
References: &migrations.TableForeignKeyReference{
628628
Table: "bar",
629629
Columns: []string{"d", "e", "f"},
630630
OnDelete: migrations.ForeignKeyActionNOACTION,
631631
OnDeleteSetColumns: []string{},
632632
OnUpdate: migrations.ForeignKeyActionNOACTION,
633-
MatchType: migrations.ConstraintReferencesMatchTypeFULL,
633+
MatchType: migrations.ForeignKeyMatchTypeFULL,
634634
},
635635
},
636636
},

0 commit comments

Comments
 (0)