Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse table level foreign key references in create_constraint #640

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/migrations/op_create_constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func TestCreateConstraint(t *testing.T) {
Table: "reports",
Type: "foreign_key",
Columns: []string{"users_id", "users_zip"},
References: &migrations.OpCreateConstraintReferences{
References: &migrations.TableForeignKeyReference{
Table: "users",
Columns: []string{"id", "zip"},
},
Expand Down Expand Up @@ -769,7 +769,7 @@ func TestCreateConstraintValidation(t *testing.T) {
Table: "users",
Columns: []string{"name"},
Type: "foreign_key",
References: &migrations.OpCreateConstraintReferences{
References: &migrations.TableForeignKeyReference{
Table: "missing_table",
Columns: []string{"id"},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/migrations/op_create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ func TestCreateTable(t *testing.T) {
Name: "fk_owners",
Type: migrations.ConstraintTypeForeignKey,
Columns: []string{"owner_id"},
References: &migrations.ConstraintReferences{
References: &migrations.TableForeignKeyReference{
Table: "owners",
Columns: []string{"id"},
OnDelete: migrations.ForeignKeyActionCASCADE,
Expand Down Expand Up @@ -947,7 +947,7 @@ func TestCreateTable(t *testing.T) {
Type: migrations.ConstraintTypeForeignKey,
Columns: []string{"owner_id", "owner_city_id"},
Deferrable: true,
References: &migrations.ConstraintReferences{
References: &migrations.TableForeignKeyReference{
Table: "owners",
Columns: []string{"id", "city"},
OnDelete: migrations.ForeignKeyActionSETDEFAULT,
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_drop_multicolumn_constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func TestDropMultiColumnConstraint(t *testing.T) {
Table: "reports",
Type: migrations.OpCreateConstraintTypeForeignKey,
Columns: []string{"user_id", "user_zip"},
References: &migrations.OpCreateConstraintReferences{
References: &migrations.TableForeignKeyReference{
Table: "users",
Columns: []string{"id", "zip"},
},
Expand Down
82 changes: 41 additions & 41 deletions pkg/migrations/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/sql2pgroll/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func convertAlterTableAddForeignKeyConstraint(stmt *pgq.AlterTableStmt, constrai
Up: migs,
Down: migs,
Name: constraint.GetConname(),
References: &migrations.OpCreateConstraintReferences{
References: &migrations.TableForeignKeyReference{
Columns: foreignColumns,
OnDelete: onDelete,
Table: foreignTable,
Expand Down
12 changes: 6 additions & 6 deletions pkg/sql2pgroll/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func convertConstraint(c *pgq.Constraint) (*migrations.Constraint, error) {
var nullsNotDistinct bool
var checkExpr string
var err error
var references *migrations.ConstraintReferences
var references *migrations.TableForeignKeyReference
var exclude *migrations.ConstraintExclude

columns := make([]string, len(c.Keys))
Expand All @@ -275,14 +275,14 @@ func convertConstraint(c *pgq.Constraint) (*migrations.Constraint, error) {
for i, node := range c.PkAttrs {
referencedColumns[i] = node.GetString_().Sval
}
matchType := migrations.ConstraintReferencesMatchTypeSIMPLE
matchType := migrations.ForeignKeyMatchTypeSIMPLE
switch c.FkMatchtype {
case "p":
matchType = migrations.ConstraintReferencesMatchTypePARTIAL
matchType = migrations.ForeignKeyMatchTypePARTIAL
case "f":
matchType = migrations.ConstraintReferencesMatchTypeFULL
matchType = migrations.ForeignKeyMatchTypeFULL
case "s":
matchType = migrations.ConstraintReferencesMatchTypeSIMPLE
matchType = migrations.ForeignKeyMatchTypeSIMPLE
}
columnsToSet := make([]string, len(c.FkDelSetCols))
onDelete := migrations.ForeignKeyActionNOACTION
Expand All @@ -301,7 +301,7 @@ func convertConstraint(c *pgq.Constraint) (*migrations.Constraint, error) {
columns[i] = node.GetString_().Sval
}

references = &migrations.ConstraintReferences{
references = &migrations.TableForeignKeyReference{
Table: referencedTable,
Columns: referencedColumns,
MatchType: matchType,
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql2pgroll/expect/add_foreign_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func AddForeignKeyOp1WithOnDelete(onDelete migrations.ForeignKeyAction) *migrati
return &migrations.OpCreateConstraint{
Columns: []string{"a", "b"},
Name: "fk_bar_cd",
References: &migrations.OpCreateConstraintReferences{
References: &migrations.TableForeignKeyReference{
Columns: []string{"c", "d"},
OnDelete: onDelete,
Table: "bar",
Expand All @@ -32,7 +32,7 @@ func AddForeignKeyOp1WithOnDelete(onDelete migrations.ForeignKeyAction) *migrati
var AddForeignKeyOp2 = &migrations.OpCreateConstraint{
Columns: []string{"a"},
Name: "fk_bar_c",
References: &migrations.OpCreateConstraintReferences{
References: &migrations.TableForeignKeyReference{
Columns: []string{"c"},
OnDelete: migrations.ForeignKeyActionNOACTION,
Table: "bar",
Expand All @@ -50,7 +50,7 @@ var AddForeignKeyOp2 = &migrations.OpCreateConstraint{
var AddForeignKeyOp3 = &migrations.OpCreateConstraint{
Columns: []string{"a"},
Name: "fk_bar_c",
References: &migrations.OpCreateConstraintReferences{
References: &migrations.TableForeignKeyReference{
Columns: []string{"c"},
OnDelete: migrations.ForeignKeyActionNOACTION,
Table: "schema_a.bar",
Expand Down
20 changes: 10 additions & 10 deletions pkg/sql2pgroll/expect/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,13 +483,13 @@ var CreateTableOp29 = &migrations.OpCreateTable{
Deferrable: false,
InitiallyDeferred: false,
NoInherit: false,
References: &migrations.ConstraintReferences{
References: &migrations.TableForeignKeyReference{
Table: "bar",
Columns: []string{"b"},
OnDelete: migrations.ForeignKeyActionNOACTION,
OnDeleteSetColumns: []string{},
OnUpdate: migrations.ForeignKeyActionNOACTION,
MatchType: migrations.ConstraintReferencesMatchTypeSIMPLE,
MatchType: migrations.ForeignKeyMatchTypeSIMPLE,
},
},
},
Expand All @@ -512,13 +512,13 @@ var CreateTableOp30 = &migrations.OpCreateTable{
Deferrable: false,
InitiallyDeferred: false,
NoInherit: false,
References: &migrations.ConstraintReferences{
References: &migrations.TableForeignKeyReference{
Table: "bar",
Columns: []string{"b"},
OnDelete: migrations.ForeignKeyActionSETNULL,
OnDeleteSetColumns: []string{},
OnUpdate: migrations.ForeignKeyActionCASCADE,
MatchType: migrations.ConstraintReferencesMatchTypeSIMPLE,
MatchType: migrations.ForeignKeyMatchTypeSIMPLE,
},
},
},
Expand Down Expand Up @@ -546,13 +546,13 @@ var CreateTableOp31 = &migrations.OpCreateTable{
Deferrable: false,
InitiallyDeferred: false,
NoInherit: false,
References: &migrations.ConstraintReferences{
References: &migrations.TableForeignKeyReference{
Table: "bar",
Columns: []string{"c", "d"},
OnDelete: migrations.ForeignKeyActionSETNULL,
OnDeleteSetColumns: []string{"b"},
OnUpdate: migrations.ForeignKeyActionNOACTION,
MatchType: migrations.ConstraintReferencesMatchTypeSIMPLE,
MatchType: migrations.ForeignKeyMatchTypeSIMPLE,
},
},
},
Expand Down Expand Up @@ -585,13 +585,13 @@ var CreateTableOp32 = &migrations.OpCreateTable{
Deferrable: false,
InitiallyDeferred: false,
NoInherit: false,
References: &migrations.ConstraintReferences{
References: &migrations.TableForeignKeyReference{
Table: "bar",
Columns: []string{"d", "e", "f"},
OnDelete: migrations.ForeignKeyActionSETNULL,
OnDeleteSetColumns: []string{},
OnUpdate: migrations.ForeignKeyActionCASCADE,
MatchType: migrations.ConstraintReferencesMatchTypeSIMPLE,
MatchType: migrations.ForeignKeyMatchTypeSIMPLE,
},
},
},
Expand Down Expand Up @@ -624,13 +624,13 @@ var CreateTableOp33 = &migrations.OpCreateTable{
Deferrable: false,
InitiallyDeferred: false,
NoInherit: false,
References: &migrations.ConstraintReferences{
References: &migrations.TableForeignKeyReference{
Table: "bar",
Columns: []string{"d", "e", "f"},
OnDelete: migrations.ForeignKeyActionNOACTION,
OnDeleteSetColumns: []string{},
OnUpdate: migrations.ForeignKeyActionNOACTION,
MatchType: migrations.ConstraintReferencesMatchTypeFULL,
MatchType: migrations.ForeignKeyMatchTypeFULL,
},
},
},
Expand Down
Loading