Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add the NOT NULL constraint AST (#1943)

rebelicec6489f43a70b
diff --git a/plugin/parser/ast/constraint_def.go b/plugin/parser/ast/constraint_def.go
index 278d604a50..f6381c247b 100644
--- a/plugin/parser/ast/constraint_def.go
+++ b/plugin/parser/ast/constraint_def.go
@@ -18,6 +18,8 @@ const (
// ConstraintTypeUniqueUsingIndex is the unique constraint only for the PostgreSQL table_constraint_using_index.
// See https://www.postgresql.org/docs/current/sql-altertable.html.
ConstraintTypeUniqueUsingIndex
+ // ConstraintTypeNotNull is the not null constraint.
+ ConstraintTypeNotNull
)
// ConstraintDef is struct for constraint definition.
diff --git a/plugin/parser/ast/drop_not_null_stmt.go b/plugin/parser/ast/drop_not_null_stmt.go
new file mode 100644
index 0000000000..1d373d77ec
--- /dev/null
+++ b/plugin/parser/ast/drop_not_null_stmt.go
@@ -0,0 +1,9 @@
+package ast
+
+// DropNotNullStmt is the struct for set not null statement.
+type DropNotNullStmt struct {
+ node
+
+ Table *TableDef
+ ColumnName string
+}
diff --git a/plugin/parser/ast/set_not_null_stmt.go b/plugin/parser/ast/set_not_null_stmt.go
new file mode 100644
index 0000000000..8ed563960e
--- /dev/null
+++ b/plugin/parser/ast/set_not_null_stmt.go
@@ -0,0 +1,9 @@
+package ast
+
+// SetNotNullStmt is the struct for set not null statement.
+type SetNotNullStmt struct {
+ node
+
+ Table *TableDef
+ ColumnName string
+}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 83d08a962e..d94f7e1eaa 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -71,6 +71,20 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {
}
alterTable.AlterItemList = append(alterTable.AlterItemList, dropConstraint)
+ case pgquery.AlterTableType_AT_SetNotNull:
+ setNotNull := &ast.SetNotNullStmt{
+ Table: alterTable.Table,
+ ColumnName: alterCmd.Name,
+ }
+
+ alterTable.AlterItemList = append(alterTable.AlterItemList, setNotNull)
+ case pgquery.AlterTableType_AT_DropNotNull:
+ dropNotNull := &ast.DropNotNullStmt{
+ Table: alterTable.Table,
+ ColumnName: alterCmd.Name,
+ }
+
+ alterTable.AlterItemList = append(alterTable.AlterItemList, dropNotNull)
}
}
}
@@ -311,6 +325,8 @@ func convertConstraintType(in pgquery.ConstrType, usingIndex bool) ast.Constrain
return ast.ConstraintTypeUndefined
}
return ast.ConstraintTypeForeign
+ case pgquery.ConstrType_CONSTR_NOTNULL:
+ return ast.ConstraintTypeNotNull
}
return ast.ConstraintTypeUndefined
}
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 880094d317..e9dd05c133 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -30,7 +30,7 @@ func runTests(t *testing.T, tests []testData) {
func TestPGConvertCreateTableStmt(t *testing.T) {
tests := []testData{
{
- stmt: "CREATE TABLE \"techBook\" (a int, b int)",
+ stmt: "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL)",
want: []ast.Node{
&ast.CreateTableStmt{
IfNotExists: false,
@@ -38,13 +38,30 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
Name: "techBook",
},
ColumnList: []*ast.ColumnDef{
- {ColumnName: "a"},
- {ColumnName: "b"},
+ {
+ ColumnName: "a",
+ ConstraintList: []*ast.ConstraintDef{
+ {
+ Type: ast.ConstraintTypeNotNull,
+ KeyList: []string{"a"},
+ },
+ },
+ },
+ {
+ ColumnName: "b",
+ ConstraintList: []*ast.ConstraintDef{
+ {
+ Type: ast.ConstraintTypeNotNull,
+ Name: "b_not_null",
+ KeyList: []string{"b"},
+ },
+ },
+ },
},
},
},
textList: []string{
- "CREATE TABLE \"techBook\" (a int, b int)",
+ "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL)",
},
},
{
@@ -589,3 +606,44 @@ func TestPGDropTableStmt(t *testing.T) {
runTests(t, tests)
}
+
+func TestPGNotNullStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "ALTER TABLE tech_book ALTER COLUMN id SET NOT NULL",
+ want: []ast.Node{
+ &ast.AlterTableStmt{
+ Table: &ast.TableDef{Name: "tech_book"},
+ AlterItemList: []ast.Node{
+ &ast.SetNotNullStmt{
+ Table: &ast.TableDef{Name: "tech_book"},
+ ColumnName: "id",
+ },
+ },
+ },
+ },
+ textList: []string{
+ "ALTER TABLE tech_book ALTER COLUMN id SET NOT NULL",
+ },
+ },
+ {
+ stmt: "ALTER TABLE tech_book ALTER COLUMN id DROP NOT NULL",
+ want: []ast.Node{
+ &ast.AlterTableStmt{
+ Table: &ast.TableDef{Name: "tech_book"},
+ AlterItemList: []ast.Node{
+ &ast.DropNotNullStmt{
+ Table: &ast.TableDef{Name: "tech_book"},
+ ColumnName: "id",
+ },
+ },
+ },
+ },
+ textList: []string{
+ "ALTER TABLE tech_book ALTER COLUMN id DROP NOT NULL",
+ },
+ },
+ }
+
+ runTests(t, tests)
+}