diff --git a/cdc/model/sink.go b/cdc/model/sink.go index cc33d2b1008..8191465b32b 100644 --- a/cdc/model/sink.go +++ b/cdc/model/sink.go @@ -1092,6 +1092,15 @@ func (d *DDLEvent) FromJobWithArgs( case model.ActionDropView: d.Query = fmt.Sprintf("DROP VIEW `%s`.`%s`", d.TableInfo.TableName.Schema, d.TableInfo.TableName.Table) + case model.ActionRenameTable: + // Note: preTableInfo may not be accurate for rename table. + // after pr: https://github.com/pingcap/tidb/pull/43341, + // assume there is a table `test.t` and a ddl: `rename table t to test2.t;`, and its commit ts is `100`. + // if you get a ddl snapshot at ts `99`, table `t` is already in `test2`. + // so preTableInfo.TableName.Schema will also be `test2`. + d.Query = fmt.Sprintf("RENAME TABLE `%s`.`%s` TO `%s`.`%s`", + job.InvolvingSchemaInfo[0].Database, job.InvolvingSchemaInfo[0].Table, + tableInfo.TableName.Schema, tableInfo.TableName.Table) case model.ActionRenameTables: oldTableName := preTableInfo.Name.O newTableName := tableInfo.Name.O diff --git a/tests/integration_tests/ddl_manager/data/prepare.sql b/tests/integration_tests/ddl_manager/data/prepare.sql index 444106d9dff..69443c69faa 100644 --- a/tests/integration_tests/ddl_manager/data/prepare.sql +++ b/tests/integration_tests/ddl_manager/data/prepare.sql @@ -113,7 +113,7 @@ CREATE TABLE t1 ( col0 INT NOT NULL ); -RENAME TABLE `cross_db_1`.`t1` TO `cross_db_2`.`t1`; +RENAME TABLE `t1` TO `cross_db_2`.`t1`; CREATE TABLE `cross_db_1`.`t2` like `cross_db_2`.`t1`;