Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: support NULL for PostgreSQL (#8848)
rebelice202dc09fbae8
diff --git a/backend/plugin/parser/sql/ast/constraint_def.go b/backend/plugin/parser/sql/ast/constraint_def.goindex fea47f97ab..97f720a3b3 100644--- a/backend/plugin/parser/sql/ast/constraint_def.go+++ b/backend/plugin/parser/sql/ast/constraint_def.go@@ -28,6 +28,8 @@ const (ConstraintTypeDefault// ConstraintTypeGenerated is the generated constraint.ConstraintTypeGenerated+ // ConstraintTypeNull is the null constraint.+ ConstraintTypeNull)// ConstraintDef is struct for constraint definition.diff --git a/backend/plugin/parser/sql/engine/pg/convert.go b/backend/plugin/parser/sql/engine/pg/convert.goindex ab9e0b62f8..610f4a6234 100644--- a/backend/plugin/parser/sql/engine/pg/convert.go+++ b/backend/plugin/parser/sql/engine/pg/convert.go@@ -1596,6 +1596,8 @@ func convertConstraintType(in pgquery.ConstrType, usingIndex bool) ast.Constrainreturn ast.ConstraintTypeExclusioncase pgquery.ConstrType_CONSTR_GENERATED:return ast.ConstraintTypeGenerated+ case pgquery.ConstrType_CONSTR_NULL:+ return ast.ConstraintTypeNull}return ast.ConstraintTypeUndefined}diff --git a/backend/plugin/parser/sql/engine/pg/convert_test.go b/backend/plugin/parser/sql/engine/pg/convert_test.goindex 412ba19053..6f7079741a 100644--- a/backend/plugin/parser/sql/engine/pg/convert_test.go+++ b/backend/plugin/parser/sql/engine/pg/convert_test.go@@ -331,7 +331,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {},},{- stmt: "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL)",+ stmt: "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL, c int NULL)",want: []ast.Node{&ast.CreateTableStmt{IfNotExists: false,@@ -361,17 +361,27 @@ func TestPGConvertCreateTableStmt(t *testing.T) {},},},+ {+ ColumnName: "c",+ Type: &ast.Integer{Size: 4},+ ConstraintList: []*ast.ConstraintDef{+ {+ Type: ast.ConstraintTypeNull,+ KeyList: []string{"c"},+ },+ },+ },},},},statementList: []base.SingleSQL{{- Text: "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL)",+ Text: "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL, c int NULL)",LastLine: 1,},},columnLine: [][]int{- {1, 1},+ {1, 1, 1},},},{diff --git a/backend/plugin/parser/sql/engine/pg/deparse.go b/backend/plugin/parser/sql/engine/pg/deparse.goindex 8f0636bb28..ae8d407abd 100644--- a/backend/plugin/parser/sql/engine/pg/deparse.go+++ b/backend/plugin/parser/sql/engine/pg/deparse.go@@ -1034,6 +1034,10 @@ func deparseColumnConstraint(_ DeparseContext, in *ast.ConstraintDef, buf *strin}}switch in.Type {+ case ast.ConstraintTypeNull:+ if _, err := buf.WriteString("NULL"); err != nil {+ return err+ }case ast.ConstraintTypeNotNull:if _, err := buf.WriteString("NOT NULL"); err != nil {return errdiff --git a/backend/plugin/parser/sql/engine/pg/test-data/test_create_table_data.yaml b/backend/plugin/parser/sql/engine/pg/test-data/test_create_table_data.yamlindex 1480c2da08..cf2802b091 100644--- a/backend/plugin/parser/sql/engine/pg/test-data/test_create_table_data.yaml+++ b/backend/plugin/parser/sql/engine/pg/test-data/test_create_table_data.yaml@@ -6,7 +6,7 @@- stmt: |-CREATE TABLE tech_book(a char(20),- b character(30) default 'this is default',+ b character(30) NULL default 'this is default',c varchar(330),d character varying(400),e text@@ -14,7 +14,7 @@want: |-CREATE TABLE "tech_book" ("a" character(20),- "b" character(30) DEFAULT 'this is default',+ "b" character(30) NULL DEFAULT 'this is default',"c" character varying(330),"d" character varying(400),"e" text