Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: support PG ADD CHECK and DROP DATABASE (#2062)
rebelice5743cf93769a
diff --git a/plugin/parser/ast/constraint_def.go b/plugin/parser/ast/constraint_def.goindex f6381c247b..0131ce065e 100644--- a/plugin/parser/ast/constraint_def.go+++ b/plugin/parser/ast/constraint_def.go@@ -20,6 +20,8 @@ const (ConstraintTypeUniqueUsingIndex// ConstraintTypeNotNull is the not null constraint.ConstraintTypeNotNull+ // ConstraintTypeCheck is the check constraint.+ ConstraintTypeCheck)// ConstraintDef is struct for constraint definition.@@ -49,4 +51,9 @@ type ConstraintDef struct {// IndexName is a ConstraintTypePrimaryUsingIndex or ConstraintTypeUniqueUsingIndex type specific field.// See https://www.postgresql.org/docs/current/sql-altertable.html.IndexName string+ // For PG, the option SkipValidation is currently only allowed for foreign key and CHECK constraints.+ // If SkipValidation is true, the constraint will only be enforced against subsequent inserts or updates.+ SkipValidation bool+ // CheckExpression is the expression for the check constraint.+ CheckExpression ExpressionNode}diff --git a/plugin/parser/ast/drop_database_stmt.go b/plugin/parser/ast/drop_database_stmt.gonew file mode 100644index 0000000000..dd6ebc6e51--- /dev/null+++ b/plugin/parser/ast/drop_database_stmt.go@@ -0,0 +1,9 @@+package ast++// DropDatabaseStmt is the struct for drop database statement.+type DropDatabaseStmt struct {+ node++ DatabaseName string+ IfExists bool+}diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex 3dfc46b5fa..4ddb5c0f0a 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -198,6 +198,11 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {}return dropTable, nil}+ case *pgquery.Node_DropdbStmt:+ return &ast.DropDatabaseStmt{+ DatabaseName: in.DropdbStmt.Dbname,+ IfExists: in.DropdbStmt.MissingOk,+ }, nilcase *pgquery.Node_SelectStmt:return convertSelectStmt(in.SelectStmt)}@@ -463,8 +468,9 @@ func convertRangeVarToIndexTableName(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, in.Constraint.Indexname != ""),+ Name: in.Constraint.Conname,+ Type: convertConstraintType(in.Constraint.Contype, in.Constraint.Indexname != ""),+ SkipValidation: in.Constraint.SkipValidation,}switch cons.Type {@@ -498,6 +504,12 @@ func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error)}case ast.ConstraintTypePrimaryUsingIndex, ast.ConstraintTypeUniqueUsingIndex:cons.IndexName = in.Constraint.Indexname+ case ast.ConstraintTypeCheck:+ expression, _, _, err := convertExpressionNode(in.Constraint.RawExpr)+ if err != nil {+ return nil, err+ }+ cons.CheckExpression = expression}return cons, nil@@ -522,6 +534,8 @@ func convertConstraintType(in pgquery.ConstrType, usingIndex bool) ast.Constrainreturn ast.ConstraintTypeForeigncase pgquery.ConstrType_CONSTR_NOTNULL:return ast.ConstraintTypeNotNull+ case pgquery.ConstrType_CONSTR_CHECK:+ return ast.ConstraintTypeCheck}return ast.ConstraintTypeUndefined}diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex 600af794d0..2a6b1d26db 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -444,6 +444,28 @@ func TestPGDropConstraintStmt(t *testing.T) {func TestPGAddConstraintStmt(t *testing.T) {tests := []testData{+ {+ stmt: "ALTER TABLE tech_book ADD CONSTRAINT check_a_bigger_than_b CHECK (a > b) NOT VALID",+ 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.ConstraintTypeCheck,+ Name: "check_a_bigger_than_b",+ SkipValidation: true,+ CheckExpression: &ast.UnconvertedExpressionDef{},+ },+ },+ },+ },+ },+ textList: []string{+ "ALTER TABLE tech_book ADD CONSTRAINT check_a_bigger_than_b CHECK (a > b) NOT VALID",+ },+ },{stmt: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE (id)",want: []ast.Node{@@ -797,3 +819,33 @@ func TestPGSelectStmt(t *testing.T) {runTests(t, tests)}++func TestPGDropDatabaseStmt(t *testing.T) {+ tests := []testData{+ {+ stmt: "DROP DATABASE test",+ want: []ast.Node{+ &ast.DropDatabaseStmt{+ DatabaseName: "test",+ },+ },+ textList: []string{+ "DROP DATABASE test",+ },+ },+ {+ stmt: "DROP DATABASE IF EXISTS test",+ want: []ast.Node{+ &ast.DropDatabaseStmt{+ DatabaseName: "test",+ IfExists: true,+ },+ },+ textList: []string{+ "DROP DATABASE IF EXISTS test",+ },+ },+ }++ runTests(t, tests)+}