Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore(mysqlv2): support rename table (#9488)
zll600ba02ebc91308
diff --git a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yamlindex 194aa2e96e..daca0e730c 100644--- a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml+++ b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml@@ -1,3 +1,73 @@+- statement: |-+ CREATE TABLE `test`.`t1` (a INT);+ CREATE TABLE `other`.`t1` (a INT);+ RENAME TABLE `other`.`t1` TO `other`.`t2`;+ ignore_case_sensitive: false+ want: ""+ err:+ type: 201+ content: Database `other` is not the current database `test`+ line: 0+ payload: null+- statement: |-+ CREATE TABLE `test`.`t1` (a INT);+ RENAME TABLE `test`.`t1` TO `other`.`t1`;+ ignore_case_sensitive: false+ want: |-+ {+ "name": "test",+ "schemas": [+ {}+ ]+ }+ err: null+- statement: |-+ CREATE TABLE t1(a INT);+ CREATE TABLE t2(a INT);+ RENAME TABLE t2 TO t1;+ ignore_case_sensitive: false+ want: ""+ err:+ type: 301+ content: Table `t1` already exists+ line: 1+ payload: null+- statement: |-+ CREATE TABLE t1(a INT);+ RENAME TABLE t2 TO t3;+ ignore_case_sensitive: false+ want: ""+ err:+ type: 302+ content: Table `t2` does not exist+ line: 0+ payload: null+- statement: |-+ CREATE TABLE t1 (a INT);+ RENAME TABLE t1 TO t2;+ ignore_case_sensitive: false+ want: |-+ {+ "name": "test",+ "schemas": [+ {+ "tables": [+ {+ "name": "t2",+ "columns": [+ {+ "name": "a",+ "position": 1,+ "nullable": true,+ "type": "int"+ }+ ]+ }+ ]+ }+ ]+ }+ err: null- statement: CREATE DATABASE another;ignore_case_sensitive: falsewant: ""@@ -44,22 +114,22 @@ignore_case_sensitive: falsewant: |-{- "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": "c",- "position": 2,- "type": "varchar"+ "name": "c",+ "position": 2,+ "type": "varchar"}]}@@ -173,49 +243,49 @@ignore_case_sensitive: falsewant: |-{- "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,- "nullable": true,- "type": "int"+ "name": "b",+ "position": 2,+ "nullable": true,+ "type": "int"}],- "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": "uk1",- "expressions": [+ "name": "uk1",+ "expressions": ["b"],- "type": "BTREE",- "unique": true,- "visible": true+ "type": "BTREE",+ "unique": true,+ "visible": true}],- "engine": "MyISAM",- "collation": "utf8mb4_unicode_ci",- "comment": "t1 table"+ "engine": "MyISAM",+ "collation": "utf8mb4_unicode_ci",+ "comment": "t1 table"}]}@@ -233,23 +303,23 @@ignore_case_sensitive: falsewant: |-{- "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,- "nullable": true,- "type": "int"+ "name": "b",+ "position": 2,+ "nullable": true,+ "type": "int"}]}@@ -272,42 +342,42 @@ignore_case_sensitive: falsewant: |-{- "name": "test",- "schemas": [+ "name": "test",+ "schemas": [{- "tables": [+ "tables": [{- "name": "t2",- "columns": [+ "name": "t2",+ "columns": [{- "name": "a",- "position": 1,- "nullable": true,- "type": "int"+ "name": "a",+ "position": 1,+ "nullable": true,+ "type": "int"},{- "name": "b",- "position": 2,- "nullable": true,- "type": "int"+ "name": "b",+ "position": 2,+ "nullable": true,+ "type": "int"}],- "indexes": [+ "indexes": [{- "name": "idx2",- "expressions": [+ "name": "idx2",+ "expressions": ["b"],- "type": "BTREE"+ "type": "BTREE"},{- "name": "uk_b",- "expressions": [+ "name": "uk_b",+ "expressions": ["b"],- "type": "BTREE",- "unique": true,- "visible": true+ "type": "BTREE",+ "unique": true,+ "visible": true}]}@@ -344,18 +414,18 @@ignore_case_sensitive: falsewant: |-{- "name": "test",- "schemas": [+ "name": "test",+ "schemas": [{- "tables": [+ "tables": [{- "name": "t1",- "columns": [+ "name": "t1",+ "columns": [{- "name": "a",- "position": 1,- "nullable": true,- "type": "blob"+ "name": "a",+ "position": 1,+ "nullable": true,+ "type": "blob"}]}@@ -377,30 +447,30 @@ignore_case_sensitive: falsewant: |-{- "name": "test",- "schemas": [+ "name": "test",+ "schemas": [{- "tables": [+ "tables": [{- "name": "t1",- "columns": [- {- "name": "a",- "position": 1,- "default": "1",- "nullable": true,- "type": "int"+ "name": "t1",+ "columns": [+ {+ "name": "a",+ "position": 1,+ "default": "1",+ "nullable": true,+ "type": "int"},{- "name": "d",- "position": 2,- "nullable": true,- "type": "text"+ "name": "d",+ "position": 2,+ "nullable": true,+ "type": "text"},{- "name": "c",- "position": 3,- "type": "int"+ "name": "c",+ "position": 3,+ "type": "int"}]}@@ -422,30 +492,30 @@ignore_case_sensitive: falsewant: |-{- "name": "test",- "schemas": [+ "name": "test",+ "schemas": [{- "tables": [+ "tables": [{- "name": "t1",- "columns": [+ "name": "t1",+ "columns": [{- "name": "e",- "position": 1,- "nullable": true,- "type": "int"+ "name": "e",+ "position": 1,+ "nullable": true,+ "type": "int"},{- "name": "f",- "position": 2,- "nullable": true,- "type": "text"+ "name": "f",+ "position": 2,+ "nullable": true,+ "type": "text"},{- "name": "c",- "position": 3,- "nullable": true,- "type": "int"+ "name": "c",+ "position": 3,+ "nullable": true,+ "type": "int"}]}@@ -462,18 +532,18 @@ignore_case_sensitive: falsewant: |-{- "name": "test",- "schemas": [+ "name": "test",+ "schemas": [{- "tables": [+ "tables": [{- "name": "t3",- "columns": [+ "name": "t3",+ "columns": [{- "name": "a",- "position": 1,- "nullable": true,- "type": "int"+ "name": "a",+ "position": 1,+ "nullable": true,+ "type": "int"}]}@@ -520,29 +590,29 @@ignore_case_sensitive: falsewant: |-{- "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"}]}@@ -565,78 +635,78 @@ignore_case_sensitive: falsewant: |-{- "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}]}@@ -690,70 +760,70 @@ignore_case_sensitive: falsewant: |-{- "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.goindex 38795503d8..53745d48d6 100644--- a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go@@ -492,6 +492,80 @@ func (l *mysqlV2Listener) EnterCreateDatabase(ctx *mysql.CreateDatabaseContext)l.err = NewAccessOtherDatabaseError(l.databaseState.name, databaseName)}+// EnterRenameTableStatement is called when production renameTableStatement is entered.+func (l *mysqlV2Listener) EnterRenameTableStatement(ctx *mysql.RenameTableStatementContext) {+ for _, pair := range ctx.AllRenamePair() {+ schema, exists := l.databaseState.schemaSet[""]+ if !exists {+ schema = l.databaseState.createSchema("")+ }++ _, oldTableName := mysqlparser.NormalizeMySQLTableRef(pair.TableRef())+ _, newTableName := mysqlparser.NormalizeMySQLTableName(pair.TableName())++ if l.databaseState.mysqlV2TheCurrentDatabase(pair) {+ if compareIdentifier(oldTableName, newTableName, l.databaseState.ctx.IgnoreCaseSensitive) {+ return+ }+ table, exists := schema.getTable(oldTableName)+ if !exists {+ if schema.ctx.CheckIntegrity {+ l.err = NewTableNotExistsError(oldTableName)+ return+ }+ table = schema.createIncompleteTable(oldTableName)+ }+ if _, exists := schema.getTable(newTableName); exists {+ l.err = NewTableExistsError(newTableName)+ return+ }+ delete(schema.tableSet, table.name)+ table.name = newTableName+ schema.tableSet[table.name] = table+ } else if l.databaseState.mysqlV2MoveToOtherDatabase(pair) {+ _, exists := schema.getTable(oldTableName)+ if !exists && schema.ctx.CheckIntegrity {+ l.err = NewTableNotExistsError(oldTableName)+ return+ }+ delete(schema.tableSet, oldTableName)+ } else {+ l.err = NewAccessOtherDatabaseError(l.databaseState.name, l.databaseState.mysqlV2TargetDatabase(pair))+ return+ }+ }+}++func (d *DatabaseState) mysqlV2TargetDatabase(renamePair mysql.IRenamePairContext) string {+ oldDatabaseName, _ := mysqlparser.NormalizeMySQLTableRef(renamePair.TableRef())+ if oldDatabaseName != "" && !d.isCurrentDatabase(oldDatabaseName) {+ return oldDatabaseName+ }+ newDatabaseName, _ := mysqlparser.NormalizeMySQLTableName(renamePair.TableName())+ return newDatabaseName+}++func (d *DatabaseState) mysqlV2MoveToOtherDatabase(renamePair mysql.IRenamePairContext) bool {+ oldDatabaseName, _ := mysqlparser.NormalizeMySQLTableRef(renamePair.TableRef())+ if oldDatabaseName != "" && !d.isCurrentDatabase(oldDatabaseName) {+ return false+ }+ newDatabaseName, _ := mysqlparser.NormalizeMySQLTableName(renamePair.TableName())+ return oldDatabaseName != newDatabaseName+}++func (d *DatabaseState) mysqlV2TheCurrentDatabase(renamePair mysql.IRenamePairContext) bool {+ newDatabaseName, _ := mysqlparser.NormalizeMySQLTableName(renamePair.TableName())+ if newDatabaseName != "" && !d.isCurrentDatabase(newDatabaseName) {+ return false+ }+ oldDatabaseName, _ := mysqlparser.NormalizeMySQLTableRef(renamePair.TableRef())+ if oldDatabaseName != "" && !d.isCurrentDatabase(oldDatabaseName) {+ return false+ }+ return true+}+func (t *TableState) mysqlV2ChangeIndexVisibility(ctx *FinderContext, indexName string, visibility mysql.IVisibilityContext) *WalkThroughError {index, exists := t.indexSet[indexName]if !exists {