Skip to content

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

feat: support delete table and delete column (#3097)

h3n4lbd7c2a55d79e
diff --git a/plugin/parser/differ/mysql/column_test.go b/plugin/parser/differ/mysql/column_test.go
index 0c2aabd453..960e2dd114 100644
--- a/plugin/parser/differ/mysql/column_test.go
+++ b/plugin/parser/differ/mysql/column_test.go
@@ -29,6 +29,13 @@ func TestColumnExist(t *testing.T) {
new: `CREATE TABLE book(id INT, price INT, code VARCHAR(50), PRIMARY KEY(id));`,
want: "",
},
+ // excess columns
+ {
+ old: `CREATE TABLE book(id INT, price INT, code VARCHAR(50), PRIMARY KEY(id));`,
+ new: `CREATE TABLE book(price INT, code VARCHAR(50));`,
+ want: "ALTER TABLE `book` DROP PRIMARY KEY;\n" +
+ "ALTER TABLE `book` DROP COLUMN `id`;\n",
+ },
}
testDiffWithoutDisableForeignKeyCheck(t, tests)
}
diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.go
index 940a711e23..716f6c1a25 100644
--- a/plugin/parser/differ/mysql/differ.go
+++ b/plugin/parser/differ/mysql/differ.go
@@ -118,6 +118,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
indexMap := buildIndexMap(oldStmt)
constraintMap := buildConstraintMap(oldStmt)
var alterTableAddColumnSpecs []*ast.AlterTableSpec
+ var alterTableDropColumnSpecs []*ast.AlterTableSpec
var alterTableModifyColumnSpecs []*ast.AlterTableSpec
var alterTableAddNewConstraintSpecs []*ast.AlterTableSpec
var alterTableDropExcessConstraintSpecs []*ast.AlterTableSpec
@@ -143,6 +144,16 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
Position: &ast.ColumnPosition{Tp: ast.ColumnPositionNone},
})
}
+ delete(oldColumnMap, newColumnName)
+ }
+ // TODO(zp): add an option to control whether to drop the excess columns.
+ for _, columnDef := range oldColumnMap {
+ alterTableDropColumnSpecs = append(alterTableDropColumnSpecs, &ast.AlterTableSpec{
+ Tp: ast.AlterTableDropColumn,
+ OldColumnName: &ast.ColumnName{
+ Name: model.NewCIStr(columnDef.Name.Name.O),
+ },
+ })
}
// Compare the create definitions
for _, constraint := range newStmt.Constraints {
@@ -238,6 +249,14 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
Specs: alterTableAddColumnSpecs,
})
}
+ if len(alterTableDropColumnSpecs) > 0 {
+ dropNodeList = append(dropNodeList, &ast.AlterTableStmt{
+ Table: &ast.TableName{
+ Name: model.NewCIStr(tableName),
+ },
+ Specs: alterTableDropColumnSpecs,
+ })
+ }
if len(alterTableModifyColumnSpecs) > 0 {
inplaceUpdate = append(inplaceUpdate, &ast.AlterTableStmt{
Table: &ast.TableName{
@@ -311,6 +330,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
Specs: alterTableInplaceAddConstraintSpecs,
})
}
+ delete(oldTableMap, tableName)
case *ast.CreateViewStmt:
newViewList = append(newViewList, newStmt)
}
@@ -396,6 +416,14 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
dropNodeList = append(dropNodeList, dropViewStmt)
}
+ // TODO(zp): Add an option to control whether to drop the excess table.
+ for _, oldTable := range oldTableMap {
+ dropTableStmt := &ast.DropTableStmt{
+ Tables: []*ast.TableName{oldTable.Table},
+ }
+ dropNodeList = append(dropNodeList, dropTableStmt)
+ }
+
var buf bytes.Buffer
if err := deparse(&buf, newNodeList, newNodeStmt, inplaceUpdate,
inplaceAddNodeList, inplaceAddStmt, inplaceDropNodeList,
diff --git a/plugin/parser/differ/mysql/table_test.go b/plugin/parser/differ/mysql/table_test.go
index fe279d3afb..47b7f9d19c 100644
--- a/plugin/parser/differ/mysql/table_test.go
+++ b/plugin/parser/differ/mysql/table_test.go
@@ -30,6 +30,15 @@ func TestTable(t *testing.T) {
`,
want: "",
},
+ // Drop excess table
+ {
+ old: `CREATE TABLE book(id INT, price_id INT, PRIMARY KEY(id), CONSTRAINT fk_price_id FOREIGN KEY (price_id) REFERENCES price(id));
+ CREATE TABLE price(id INT, PRIMARY KEY(id));`,
+ new: `CREATE TABLE book(id INT, PRIMARY KEY(id));`,
+ want: "DROP TABLE `price`;\n" +
+ "ALTER TABLE `book` DROP FOREIGN KEY `fk_price_id`;\n" +
+ "ALTER TABLE `book` DROP COLUMN `price_id`;\n",
+ },
}
testDiffWithoutDisableForeignKeyCheck(t, tests)
}