Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(parser): support to sync CHECK constraints (#3323)
rebelice66bb76edce42
diff --git a/plugin/parser/differ/pg/differ.go b/plugin/parser/differ/pg/differ.goindex ac3645ece0..5749be0b2a 100644--- a/plugin/parser/differ/pg/differ.go+++ b/plugin/parser/differ/pg/differ.go@@ -156,6 +156,9 @@ func (diff *diffNode) dropObject(oldSchemaMap schemaMap) error {func (diff *diffNode) modifyTable(oldTable *ast.CreateTableStmt, newTable *ast.CreateTableStmt) error {tableName := oldTable.Name+ alterTableStmt := &ast.AlterTableStmt{+ Table: tableName,+ }// Modify table for columns.oldColumnMap := make(map[string]*ast.ColumnDef)@@ -163,9 +166,6 @@ func (diff *diffNode) modifyTable(oldTable *ast.CreateTableStmt, newTable *ast.ColdColumnMap[column.ColumnName] = column}- alterTableStmt := &ast.AlterTableStmt{- Table: tableName,- }for _, newColumn := range newTable.ColumnList {oldColumn, exists := oldColumnMap[newColumn.ColumnName]// Add the new column.@@ -192,6 +192,36 @@ func (diff *diffNode) modifyTable(oldTable *ast.CreateTableStmt, newTable *ast.C}}+ // Modify table for constraints.+ oldConstraintMap := make(map[string]*ast.ConstraintDef)+ for _, constraint := range oldTable.ConstraintList {+ oldConstraintMap[constraint.Name] = constraint+ }+ for _, newConstraint := range newTable.ConstraintList {+ oldConstraint, exists := oldConstraintMap[newConstraint.Name]+ // Add the new constraint.+ if !exists {+ alterTableStmt.AlterItemList = append(alterTableStmt.AlterItemList, &ast.AddConstraintStmt{+ Table: tableName,+ Constraint: newConstraint,+ })+ continue+ }+ if err := diff.modifyConstraint(alterTableStmt, oldConstraint, newConstraint); err != nil {+ return err+ }+ delete(oldConstraintMap, oldConstraint.Name)+ }++ for _, oldConstraint := range oldTable.ConstraintList {+ if _, exists := oldConstraintMap[oldConstraint.Name]; exists {+ alterTableStmt.AlterItemList = append(alterTableStmt.AlterItemList, &ast.DropConstraintStmt{+ Table: alterTableStmt.Table,+ ConstraintName: oldConstraint.Name,+ })+ }+ }+if len(alterTableStmt.AlterItemList) > 0 {diff.modifyTableList = append(diff.modifyTableList, alterTableStmt)}@@ -199,6 +229,40 @@ func (diff *diffNode) modifyTable(oldTable *ast.CreateTableStmt, newTable *ast.Creturn nil}+func (*diffNode) modifyConstraint(alterTableStmt *ast.AlterTableStmt, oldConstraint *ast.ConstraintDef, newConstraint *ast.ConstraintDef) error {+ constraintName := oldConstraint.Name++ if oldConstraint.Type != newConstraint.Type {+ alterTableStmt.AlterItemList = append(alterTableStmt.AlterItemList, &ast.DropConstraintStmt{+ Table: alterTableStmt.Table,+ ConstraintName: constraintName,+ })+ alterTableStmt.AlterItemList = append(alterTableStmt.AlterItemList, &ast.AddConstraintStmt{+ Table: alterTableStmt.Table,+ Constraint: newConstraint,+ })+ return nil+ }++ switch newConstraint.Type {+ case ast.ConstraintTypeCheck:+ if newConstraint.Expression.Text() != oldConstraint.Expression.Text() {+ alterTableStmt.AlterItemList = append(alterTableStmt.AlterItemList, &ast.DropConstraintStmt{+ Table: alterTableStmt.Table,+ ConstraintName: constraintName,+ })+ alterTableStmt.AlterItemList = append(alterTableStmt.AlterItemList, &ast.AddConstraintStmt{+ Table: alterTableStmt.Table,+ Constraint: newConstraint,+ })+ return nil+ }+ default:+ return errors.Errorf("Unsupported table constraint type: %d for modifyConstraint", newConstraint.Type)+ }+ return nil+}+func (*diffNode) modifyColumn(alterTableStmt *ast.AlterTableStmt, oldColumn *ast.ColumnDef, newColumn *ast.ColumnDef) error {columnName := oldColumn.ColumnName// compare the data typediff --git a/plugin/parser/differ/pg/test-data/test_differ_data.yaml b/plugin/parser/differ/pg/test-data/test_differ_data.yamlindex d9d6d570e4..854b248f84 100644--- a/plugin/parser/differ/pg/test-data/test_differ_data.yaml+++ b/plugin/parser/differ/pg/test-data/test_differ_data.yaml@@ -70,3 +70,38 @@ALTER TABLE "s1"."t2"DROP COLUMN "b";DROP TABLE "s1"."t1", "s2"."t4";+- oldSchema: |+ create table public.t1(+ a int,+ b int,+ constraint check_a check(a > b),+ constraint check_b check(b > 1)+ );+ newSchema: |+ create table public.t1(+ a int,+ b int,+ constraint check_b check(b > a),+ constraint check_c check(a > 1)+ )+ diff: |+ ALTER TABLE "public"."t1"+ DROP CONSTRAINT "check_b",+ ADD CONSTRAINT "check_b" CHECK (b > a),+ ADD CONSTRAINT "check_c" CHECK (a > 1),+ DROP CONSTRAINT "check_a";+- oldSchema: |+ create table public.t1(+ a int,+ b int,+ constraint check_a check(a > 1),+ constraint check_b check(b > 1)+ );+ newSchema: |+ create table public.t1(+ a int,+ b int,+ constraint check_b check(b > 1),+ constraint check_a check(a > 1)+ )+ diff: ""diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.goindex 63ce15e3e2..2a533832d5 100644--- a/plugin/parser/engine/pg/deparse.go+++ b/plugin/parser/engine/pg/deparse.go@@ -241,6 +241,16 @@ func deparseAddConstraint(context parser.DeparseContext, in *ast.AddConstraintStreturn err}}+ case ast.ConstraintTypeCheck:+ if _, err := buf.WriteString("CHECK ("); err != nil {+ return err+ }+ if _, err := buf.WriteString(in.Constraint.Expression.Text()); err != nil {+ return err+ }+ if _, err := buf.WriteString(")"); err != nil {+ return err+ }}return nil}diff --git a/plugin/parser/engine/pg/test-data/test_alter_table_data.yaml b/plugin/parser/engine/pg/test-data/test_alter_table_data.yamlindex 591ac957fa..73f28e5911 100644--- a/plugin/parser/engine/pg/test-data/test_alter_table_data.yaml+++ b/plugin/parser/engine/pg/test-data/test_alter_table_data.yaml@@ -74,3 +74,7 @@want: |-ALTER TABLE "t"ALTER COLUMN "a" DROP DEFAULT;+- stmt: alter table t add constraint check_a check (a + b > c)+ want: |-+ ALTER TABLE "t"+ ADD CONSTRAINT "check_a" CHECK ((a + b) > c);