Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(differ): support to sync foreign keys (#3439)

rebelice6aed4441a77f
diff --git a/plugin/parser/differ/pg/differ.go b/plugin/parser/differ/pg/differ.go
index bbaf95fe92..0030cdc68b 100644
--- a/plugin/parser/differ/pg/differ.go
+++ b/plugin/parser/differ/pg/differ.go
@@ -203,7 +203,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
switch item := alterItem.(type) {
case *ast.AddConstraintStmt:
switch item.Constraint.Type {
- case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion:
+ case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion, ast.ConstraintTypeForeign:
if err := oldSchemaMap.addConstraint(i, item); err != nil {
return "", err
}
@@ -248,7 +248,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
switch item := alterItem.(type) {
case *ast.AddConstraintStmt:
switch item.Constraint.Type {
- case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion:
+ case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion, ast.ConstraintTypeForeign:
oldConstraint := oldSchemaMap.getConstraint(item.Table.Schema, item.Table.Name, item.Constraint.Name)
if oldConstraint == nil {
diff.appendAddConstraint(stmt.Table, []*ast.ConstraintDef{item.Constraint})
@@ -485,7 +485,7 @@ func isEqualConstraint(oldConstraint *ast.ConstraintDef, newConstraint *ast.Cons
if newConstraint.Expression.Text() != oldConstraint.Expression.Text() {
return false, nil
}
- case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion:
+ case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion, ast.ConstraintTypeForeign:
// TODO(zp): To make the logic simple now, we just restore the statement, and drop and create the new one if
// there is any difference.
oldAlterTableAddConstraint, err := parser.Deparse(parser.Postgres, parser.DeparseContext{}, oldConstraint)
diff --git a/plugin/parser/differ/pg/test-data/test_differ_constraint.yaml b/plugin/parser/differ/pg/test-data/test_differ_constraint.yaml
index 15d0679a3c..b2ff903e8a 100644
--- a/plugin/parser/differ/pg/test-data/test_differ_constraint.yaml
+++ b/plugin/parser/differ/pg/test-data/test_differ_constraint.yaml
@@ -127,3 +127,26 @@
DROP CONSTRAINT "circles_a_b_excl";
ALTER TABLE "public"."circles"
ADD CONSTRAINT "circles_a_b_excl" EXCLUDE USING gist (a WITH &&, b WITH &&) WHERE (c > 0);
+- oldSchema: |
+ CREATE TABLE public.t1(a int, b int, c int);
+ create table public.t2(a int, b int, c int);
+ create unique index uk_1 on public.t1 (a, b, c);
+ create unique index uk_2 on public.t1 (a, b);
+ alter table public.t2 add constraint fk_1 foreign key (a, b, c) references public.t2(a, b, c);
+ alter table public.t2 add constraint fk_3 foreign key (a, b) references public.t2(a, b);
+ newSchema: |
+ CREATE TABLE public.t1(a int, b int, c int);
+ create table public.t2(a int, b int, c int);
+ create unique index uk_1 on public.t1 (a, b, c);
+ create unique index uk_2 on public.t1 (a, b);
+ alter table public.t2 add constraint fk_2 foreign key (a, b, c) references public.t2(a, b, c);
+ alter table public.t2 add constraint fk_3 foreign key (a, b, c) references public.t2(a, b, c);
+ diff: |
+ ALTER TABLE "public"."t2"
+ DROP CONSTRAINT "fk_3";
+ ALTER TABLE "public"."t2"
+ DROP CONSTRAINT "fk_1";
+ ALTER TABLE "public"."t2"
+ ADD CONSTRAINT "fk_2" FOREIGN KEY ("a", "b", "c") REFERENCES "public"."t2" ("a", "b", "c");
+ ALTER TABLE "public"."t2"
+ ADD CONSTRAINT "fk_3" FOREIGN KEY ("a", "b", "c") REFERENCES "public"."t2" ("a", "b", "c");