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: support PostgreSQL ALTER TABLE RENAME/ADD/DROP CONSTRAINT and ADD COLUMN CONSTRAINT (#1736)
rebelicef99172cf7702
diff --git a/plugin/parser/ast/add_constraint_stmt.go b/plugin/parser/ast/add_constraint_stmt.gonew file mode 100644index 0000000000..b468260cf2--- /dev/null+++ b/plugin/parser/ast/add_constraint_stmt.go@@ -0,0 +1,9 @@+package ast++// AddConstraintStmt is the struct for add constraint statement.+type AddConstraintStmt struct {+ node++ Table *TableDef+ Constraint *ConstraintDef+}diff --git a/plugin/parser/ast/constraint_def.go b/plugin/parser/ast/constraint_def.goindex 56c832b714..9ad29a110e 100644--- a/plugin/parser/ast/constraint_def.go+++ b/plugin/parser/ast/constraint_def.go@@ -12,6 +12,12 @@ const (ConstraintTypeUnique// ConstraintTypeForeign is the foreign key constraint.ConstraintTypeForeign+ // ConstraintTypePrimaryUsingIndex is the primary key constraint only for the PostgreSQL table_constraint_using_index.+ // See https://www.postgresql.org/docs/current/sql-altertable.html.+ ConstraintTypePrimaryUsingIndex+ // ConstraintTypeUniqueUsingIndex is the unique constraint only for the PostgreSQL table_constraint_using_index.+ // See https://www.postgresql.org/docs/current/sql-altertable.html.+ ConstraintTypeUniqueUsingIndex)// ConstraintDef is struct for constraint definition.@@ -38,4 +44,7 @@ type ConstraintDef struct {KeyList []string// Foreign is a FOREIGN type specific field.Foreign *ForeignDef+ // IndexName is a ConstraintTypePrimaryUsingIndex/ConstraintTypeUniqueUsingIndex type specific field.+ // See https://www.postgresql.org/docs/current/sql-altertable.html.+ IndexName string}diff --git a/plugin/parser/ast/drop_constraint_stmt.go b/plugin/parser/ast/drop_constraint_stmt.gonew file mode 100644index 0000000000..19a9520c1e--- /dev/null+++ b/plugin/parser/ast/drop_constraint_stmt.go@@ -0,0 +1,9 @@+package ast++// DropConstraintStmt is the struct for drop constraint statement.+type DropConstraintStmt struct {+ node++ Table *TableDef+ ConstraintName string+}diff --git a/plugin/parser/ast/rename_constraint_stmt.go b/plugin/parser/ast/rename_constraint_stmt.gonew file mode 100644index 0000000000..d05c250dfc--- /dev/null+++ b/plugin/parser/ast/rename_constraint_stmt.go@@ -0,0 +1,10 @@+package ast++// RenameConstraintStmt is the struct for rename constraint statement.+type RenameConstraintStmt struct {+ node++ Table *TableDef+ ConstraintName string+ NewName string+}diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex edeb1bdad5..56d7af4ebc 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -25,19 +25,40 @@ func convert(node *pgquery.Node) (ast.Node, error) {return nil, parser.NewConvertErrorf("expected ColumnDef but found %t", alterCmd.Def.Node)}+ column, err := convertColumnDef(def)+ if err != nil {+ return nil, err+ }+addColumn := &ast.AddColumnListStmt{- Table: alterTable.Table,- ColumnList: []*ast.ColumnDef{- {- ColumnName: def.ColumnDef.Colname,- },- },+ Table: alterTable.Table,+ ColumnList: []*ast.ColumnDef{column},}alterTable.AlterItemList = append(alterTable.AlterItemList, addColumn)- case pgquery.AlterTableType_AT_AddIndex:- // TODO.- // Trick linter to use switch...case...+ case pgquery.AlterTableType_AT_AddConstraint:+ def, ok := alterCmd.Def.Node.(*pgquery.Node_Constraint)+ if !ok {+ return nil, parser.NewConvertErrorf("expected Constraint but found %t", alterCmd.Def.Node)+ }+ constraint, err := convertConstraint(def)+ if err != nil {+ return nil, err+ }++ addConstraint := &ast.AddConstraintStmt{+ Table: alterTable.Table,+ Constraint: constraint,+ }++ alterTable.AlterItemList = append(alterTable.AlterItemList, addConstraint)+ case pgquery.AlterTableType_AT_DropConstraint:+ dropConstraint := &ast.DropConstraintStmt{+ Table: alterTable.Table,+ ConstraintName: alterCmd.Name,+ }++ alterTable.AlterItemList = append(alterTable.AlterItemList, dropConstraint)}}}@@ -51,23 +72,10 @@ func convert(node *pgquery.Node) (ast.Node, error) {for _, elt := range in.CreateStmt.TableElts {switch item := elt.Node.(type) {case *pgquery.Node_ColumnDef:- column := &ast.ColumnDef{- ColumnName: item.ColumnDef.Colname,- }-- for _, cons := range item.ColumnDef.Constraints {- constraint, ok := cons.Node.(*pgquery.Node_Constraint)- if !ok {- return nil, parser.NewConvertErrorf("expected Constraint but found %t", cons.Node)- }- columnCons, err := convertConstraint(constraint)- if err != nil {- return nil, err- }- columnCons.KeyList = append(columnCons.KeyList, item.ColumnDef.Colname)- column.ConstraintList = append(column.ConstraintList, columnCons)+ column, err := convertColumnDef(item)+ if err != nil {+ return nil, err}-table.ColumnList = append(table.ColumnList, column)case *pgquery.Node_Constraint:cons, err := convertConstraint(item)@@ -91,6 +99,12 @@ func convert(node *pgquery.Node) (ast.Node, error) {Table: convertRangeVarToTableName(in.RenameStmt.Relation),NewName: in.RenameStmt.Newname,}, nil+ case pgquery.ObjectType_OBJECT_TABCONSTRAINT:+ return &ast.RenameConstraintStmt{+ Table: convertRangeVarToTableName(in.RenameStmt.Relation),+ ConstraintName: in.RenameStmt.Subname,+ NewName: in.RenameStmt.Newname,+ }, nil}}@@ -108,18 +122,19 @@ func convertRangeVarToTableName(in *pgquery.RangeVar) *ast.TableDef {func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error) {cons := &ast.ConstraintDef{Name: in.Constraint.Conname,- Type: convertConstraintType(in.Constraint.Contype),+ Type: convertConstraintType(in.Constraint.Contype, in.Constraint.Indexname != ""),}- for _, key := range in.Constraint.Keys {- name, ok := key.Node.(*pgquery.Node_String_)- if !ok {- return nil, parser.NewConvertErrorf("expected String but found %t", key.Node)+ switch cons.Type {+ case ast.ConstraintTypePrimary, ast.ConstraintTypeUnique:+ for _, key := range in.Constraint.Keys {+ name, ok := key.Node.(*pgquery.Node_String_)+ if !ok {+ return nil, parser.NewConvertErrorf("expected String but found %t", key.Node)+ }+ cons.KeyList = append(cons.KeyList, name.String_.Str)}- cons.KeyList = append(cons.KeyList, name.String_.Str)- }-- if cons.Type == ast.ConstraintTypeForeign {+ case ast.ConstraintTypeForeign:cons.Foreign = &ast.ForeignDef{Table: convertRangeVarToTableName(in.Constraint.Pktable),}@@ -139,19 +154,51 @@ func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error)}cons.KeyList = append(cons.KeyList, name.String_.Str)}+ case ast.ConstraintTypePrimaryUsingIndex, ast.ConstraintTypeUniqueUsingIndex:+ cons.IndexName = in.Constraint.Indexname}return cons, nil}-func convertConstraintType(in pgquery.ConstrType) ast.ConstraintType {+func convertConstraintType(in pgquery.ConstrType, usingIndex bool) ast.ConstraintType {switch in {case pgquery.ConstrType_CONSTR_PRIMARY:+ if usingIndex {+ return ast.ConstraintTypePrimaryUsingIndex+ }return ast.ConstraintTypePrimarycase pgquery.ConstrType_CONSTR_UNIQUE:+ if usingIndex {+ return ast.ConstraintTypeUniqueUsingIndex+ }return ast.ConstraintTypeUniquecase pgquery.ConstrType_CONSTR_FOREIGN:+ if usingIndex {+ return ast.ConstraintTypeUndefined+ }return ast.ConstraintTypeForeign}return ast.ConstraintTypeUndefined}++func convertColumnDef(in *pgquery.Node_ColumnDef) (*ast.ColumnDef, error) {+ column := &ast.ColumnDef{+ ColumnName: in.ColumnDef.Colname,+ }++ for _, cons := range in.ColumnDef.Constraints {+ constraint, ok := cons.Node.(*pgquery.Node_Constraint)+ if !ok {+ return nil, parser.NewConvertErrorf("expected Constraint but found %t", cons.Node)+ }+ columnCons, err := convertConstraint(constraint)+ if err != nil {+ return nil, err+ }+ columnCons.KeyList = append(columnCons.KeyList, in.ColumnDef.Colname)+ column.ConstraintList = append(column.ConstraintList, columnCons)+ }++ return column, nil+}diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex 938bd22afc..dafd245ae5 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -14,6 +14,16 @@ type testData struct {want []ast.Node}+func runTests(t *testing.T, tests []testData) {+ p := &PostgreSQLParser{}++ for _, test := range tests {+ res, err := p.Parse(parser.Context{}, test.stmt)+ require.NoError(t, err)+ require.Nilf(t, pretty.Diff(test.want, res), "stmt: %s", test.stmt)+ }+}+func TestPGConvertCreateTableStmt(t *testing.T) {tests := []testData{{@@ -139,13 +149,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {},}- p := &PostgreSQLParser{}-- for _, test := range tests {- res, err := p.Parse(parser.Context{}, test.stmt)- require.NoError(t, err)- require.Nilf(t, pretty.Diff(test.want, res), "stmt: %s", test.stmt)- }+ runTests(t, tests)}func TestPGAddColumnStmt(t *testing.T) {@@ -170,15 +174,38 @@ func TestPGAddColumnStmt(t *testing.T) {},},},+ {+ stmt: "ALTER TABLE techbook ADD COLUMN a int CONSTRAINT uk_techbook_a UNIQUE",+ want: []ast.Node{+ &ast.AlterTableStmt{+ Table: &ast.TableDef{+ Name: "techbook",+ },+ AlterItemList: []ast.Node{+ &ast.AddColumnListStmt{+ Table: &ast.TableDef{+ Name: "techbook",+ },+ ColumnList: []*ast.ColumnDef{+ {+ ColumnName: "a",+ ConstraintList: []*ast.ConstraintDef{+ {+ Type: ast.ConstraintTypeUnique,+ Name: "uk_techbook_a",+ KeyList: []string{"a"},+ },+ },+ },+ },+ },+ },+ },+ },+ },}- p := &PostgreSQLParser{}-- for _, test := range tests {- res, err := p.Parse(parser.Context{}, test.stmt)- require.NoError(t, err)- require.Nilf(t, pretty.Diff(test.want, res), "stmt: %s", test.stmt)- }+ runTests(t, tests)}func TestPGRenameTableStmt(t *testing.T) {@@ -196,13 +223,7 @@ func TestPGRenameTableStmt(t *testing.T) {},}- p := &PostgreSQLParser{}-- for _, test := range tests {- res, err := p.Parse(parser.Context{}, test.stmt)- require.NoError(t, err)- require.Nilf(t, pretty.Diff(test.want, res), "stmt: %s", test.stmt)- }+ runTests(t, tests)}func TestPGRenameColumnStmt(t *testing.T) {@@ -221,6 +242,23 @@ func TestPGRenameColumnStmt(t *testing.T) {},}+ runTests(t, tests)+}++func TestPGRenameConstraintStmt(t *testing.T) {+ tests := []testData{+ {+ stmt: "ALTER TABLE tech_book RENAME CONSTRAINT uk_tech_a to \"UK_TECH_A\"",+ want: []ast.Node{+ &ast.RenameConstraintStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ ConstraintName: "uk_tech_a",+ NewName: "UK_TECH_A",+ },+ },+ },+ }+p := &PostgreSQLParser{}for _, test := range tests {@@ -229,3 +267,125 @@ func TestPGRenameColumnStmt(t *testing.T) {require.Nilf(t, pretty.Diff(test.want, res), "stmt: %s", test.stmt)}}++func TestPGDropConstraintStmt(t *testing.T) {+ tests := []testData{+ {+ stmt: "ALTER TABLE tech_book DROP CONSTRAINT uk_tech_a",+ want: []ast.Node{+ &ast.AlterTableStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ AlterItemList: []ast.Node{+ &ast.DropConstraintStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ ConstraintName: "uk_tech_a",+ },+ },+ },+ },+ },+ }++ runTests(t, tests)+}++func TestPGAddConstraintStmt(t *testing.T) {+ tests := []testData{+ {+ stmt: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE (id)",+ want: []ast.Node{+ &ast.AlterTableStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ AlterItemList: []ast.Node{+ &ast.AddConstraintStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ Constraint: &ast.ConstraintDef{+ Type: ast.ConstraintTypeUnique,+ Name: "uk_tech_book_id",+ KeyList: []string{"id"},+ },+ },+ },+ },+ },+ },+ {+ stmt: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY (id)",+ want: []ast.Node{+ &ast.AlterTableStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ AlterItemList: []ast.Node{+ &ast.AddConstraintStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ Constraint: &ast.ConstraintDef{+ Type: ast.ConstraintTypePrimary,+ Name: "pk_tech_book_id",+ KeyList: []string{"id"},+ },+ },+ },+ },+ },+ },+ {+ stmt: "ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_id FOREIGN KEY (id) REFERENCES people(id)",+ want: []ast.Node{+ &ast.AlterTableStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ AlterItemList: []ast.Node{+ &ast.AddConstraintStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ Constraint: &ast.ConstraintDef{+ Type: ast.ConstraintTypeForeign,+ Name: "fk_tech_book_id",+ KeyList: []string{"id"},+ Foreign: &ast.ForeignDef{+ Table: &ast.TableDef{Name: "people"},+ ColumnList: []string{"id"},+ },+ },+ },+ },+ },+ },+ },+ {+ stmt: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE USING INDEX uk_id",+ want: []ast.Node{+ &ast.AlterTableStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ AlterItemList: []ast.Node{+ &ast.AddConstraintStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ Constraint: &ast.ConstraintDef{+ Type: ast.ConstraintTypeUniqueUsingIndex,+ Name: "uk_tech_book_id",+ IndexName: "uk_id",+ },+ },+ },+ },+ },+ },+ {+ stmt: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY USING INDEX pk_id",+ want: []ast.Node{+ &ast.AlterTableStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ AlterItemList: []ast.Node{+ &ast.AddConstraintStmt{+ Table: &ast.TableDef{Name: "tech_book"},+ Constraint: &ast.ConstraintDef{+ Type: ast.ConstraintTypePrimaryUsingIndex,+ Name: "pk_tech_book_id",+ IndexName: "pk_id",+ },+ },+ },+ },+ },+ },+ }++ runTests(t, tests)+}