Skip to content

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

chore: refactor ALTER TABLE stmt and partial support ALTER VIEW, DROP VIEW (#2074)

rebelicef70aa92adf97
diff --git a/plugin/parser/ast/alter_table_stmt.go b/plugin/parser/ast/alter_table_stmt.go
index d6d1d55d86..3cc7acab2d 100644
--- a/plugin/parser/ast/alter_table_stmt.go
+++ b/plugin/parser/ast/alter_table_stmt.go
@@ -1,6 +1,6 @@
package ast
-// AlterTableStmt is the struct for alter table statement.
+// AlterTableStmt is the struct for alter table or view statement.
type AlterTableStmt struct {
node
diff --git a/plugin/parser/ast/drop_table_stmt.go b/plugin/parser/ast/drop_table_stmt.go
index 6f7cc8ddb1..4bbb6f6b77 100644
--- a/plugin/parser/ast/drop_table_stmt.go
+++ b/plugin/parser/ast/drop_table_stmt.go
@@ -1,6 +1,6 @@
package ast
-// DropTableStmt is the struct for drop table statement.
+// DropTableStmt is the struct for drop table or view statement.
type DropTableStmt struct {
node
diff --git a/plugin/parser/ast/rename_table_stmt.go b/plugin/parser/ast/rename_table_stmt.go
index 479058e286..390bd83a22 100644
--- a/plugin/parser/ast/rename_table_stmt.go
+++ b/plugin/parser/ast/rename_table_stmt.go
@@ -1,7 +1,7 @@
package ast
-// RenameTableStmt is the struct for rename table statement.
-// For PostgreSQL dialect is ALTER TABLE RENAME.
+// RenameTableStmt is the struct for rename table or view statement.
+// For PostgreSQL dialect is ALTER TABLE RENAME and ALTER VIEW RENAME.
type RenameTableStmt struct {
node
diff --git a/plugin/parser/ast/set_schema_stmt.go b/plugin/parser/ast/set_schema_stmt.go
new file mode 100644
index 0000000000..d3634b2515
--- /dev/null
+++ b/plugin/parser/ast/set_schema_stmt.go
@@ -0,0 +1,9 @@
+package ast
+
+// SetSchemaStmt is the struct for set schema statement.
+type SetSchemaStmt struct {
+ node
+
+ Table *TableDef
+ NewSchema string
+}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 800a033c44..25b23d6356 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -121,21 +121,50 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {
if err != nil {
return nil, err
}
- return &ast.RenameColumnStmt{
- Table: convertRangeVarToTableName(in.RenameStmt.Relation, tableType),
- ColumnName: in.RenameStmt.Subname,
- NewName: in.RenameStmt.Newname,
+ table := convertRangeVarToTableName(in.RenameStmt.Relation, tableType)
+ return &ast.AlterTableStmt{
+ Table: table,
+ AlterItemList: []ast.Node{
+ &ast.RenameColumnStmt{
+ Table: table,
+ ColumnName: in.RenameStmt.Subname,
+ NewName: in.RenameStmt.Newname,
+ },
+ },
}, nil
case pgquery.ObjectType_OBJECT_TABLE:
- return &ast.RenameTableStmt{
- Table: convertRangeVarToTableName(in.RenameStmt.Relation, ast.TableTypeBaseTable),
- NewName: in.RenameStmt.Newname,
+ table := convertRangeVarToTableName(in.RenameStmt.Relation, ast.TableTypeBaseTable)
+ return &ast.AlterTableStmt{
+ Table: table,
+ AlterItemList: []ast.Node{
+ &ast.RenameTableStmt{
+ Table: table,
+ NewName: in.RenameStmt.Newname,
+ },
+ },
}, nil
case pgquery.ObjectType_OBJECT_TABCONSTRAINT:
- return &ast.RenameConstraintStmt{
- Table: convertRangeVarToTableName(in.RenameStmt.Relation, ast.TableTypeBaseTable),
- ConstraintName: in.RenameStmt.Subname,
- NewName: in.RenameStmt.Newname,
+ table := convertRangeVarToTableName(in.RenameStmt.Relation, ast.TableTypeBaseTable)
+ return &ast.AlterTableStmt{
+ Table: table,
+ AlterItemList: []ast.Node{
+ &ast.RenameConstraintStmt{
+ Table: table,
+ ConstraintName: in.RenameStmt.Subname,
+ NewName: in.RenameStmt.Newname,
+ },
+ },
+ }, nil
+ case pgquery.ObjectType_OBJECT_VIEW:
+ view := convertRangeVarToTableName(in.RenameStmt.Relation, ast.TableTypeView)
+ return &ast.AlterTableStmt{
+ Table: view,
+ AlterItemList: []ast.Node{
+ &ast.RenameTableStmt{
+ Table: view,
+ NewName: in.RenameStmt.Newname,
+ },
+ },
}, nil
case pgquery.ObjectType_OBJECT_INDEX:
return &ast.RenameIndexStmt{
@@ -201,6 +230,20 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {
dropTable.TableList = append(dropTable.TableList, tableDef)
}
return dropTable, nil
+ case pgquery.ObjectType_OBJECT_VIEW:
+ dropView := &ast.DropTableStmt{}
+ for _, object := range in.DropStmt.Objects {
+ list, ok := object.Node.(*pgquery.Node_List)
+ if !ok {
+ return nil, parser.NewConvertErrorf("expected List but found %t", object.Node)
+ }
+ viewDef, err := convertListToTableDef(list, ast.TableTypeView)
+ if err != nil {
+ return nil, err
+ }
+ dropView.TableList = append(dropView.TableList, viewDef)
+ }
+ return dropView, nil
}
case *pgquery.Node_DropdbStmt:
return &ast.DropDatabaseStmt{
@@ -209,6 +252,31 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {
}, nil
case *pgquery.Node_SelectStmt:
return convertSelectStmt(in.SelectStmt)
+ case *pgquery.Node_AlterObjectSchemaStmt:
+ switch in.AlterObjectSchemaStmt.ObjectType {
+ case pgquery.ObjectType_OBJECT_TABLE:
+ table := convertRangeVarToTableName(in.AlterObjectSchemaStmt.Relation, ast.TableTypeBaseTable)
+ return &ast.AlterTableStmt{
+ Table: table,
+ AlterItemList: []ast.Node{
+ &ast.SetSchemaStmt{
+ Table: table,
+ NewSchema: in.AlterObjectSchemaStmt.Newschema,
+ },
+ },
+ }, nil
+ case pgquery.ObjectType_OBJECT_VIEW:
+ view := convertRangeVarToTableName(in.AlterObjectSchemaStmt.Relation, ast.TableTypeView)
+ return &ast.AlterTableStmt{
+ Table: view,
+ AlterItemList: []ast.Node{
+ &ast.SetSchemaStmt{
+ Table: view,
+ NewSchema: in.AlterObjectSchemaStmt.Newschema,
+ },
+ },
+ }, nil
+ }
}
return nil, nil
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 0bab199806..687c9ebe69 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -269,18 +269,49 @@ func TestPGRenameTableStmt(t *testing.T) {
{
stmt: "ALTER TABLE techbook RENAME TO \"techBook\"",
want: []ast.Node{
- &ast.RenameTableStmt{
+ &ast.AlterTableStmt{
Table: &ast.TableDef{
Type: ast.TableTypeBaseTable,
Name: "techbook",
},
- NewName: "techBook",
+ AlterItemList: []ast.Node{
+ &ast.RenameTableStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "techbook",
+ },
+ NewName: "techBook",
+ },
+ },
},
},
textList: []string{
"ALTER TABLE techbook RENAME TO \"techBook\"",
},
},
+ {
+ stmt: "ALTER VIEW techbook RENAME TO \"techBook\"",
+ want: []ast.Node{
+ &ast.AlterTableStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeView,
+ Name: "techbook",
+ },
+ AlterItemList: []ast.Node{
+ &ast.RenameTableStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeView,
+ Name: "techbook",
+ },
+ NewName: "techBook",
+ },
+ },
+ },
+ },
+ textList: []string{
+ "ALTER VIEW techbook RENAME TO \"techBook\"",
+ },
+ },
}
runTests(t, tests)
@@ -291,19 +322,51 @@ func TestPGRenameColumnStmt(t *testing.T) {
{
stmt: "ALTER TABLE techbook RENAME abc TO \"ABC\"",
want: []ast.Node{
- &ast.RenameColumnStmt{
+ &ast.AlterTableStmt{
Table: &ast.TableDef{
Type: ast.TableTypeBaseTable,
Name: "techbook",
},
- ColumnName: "abc",
- NewName: "ABC",
+ AlterItemList: []ast.Node{
+ &ast.RenameColumnStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "techbook",
+ },
+ ColumnName: "abc",
+ NewName: "ABC",
+ },
+ },
},
},
textList: []string{
"ALTER TABLE techbook RENAME abc TO \"ABC\"",
},
},
+ {
+ stmt: "ALTER VIEW techbook RENAME abc TO \"ABC\"",
+ want: []ast.Node{
+ &ast.AlterTableStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeView,
+ Name: "techbook",
+ },
+ AlterItemList: []ast.Node{
+ &ast.RenameColumnStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeView,
+ Name: "techbook",
+ },
+ ColumnName: "abc",
+ NewName: "ABC",
+ },
+ },
+ },
+ },
+ textList: []string{
+ "ALTER VIEW techbook RENAME abc TO \"ABC\"",
+ },
+ },
}
runTests(t, tests)
@@ -314,13 +377,21 @@ func TestPGRenameConstraintStmt(t *testing.T) {
{
stmt: "ALTER TABLE tech_book RENAME CONSTRAINT uk_tech_a to \"UK_TECH_A\"",
want: []ast.Node{
- &ast.RenameConstraintStmt{
+ &ast.AlterTableStmt{
Table: &ast.TableDef{
Type: ast.TableTypeBaseTable,
Name: "tech_book",
},
- ConstraintName: "uk_tech_a",
- NewName: "UK_TECH_A",
+ AlterItemList: []ast.Node{
+ &ast.RenameConstraintStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ ConstraintName: "uk_tech_a",
+ NewName: "UK_TECH_A",
+ },
+ },
},
},
textList: []string{
@@ -698,6 +769,27 @@ func TestPGDropTableStmt(t *testing.T) {
"DROP TABLE tech_book, xschema.user",
},
},
+ {
+ stmt: "DROP VIEW tech_book, xschema.user",
+ want: []ast.Node{
+ &ast.DropTableStmt{
+ TableList: []*ast.TableDef{
+ {
+ Type: ast.TableTypeView,
+ Name: "tech_book",
+ },
+ {
+ Type: ast.TableTypeView,
+ Schema: "xschema",
+ Name: "user",
+ },
+ },
+ },
+ },
+ textList: []string{
+ "DROP VIEW tech_book, xschema.user",
+ },
+ },
}
runTests(t, tests)
@@ -934,3 +1026,56 @@ func TestPGDropDatabaseStmt(t *testing.T) {
runTests(t, tests)
}
+
+func TestSetSchemaStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "ALTER TABLE tech_book SET SCHEMA new_schema",
+ want: []ast.Node{
+ &ast.AlterTableStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ AlterItemList: []ast.Node{
+ &ast.SetSchemaStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ NewSchema: "new_schema",
+ },
+ },
+ },
+ },
+ textList: []string{
+ "ALTER TABLE tech_book SET SCHEMA new_schema",
+ },
+ },
+ {
+ stmt: "ALTER VIEW tech_book SET SCHEMA new_schema",
+ want: []ast.Node{
+ &ast.AlterTableStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeView,
+ Name: "tech_book",
+ },
+ AlterItemList: []ast.Node{
+ &ast.SetSchemaStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeView,
+ Name: "tech_book",
+ },
+ NewSchema: "new_schema",
+ },
+ },
+ },
+ },
+ textList: []string{
+ "ALTER VIEW tech_book SET SCHEMA new_schema",
+ },
+ },
+ }
+
+ runTests(t, tests)
+}