-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert ADD CONSTRAINT SQL into OpCreateConstraint operations for for…
…eign keys (#531) Supports translating statements in the following forms: ```sql ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE NO ACTION ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE RESTRICT ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE SET DEFAULT ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE SET NULL ALTER TABLE foo ADD CONSTRAINT fk_bar_c FOREIGN KEY (a) REFERENCES bar (c) ALTER TABLE schema.foo ADD CONSTRAINT fk_bar_c FOREIGN KEY (a) REFERENCES schema.bar (c) ``` The following fall back to raw SQL: ```sql ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE RESTRICT; ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE CASCADE; ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE SET NULL; ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE SET DEFAULT; ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) MATCH FULL; ``` Part of #504
- Loading branch information
Showing
3 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package expect | ||
|
||
import ( | ||
"github.com/xataio/pgroll/pkg/migrations" | ||
"github.com/xataio/pgroll/pkg/sql2pgroll" | ||
) | ||
|
||
func AddForeignKeyOp1WithOnDelete(onDelete migrations.ForeignKeyReferenceOnDelete) *migrations.OpCreateConstraint { | ||
return &migrations.OpCreateConstraint{ | ||
Columns: []string{"a", "b"}, | ||
Name: "fk_bar_cd", | ||
References: &migrations.OpCreateConstraintReferences{ | ||
Columns: []string{"c", "d"}, | ||
OnDelete: onDelete, | ||
Table: "bar", | ||
}, | ||
Table: "foo", | ||
Type: migrations.OpCreateConstraintTypeForeignKey, | ||
Up: map[string]string{ | ||
"a": sql2pgroll.PlaceHolderSQL, | ||
"b": sql2pgroll.PlaceHolderSQL, | ||
}, | ||
Down: map[string]string{ | ||
"a": sql2pgroll.PlaceHolderSQL, | ||
"b": sql2pgroll.PlaceHolderSQL, | ||
}, | ||
} | ||
} | ||
|
||
var AddForeignKeyOp2 = &migrations.OpCreateConstraint{ | ||
Columns: []string{"a"}, | ||
Name: "fk_bar_c", | ||
References: &migrations.OpCreateConstraintReferences{ | ||
Columns: []string{"c"}, | ||
OnDelete: migrations.ForeignKeyReferenceOnDeleteNOACTION, | ||
Table: "bar", | ||
}, | ||
Table: "foo", | ||
Type: migrations.OpCreateConstraintTypeForeignKey, | ||
Up: map[string]string{ | ||
"a": sql2pgroll.PlaceHolderSQL, | ||
}, | ||
Down: map[string]string{ | ||
"a": sql2pgroll.PlaceHolderSQL, | ||
}, | ||
} | ||
|
||
var AddForeignKeyOp3 = &migrations.OpCreateConstraint{ | ||
Columns: []string{"a"}, | ||
Name: "fk_bar_c", | ||
References: &migrations.OpCreateConstraintReferences{ | ||
Columns: []string{"c"}, | ||
OnDelete: migrations.ForeignKeyReferenceOnDeleteNOACTION, | ||
Table: "schema_a.bar", | ||
}, | ||
Table: "schema_a.foo", | ||
Type: migrations.OpCreateConstraintTypeForeignKey, | ||
Up: map[string]string{ | ||
"a": sql2pgroll.PlaceHolderSQL, | ||
}, | ||
Down: map[string]string{ | ||
"a": sql2pgroll.PlaceHolderSQL, | ||
}, | ||
} |