drop table if exists t0,t1,t2,t3,t4; drop table if exists t0,t5,t6,t7,t8,t9,t1_1,t1_2,t9_1,t9_2; create table t0 SELECT 1,"table 1"; create table t2 SELECT 2,"table 2"; create table t3 SELECT 3,"table 3"; rename table t0 to t1; rename table t3 to t4, t2 to t3, t1 to t2, t4 to t1; select * from t1; 3 table 3 3 table 3 rename table t3 to t4, t2 to t3, t1 to t2, t4 to t1; rename table t3 to t4, t2 to t3, t1 to t2, t4 to t1; select * from t1; 1 table 1 1 table 1 rename table t1 to t2; Got one of the listed errors rename table t1 to t1; Got one of the listed errors rename table t3 to t4, t2 to t3, t1 to t2, t4 to t2; Got one of the listed errors show tables like "t_"; Tables_in_test (t_) t1 t2 t3 rename table t3 to t1, t2 to t3, t1 to t2, t4 to t1; Got one of the listed errors rename table t3 to t4, t5 to t3, t1 to t2, t4 to t1; Got one of the listed errors select * from t1; 1 table 1 1 table 1 select * from t2; 2 table 2 2 table 2 select * from t3; 3 table 3 3 table 3 drop table if exists t1,t2,t3,t4; Warnings: Note 1051 Unknown table 'test.t4' CREATE TABLE t1 (a int); CREATE TABLE t3 (a int); FLUSH TABLES WITH READ LOCK; RENAME TABLE t1 TO t2, t3 to t4; show tables; Tables_in_test t1 t3 UNLOCK TABLES; show tables; Tables_in_test t2 t4 drop table t2, t4; End of 4.1 tests # # Bug#14959: "ALTER TABLE isn't able to rename a view" # Bug#53976: "ALTER TABLE RENAME is allowed on views # (not documented, broken)" # create table t1(f1 int); create view v1 as select * from t1; alter table v1 rename to v2; ERROR HY000: 'test.v1' is not BASE TABLE drop view v1; drop table t1; End of 5.0 tests # # Bug#20197870: sql_table.cc: bool mysql_rename_table ... # assertion `error==0\' failed. # The test crashes server without the fix. # SET @orig_innodb_file_per_table= @@innodb_file_per_table; SET GLOBAL innodb_file_per_table = 0; create table t1(f1 int) engine=innodb; rename table test.t1 to nonexistingdb.t2; ERROR 42000: Unknown database 'nonexistingdb' drop table t1; SET GLOBAL innodb_file_per_table = @orig_innodb_file_per_table; # # Test coverage for WL#9826 "Allow RENAME TABLES under LOCK TABLES". # SET @old_lock_wait_timeout= @@lock_wait_timeout; connect con1, localhost, root,,; SET @old_lock_wait_timeout= @@lock_wait_timeout; connection default; # # 1) Requirements on table locking for tables renamed and # target table names. # # 1.1) Requirements on renamed table locks. CREATE TABLE t1 (i INT); CREATE TABLE t2 (j INT); LOCK TABLES t1 READ; # Renamed table must be locked. RENAME TABLE t2 TO t3; ERROR HY000: Table 't2' was not locked with LOCK TABLES # Moreover, renamed table must be locked for write. RENAME TABLE t1 TO t3; ERROR HY000: Table 't1' was locked with a READ lock and can't be updated UNLOCK TABLES; LOCK TABLE t1 WRITE; # Renaming write-locked table is OK, there is no need # (and way) to lock target table name. RENAME TABLE t1 TO t3; # There is no need to lock source name for the table, if it is # result of some earlier step of the same RENAME TABLES. RENAME TABLE t3 TO t4, t4 TO t5; UNLOCK TABLES; # Special case involving foreign keys. If RENAME TABLE is # going to provide parent for some orphan FK, then the # child table of this FK needs to be write locked, or # FK invariants for LOCK TABLES will be broken, SET FOREIGN_KEY_CHECKS=0; CREATE TABLE t1 (fk INT, FOREIGN KEY(fk) REFERENCES t3(pk)); SET FOREIGN_KEY_CHECKS=1; CREATE TABLE t0 (pk INT PRIMARY KEY); LOCK TABLES t0 WRITE; RENAME TABLE t0 TO t3; ERROR HY000: Table 't1' was not locked with LOCK TABLES UNLOCK TABLES; LOCK TABLES t1 READ, t0 WRITE; RENAME TABLE t0 TO t3; ERROR HY000: Table 't1' was locked with a READ lock and can't be updated UNLOCK TABLES; LOCK TABLES t1 WRITE, t0 WRITE; RENAME TABLE t0 TO t3; UNLOCK TABLES; DROP TABLES t1, t3; # # 1.2) Locking of target table. # # This part of test resides in rename_debug.test. # # 2) Failure to acquire/upgrade locks on tables involved. # # This part of test resides in rename_debug.test. # # 3) Effects of RENAME TABLES on set of locked tables and # metadata locks held. # # 3.1) Trivial RENAME TABLE. LOCK TABLES t5 WRITE; RENAME TABLE t5 TO t4; # Table is available under new name under LOCK TABLES. SELECT * FROM t4; i connection con1; # Access by new name from other connections should be blocked. SET @@lock_wait_timeout= 1; SELECT * FROM t4; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SET @@lock_wait_timeout= @old_lock_wait_timeout; # But not for old table name. SELECT * FROM t5; ERROR 42S02: Table 'test.t5' doesn't exist connection default; UNLOCK TABLES; # # 3.2) RENAME TABLE in case when several tables are locked. LOCK TABLES t2 READ, t4 WRITE; RENAME TABLE t4 TO t5; # Table t2 should be still locked, and t4 should be available as t5 # with correct lock type. SELECT * FROM t2; j INSERT INTO t5 values (1); UNLOCK TABLES; # # 3.3) RENAME TABLE in case when same table locked more than once. LOCK TABLES t2 READ, t5 WRITE, t5 AS a WRITE, t5 AS b READ; RENAME TABLE t5 TO t4; # Check that tables are locked under correct aliases and with modes. SELECT * FROM t4 AS a, t4 AS b; i i 1 1 INSERT INTO t4 VALUES (2); DELETE a FROM t4 AS a, t4 AS b; DELETE b FROM t4 AS a, t4 AS b; ERROR HY000: Table 'b' was locked with a READ lock and can't be updated UNLOCK TABLES; # # 3.4) RENAME TABLES which does old good table swap. LOCK TABLES t2 WRITE, t4 WRITE; RENAME TABLES t2 TO t0, t4 TO t2, t0 TO t4; # Tables are available under new name under LOCK TABLES. SELECT * FROM t2; i SELECT * FROM t4; j connection con1; # Access by new names from other connections should be blocked. SET @@lock_wait_timeout= 1; SELECT * FROM t2; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SELECT * FROM t4; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SET @@lock_wait_timeout= @old_lock_wait_timeout; # But not for auxiliary name. SELECT * FROM t0; ERROR 42S02: Table 'test.t0' doesn't exist connection default; UNLOCK TABLES; # # 3.5) RENAME TABLES which renames same table several times. LOCK TABLE t2 WRITE; RENAME TABLES t2 TO t1, t1 TO t3, t3 TO t5; # Table is available under new name under LOCK TABLES. SELECT * FROM t5; i # But not under other names. SELECT * FROM t1; ERROR HY000: Table 't1' was not locked with LOCK TABLES SELECT * FROM t2; ERROR HY000: Table 't2' was not locked with LOCK TABLES SELECT * FROM t3; ERROR HY000: Table 't3' was not locked with LOCK TABLES connection con1; # Access by new name from other connections should be blocked. SET @@lock_wait_timeout= 1; SELECT * FROM t5; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SET @@lock_wait_timeout= @old_lock_wait_timeout; # But not for other names SELECT * FROM t1; ERROR 42S02: Table 'test.t1' doesn't exist SELECT * FROM t2; ERROR 42S02: Table 'test.t2' doesn't exist SELECT * FROM t3; ERROR 42S02: Table 'test.t3' doesn't exist connection default; UNLOCK TABLES; # # 3.6) RENAME TABLES which renames 2 tables while additional # table is locked. CREATE TABLE t6(k INT); LOCK TABLES t4 WRITE, t5 WRITE, t6 WRITE; RENAME TABLES t4 TO t1, t5 TO t2; # Renamed tables are available under new names. SELECT * FROM t1; j SELECT * FROM t2; i # But not under other names. SELECT * FROM t4; ERROR HY000: Table 't4' was not locked with LOCK TABLES SELECT * FROM t5; ERROR HY000: Table 't5' was not locked with LOCK TABLES # Untouched table is still available. SELECT * FROM t6; k connection con1; # Access by new name from other connections should be blocked. SET @@lock_wait_timeout= 1; SELECT * FROM t1; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SELECT * FROM t2; ERROR HY000: Lock wait timeout exceeded; try restarting transaction # And access to untouched table too. SELECT * FROM t6; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SET @@lock_wait_timeout= @old_lock_wait_timeout; # Old names should not be locked. SELECT * FROM t4; ERROR 42S02: Table 'test.t4' doesn't exist SELECT * FROM t5; ERROR 42S02: Table 'test.t5' doesn't exist connection default; UNLOCK TABLES; DROP TABLES t1, t2, t6; # # 4) Effects of failed RENAME TABLES on set of locked tables and # metadata locks held. # # 4.1) Atomic RENAME TABLES which fails at late stage should be # fully rolled back. CREATE TABLE t1 (i INT) ENGINE=InnoDB; CREATE TABLE t2 (j INT) ENGINE=InnoDB; CREATE TABLE t3 (k INT) ENGINE=InnoDB; CREATE TABLE t4 (l INT) ENGINE=InnoDB; LOCK TABLES t1 WRITE, t2 WRITE, t3 WRITE; RENAME TABLES t1 TO t0, t2 TO t4; ERROR 42S01: Table 't4' already exists # Tables are available under old names. SELECT * FROM t1; i SELECT * FROM t2; j # Including untouched table. SELECT * FROM t3; k # And not under new names. SELECT * FROM t0; ERROR HY000: Table 't0' was not locked with LOCK TABLES SELECT * FROM t4; ERROR HY000: Table 't4' was not locked with LOCK TABLES connection con1; # Access by old names from other connections should be blocked. SET @@lock_wait_timeout= 1; SELECT * FROM t1; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SELECT * FROM t2; ERROR HY000: Lock wait timeout exceeded; try restarting transaction # And access to untouched table too. SELECT * FROM t3; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SET @@lock_wait_timeout= @old_lock_wait_timeout; # New names should not be locked. SELECT * FROM t0; ERROR 42S02: Table 'test.t0' doesn't exist SELECT * FROM t4; l connection default; UNLOCK TABLES; DROP TABLES t1, t2, t3, t4; # # 4.3) Non-atomic RENAME TABLES which fails at late stage and # is NOT fully reverted. # # This part of test resides in rename_debug.test. # # 5) RENAME TABLES under LOCK TABLES and views. # # 5.1) Requirements on locking is similar to tables. CREATE TABLE t1 (i INT); CREATE TABLE t2 (j INT); CREATE VIEW v1 AS SELECT * FROM t1; CREATE VIEW v2 AS SELECT * FROM t2; LOCK TABLES t1 WRITE; RENAME TABLE v1 TO v2; ERROR HY000: Table 'v1' was not locked with LOCK TABLES UNLOCK TABLES; LOCK TABLES v1 READ; RENAME TABLE v1 TO v2; ERROR HY000: Table 'v1' was locked with a READ lock and can't be updated UNLOCK TABLES; # # Coverage for target name resides in rename_debug.test. # # 5.2) Effects of RENAME TABLE on a view on the # set of locked tables and metadata locks. LOCK TABLES v1 WRITE; RENAME TABLE v1 TO v3; # View is available under new name under LOCK TABLES. INSERT INTO v3 VALUES (1); # And not under old name. SELECT * FROM v1; ERROR HY000: Table 'v1' was not locked with LOCK TABLES connection con1; # Access by new name from other connections should be blocked. SET @@lock_wait_timeout= 1; SELECT * FROM v3; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SET @@lock_wait_timeout= @old_lock_wait_timeout; # But not for old table name. SELECT * FROM v1; ERROR 42S02: Table 'test.v1' doesn't exist connection default; UNLOCK TABLES; # # A bit more complex RENAME TABLE which swaps two views. LOCK TABLES v2 WRITE, v3 WRITE; RENAME TABLE v2 TO v0, v3 TO v2, v0 TO v3; # Views are available under new name under LOCK TABLES. SELECT * FROM v2; i 1 SELECT * FROM v3; j connection con1; # Access by new names from other connections should be blocked. SET @@lock_wait_timeout= 1; SELECT * FROM v2; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SELECT * FROM v3; ERROR HY000: Lock wait timeout exceeded; try restarting transaction SET @@lock_wait_timeout= @old_lock_wait_timeout; # But not for auxiliary name. SELECT * FROM v0; ERROR 42S02: Table 'test.v0' doesn't exist connection default; UNLOCK TABLES; DROP VIEW v2; DROP VIEW v3; DROP TABLES t1, t2; # # 6) RENAME TABLES under LOCK TABLES and foreign keys. # # 6.1) Renaming of child table updates parent definition. CREATE TABLE t1 (pk INT PRIMARY KEY); INSERT INTO t1 VALUES (1); CREATE TABLE t2 (fk INT, FOREIGN KEY (fk) REFERENCES t1 (pk)); INSERT INTO t2 VALUES (1); CREATE TABLE t3 (fk INT); CREATE TABLE t4 (pk INT NOT NULL, UNIQUE(pk)); INSERT INTO t4 VALUES (2); LOCK TABLES t1 WRITE, t2 WRITE, t3 WRITE, t4 WRITE; RENAME TABLES t2 TO t0, t3 TO t2, t0 TO t3; # The above RENAME TABLES should have updated parent definition # so below DELETE goes to new 't3' (old 't2') when checking if # rows can be deleted. DELETE FROM t1; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t3`, CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `t1` (`pk`)) # # 6.2) Renaming of parent table updates parent definition as well. RENAME TABLE t1 TO t0, t4 TO t1, t0 TO t4; # The above RENAME TABLES should have updated child definition # so below INSERT goes to new 't4' (old 't1') when checking if # row can be inserted. INSERT INTO t3 VALUES (2); ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t3`, CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `t4` (`pk`)) SHOW CREATE TABLE t3; Table Create Table t3 CREATE TABLE `t3` ( `fk` int(11) DEFAULT NULL, KEY `fk` (`fk`), CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `t4` (`pk`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci # Parent key name still should be PRIMARY. SELECT unique_constraint_name FROM information_schema.referential_constraints WHERE table_name = 't3'; UNIQUE_CONSTRAINT_NAME PRIMARY UNLOCK TABLES; # # 6.3) RENAME TABLES which adds parent to orphan FK updates # child definition. # Make FK on t3 orphan. SET foreign_key_checks = 0; DROP TABLES t1, t2, t4; SET foreign_key_checks = 1; CREATE TABLE t1 (pk INT NOT NULL, UNIQUE(pk)); INSERT INTO t1 VALUES (1), (2); LOCK TABLES t1 WRITE, t3 WRITE; # Add parent for orphan FK. RENAME TABLE t1 TO t4; # Child definition should be updated to reflect new parent. INSERT INTO t3 VALUES (2); INSERT INTO t3 VALUES (3); ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t3`, CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `t4` (`pk`)) # Parent key name should be 'pk'. SELECT unique_constraint_name FROM information_schema.referential_constraints WHERE table_name = 't3'; UNIQUE_CONSTRAINT_NAME pk # Parent definition should be aware of new child as well. DELETE FROM t4; ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t3`, CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `t4` (`pk`)) UNLOCK TABLES; DROP TABLE t3, t4; # Clean-up. connection con1; disconnect con1; connection default;