Skip to content

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

chore(mysqlv2): support drop table (#9438)

zll600f3a6c5e058fc
diff --git a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
index f5538ce776..a818867e5a 100644
--- a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
+++ b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
@@ -1,3 +1,31 @@
+- statement: |-
+ CREATE TABLE t1(a INT);
+ CREATE TABLE t2(a INT);
+ DROP TABLE t1, t2;
+ CREATE TABLE t3(a INT);
+ ignore_case_sensitive: false
+ want: |-
+ {
+ "name": "test",
+ "schemas": [
+ {
+ "tables": [
+ {
+ "name": "t3",
+ "columns": [
+ {
+ "name": "a",
+ "position": 1,
+ "nullable": true,
+ "type": "int"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ err: null
- statement: |-
CREATE TABLE t1(
a INT AUTO_INCREMENT,
@@ -36,29 +64,29 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
+ "name": "t1",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "nullable": true,
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "nullable": true,
+ "type": "int"
}
]
},
{
- "name": "t2",
- "columns": [
+ "name": "t2",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "nullable": true,
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "nullable": true,
+ "type": "int"
}
]
}
@@ -81,78 +109,78 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
+ "name": "t1",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "type": "int"
},
{
- "name": "b",
- "position": 2,
- "default": "1",
- "type": "int"
+ "name": "b",
+ "position": 2,
+ "default": "1",
+ "type": "int"
},
{
- "name": "c",
- "position": 3,
- "nullable": true,
- "type": "int"
+ "name": "c",
+ "position": 3,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "d",
- "position": 4,
- "type": "varchar"
+ "name": "d",
+ "position": 4,
+ "type": "varchar"
},
{
- "name": "e",
- "position": 5,
- "nullable": true,
- "type": "varchar"
+ "name": "e",
+ "position": 5,
+ "nullable": true,
+ "type": "varchar"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "type": "BTREE",
- "unique": true,
- "primary": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "primary": true,
+ "visible": true
},
{
- "name": "d",
- "expressions": [
+ "name": "d",
+ "expressions": [
"d",
"e"
],
- "type": "FULLTEXT"
+ "type": "FULLTEXT"
},
{
- "name": "idx1",
- "expressions": [
+ "name": "idx1",
+ "expressions": [
"c"
],
- "type": "BTREE",
- "visible": true
+ "type": "BTREE",
+ "visible": true
},
{
- "name": "uk1",
- "expressions": [
+ "name": "uk1",
+ "expressions": [
"b",
"c"
],
- "type": "BTREE",
- "unique": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
}
]
}
@@ -206,70 +234,70 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
+ "name": "t1",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "default": "1",
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "default": "1",
+ "type": "int"
},
{
- "name": "b",
- "position": 2,
- "nullable": true,
- "type": "int"
+ "name": "b",
+ "position": 2,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "c",
- "position": 3,
- "default": "NULL",
- "type": "varchar",
- "comment": "column c"
+ "name": "c",
+ "position": 3,
+ "default": "NULL",
+ "type": "varchar",
+ "comment": "column c"
},
{
- "name": "e",
- "position": 4,
- "nullable": true,
- "type": "int"
+ "name": "e",
+ "position": 4,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "f",
- "position": 5,
- "type": "varchar",
- "collation": "utf8mb4_polish_ci"
+ "name": "f",
+ "position": 5,
+ "type": "varchar",
+ "collation": "utf8mb4_polish_ci"
},
{
- "name": "g",
- "position": 6,
- "type": "varchar",
- "characterSet": "utf8mb4"
+ "name": "g",
+ "position": 6,
+ "type": "varchar",
+ "characterSet": "utf8mb4"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "type": "BTREE",
- "unique": true,
- "primary": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "primary": true,
+ "visible": true
},
{
- "name": "b",
- "expressions": [
+ "name": "b",
+ "expressions": [
"b"
],
- "type": "BTREE",
- "unique": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
}
]
}
diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
index 76b2d6d75f..71c876de42 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
@@ -162,6 +162,42 @@ func (l *mysqlV2Listener) EnterCreateTable(ctx *mysql.CreateTableContext) {
}
}
+// EnterDropTable is called when production dropTable is entered.
+func (l *mysqlV2Listener) EnterDropTable(ctx *mysql.DropTableContext) {
+ if ctx.TableRefList() == nil {
+ return
+ }
+
+ for _, tableRef := range ctx.TableRefList().AllTableRef() {
+ databaseName, tableName := mysqlparser.NormalizeMySQLTableRef(tableRef)
+ if databaseName != "" && !l.databaseState.isCurrentDatabase(databaseName) {
+ l.err = &WalkThroughError{
+ Type: ErrorTypeAccessOtherDatabase,
+ Content: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, tableName),
+ }
+ }
+
+ schema, exists := l.databaseState.schemaSet[""]
+ if !exists {
+ schema = l.databaseState.createSchema("")
+ }
+
+ table, exists := schema.getTable(tableName)
+ if !exists {
+ if ctx.IfExists() != nil || !l.databaseState.ctx.CheckIntegrity {
+ return
+ }
+ l.err = &WalkThroughError{
+ Type: ErrorTypeTableNotExists,
+ Content: fmt.Sprintf("Table `%s` does not exist", tableName),
+ }
+ return
+ }
+
+ delete(schema.tableSet, table.name)
+ }
+}
+
func (d *DatabaseState) mysqlV2CopyTable(databaseName, tableName, referTable string) *WalkThroughError {
targetTable, err := d.mysqlV2FindTableState(databaseName, referTable, true /* createIncompleteTable */)
if err != nil {