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(sync-schema): support sync exclude constraint (#3389)
h3n4l3637ed184e60
diff --git a/go.mod b/go.modindex 7df0c6d1c1..9e8b24b8d0 100644--- a/go.mod+++ b/go.mod@@ -170,4 +170,4 @@ replace github.com/pingcap/tidb => github.com/bytebase/tidb v0.0.0-2022102803595replace github.com/pingcap/tidb/parser => github.com/bytebase/tidb/parser v0.0.0-20221028035959-5d3b71eadf24-replace github.com/pganalyze/pg_query_go/v2 => github.com/bytebase/pg_query_go/v2 v2.0.0-20221102094723-0d12de86f87c+replace github.com/pganalyze/pg_query_go/v2 => github.com/bytebase/pg_query_go/v2 v2.0.0-20221115090145-10eaaf3c22b6diff --git a/go.sum b/go.sumindex 5c147f3e23..3f38a2a325 100644--- a/go.sum+++ b/go.sum@@ -145,8 +145,8 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRgithub.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=github.com/bytebase/gh-ost v1.1.3-0.20220728080340-11d9c9027651 h1:NpoTuRY6Ckj37zTlEax/UTfvHCOdanI5/5dVCarOD18=github.com/bytebase/gh-ost v1.1.3-0.20220728080340-11d9c9027651/go.mod h1:/HsnX0+34LXVz3MxtZoW15GiwEOXz/PrEhutjb3icfE=-github.com/bytebase/pg_query_go/v2 v2.0.0-20221102094723-0d12de86f87c h1:ySQRx63mU0OzCJ30nPFQf5BvbHCqvpCW4gizDLgN87I=-github.com/bytebase/pg_query_go/v2 v2.0.0-20221102094723-0d12de86f87c/go.mod h1:XAxmVqz1tEGqizcQ3YSdN90vCOHBWjJi8URL1er5+cA=+github.com/bytebase/pg_query_go/v2 v2.0.0-20221115090145-10eaaf3c22b6 h1:cdcvc3OYUFLylc9EKFnexiRixHryry5MG7d4jqsxBIA=+github.com/bytebase/pg_query_go/v2 v2.0.0-20221115090145-10eaaf3c22b6/go.mod h1:XAxmVqz1tEGqizcQ3YSdN90vCOHBWjJi8URL1er5+cA=github.com/bytebase/tidb v0.0.0-20221028035959-5d3b71eadf24 h1:XR25hLtR8TtpW/4mgP7EcOlzi8ewNY5EVan97WzDQxc=github.com/bytebase/tidb v0.0.0-20221028035959-5d3b71eadf24/go.mod h1:xrVXE+0V54QRGgrD9o+tuZmBDDx8vJpQgrPtRQvodWo=github.com/bytebase/tidb/parser v0.0.0-20221028035959-5d3b71eadf24 h1:DZPxIt2aZJ6L/gh8tH9jspTTIufBt7Wvgae1T7TmXAg=diff --git a/plugin/parser/ast/constraint_def.go b/plugin/parser/ast/constraint_def.goindex c73ae4fb66..432e3faf30 100644--- a/plugin/parser/ast/constraint_def.go+++ b/plugin/parser/ast/constraint_def.go@@ -22,6 +22,8 @@ const (ConstraintTypeNotNull// ConstraintTypeCheck is the check constraint.ConstraintTypeCheck+ // ConstraintTypeExclusion is the exclude constraint.+ ConstraintTypeExclusion// ConstraintTypeDefault is the default constraint.ConstraintTypeDefault)@@ -63,6 +65,12 @@ type ConstraintDef struct {// 1. CHECK constraint// 2. DEFAULT constraintExpression ExpressionNode+ // WhereClause is the where clause for the EXCLUDE constraint.+ WhereClause string+ // Exclusions is the list of exclusion elements for the EXCLUDE constraint.+ Exclusions string+ // AccessMethod is the access method for the EXCLUDE constraint.+ AccessMethod IndexMethodType// https://www.postgresql.org/docs/14/sql-altertable.htmlDeferrable boolInitdeferred booldiff --git a/plugin/parser/differ/pg/differ.go b/plugin/parser/differ/pg/differ.goindex be8b2a879f..616c40b4fb 100644--- a/plugin/parser/differ/pg/differ.go+++ b/plugin/parser/differ/pg/differ.go@@ -191,7 +191,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:+ case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion:if err := oldSchemaMap.addConstraint(i, item); err != nil {return "", err}@@ -239,7 +239,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:+ case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion:oldConstraint := oldSchemaMap.getConstraint(item.Table.Schema, item.Table.Name, item.Constraint.Name)if oldConstraint == nil {alterTableStmt.AlterItemList = append(alterTableStmt.AlterItemList, item)@@ -424,7 +424,7 @@ func (*diffNode) modifyConstraint(alterTableStmt *ast.AlterTableStmt, oldConstra})return nil}- case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary:+ case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion:// 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.yamlindex 57a6167451..34ac566b1d 100644--- a/plugin/parser/differ/pg/test-data/test_differ_constraint.yaml+++ b/plugin/parser/differ/pg/test-data/test_differ_constraint.yaml@@ -94,3 +94,29 @@ALTER TABLE "public"."order_details"DROP CONSTRAINT IF EXISTS "order_details_pk",ADD CONSTRAINT "order_details_pk" PRIMARY KEY ("id", "order_id");+- oldSchema: |+ CREATE TABLE public.circles(a circle, b circle, c integer);+ ALTER TABLE public.circles ADD CONSTRAINT circles_a_b_excl EXCLUDE USING gist (a WITH &&, b WITH &&) WHERE (c > 0);+ newSchema: |+ CREATE TABLE public.circles(a circle, b circle, c integer);+ diff: |+ ALTER TABLE "public"."circles"+ DROP CONSTRAINT IF EXISTS "circles_a_b_excl";+- oldSchema: |+ CREATE TABLE public.circles(a circle, b circle, c integer);+ newSchema: |+ CREATE TABLE public.circles(a circle, b circle, c integer);+ ALTER TABLE public.circles ADD CONSTRAINT circles_a_b_excl EXCLUDE USING gist (a WITH &&, b WITH &&) WHERE (c > 0);+ diff: |+ ALTER TABLE "public"."circles"+ ADD CONSTRAINT "circles_a_b_excl" EXCLUDE USING gist (a WITH &&, b WITH &&) WHERE (c > 0);+- oldSchema: |+ CREATE TABLE public.circles(a circle, b circle, c integer);+ ALTER TABLE public.circles ADD CONSTRAINT circles_a_b_excl EXCLUDE USING gist (a WITH &&) WHERE (c > 25);+ newSchema: |+ CREATE TABLE public.circles(a circle, b circle, c integer);+ ALTER TABLE public.circles ADD CONSTRAINT circles_a_b_excl EXCLUDE USING gist (a WITH &&, b WITH &&) WHERE (c > 0);+ diff: |+ ALTER TABLE "public"."circles"+ DROP CONSTRAINT IF EXISTS "circles_a_b_excl",+ ADD CONSTRAINT "circles_a_b_excl" EXCLUDE USING gist (a WITH &&, b WITH &&) WHERE (c > 0);diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex 411dc186a8..66019598ae 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -60,7 +60,6 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, errTable: alterTable.Table,ColumnName: alterCmd.Name,}-alterTable.AlterItemList = append(alterTable.AlterItemList, dropColumn)case pgquery.AlterTableType_AT_AddConstraint:def, ok := alterCmd.Def.Node.(*pgquery.Node_Constraint)@@ -961,6 +960,25 @@ func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error)}expression.SetText(text)cons.Expression = expression+ case ast.ConstraintTypeExclusion:+ if len(in.Constraint.Exclusions) >= 1 {+ exclusion, err := pgquery.DeparseNodes(pgquery.DeparseTypeExclusion, in.Constraint.Exclusions)+ if err != nil {+ return nil, err+ }+ cons.Exclusions = exclusion+ }+ var err error+ if cons.AccessMethod, err = convertMethodType(in.Constraint.AccessMethod); err != nil {+ return nil, err+ }+ if in.Constraint.WhereClause != nil {+ whereClause, err := pgquery.DeparseNode(pgquery.DeparseTypeExpr, in.Constraint.WhereClause)+ if err != nil {+ return nil, err+ }+ cons.WhereClause = whereClause+ }}return cons, nil@@ -1019,6 +1037,8 @@ func convertConstraintType(in pgquery.ConstrType, usingIndex bool) ast.Constrainreturn ast.ConstraintTypeCheckcase pgquery.ConstrType_CONSTR_DEFAULT:return ast.ConstraintTypeDefault+ case pgquery.ConstrType_CONSTR_EXCLUSION:+ return ast.ConstraintTypeExclusion}return ast.ConstraintTypeUndefined}diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex 033fe214d8..cbefa3db54 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -1333,6 +1333,38 @@ func TestPGAddConstraintStmt(t *testing.T) {},},},+ {+ stmt: "ALTER TABLE ONLY circles ADD CONSTRAINT circles_c_excl EXCLUDE USING gist (c WITH &&, d WITH &&) WHERE (a > 0);",+ want: []ast.Node{+ &ast.AlterTableStmt{+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "circles",+ },+ AlterItemList: []ast.Node{+ &ast.AddConstraintStmt{+ Table: &ast.TableDef{+ Type: ast.TableTypeBaseTable,+ Name: "circles",+ },+ Constraint: &ast.ConstraintDef{+ Type: ast.ConstraintTypeExclusion,+ Name: "circles_c_excl",+ Exclusions: "c WITH &&, d WITH &&",+ AccessMethod: ast.IndexMethodTypeGiST,+ WhereClause: "a > 0",+ },+ },+ },+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE ONLY circles ADD CONSTRAINT circles_c_excl EXCLUDE USING gist (c WITH &&, d WITH &&) WHERE (a > 0);",+ LastLine: 1,+ },+ },+ },}runTests(t, tests)diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.goindex a663f8a73b..afe31349b5 100644--- a/plugin/parser/engine/pg/deparse.go+++ b/plugin/parser/engine/pg/deparse.go@@ -456,6 +456,33 @@ func deparseConstraintDef(_ parser.DeparseContext, in *ast.ConstraintDef, buf *sif _, err := buf.WriteString(")"); err != nil {return err}+ case ast.ConstraintTypeExclusion:+ if _, err := buf.WriteString("EXCLUDE USING "); err != nil {+ return err+ }+ if err := deparseIndexMethod(in.AccessMethod, buf); err != nil {+ return err+ }+ if _, err := buf.WriteString(" ("); err != nil {+ return err+ }+ if _, err := buf.WriteString(in.Exclusions); err != nil {+ return err+ }+ if err := buf.WriteByte(')'); err != nil {+ return err+ }+ if in.WhereClause != "" {+ if _, err := buf.WriteString(" WHERE ("); err != nil {+ return err+ }+ if _, err := buf.WriteString(in.WhereClause); err != nil {+ return err+ }+ if err := buf.WriteByte(')'); err != nil {+ return err+ }+ }case ast.ConstraintTypeForeign:if _, err := buf.WriteString("FOREIGN KEY ("); err != nil {return errdiff --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 fee8d20d5e..e2ed7b2413 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@@ -112,3 +112,15 @@want: |-ALTER TABLE "t"ADD CONSTRAINT "email_pkey" PRIMARY KEY ("email") INCLUDE ("email") USING INDEX TABLESPACE "demo_tablespace";+- stmt: |-+ alter table t+ add constraint circles_c_excl exclude using gist (c WITH &&);+ want: |-+ ALTER TABLE "t"+ ADD CONSTRAINT "circles_c_excl" EXCLUDE USING gist (c WITH &&);+- stmt: |-+ alter table t+ add constraint circles_c_excl exclude using gist (c WITH &&, d WITH &&) WHERE (a > 0 AND b < 10);+ want: |-+ ALTER TABLE "t"+ ADD CONSTRAINT "circles_c_excl" EXCLUDE USING gist (c WITH &&, d WITH &&) WHERE (a > 0 AND b < 10);