github.com/kaydxh/golang@v0.0.131/pkg/database/mysql/migrate/rename_db.sql (about)

     1  delimiter ;;
     2  CREATE DATABASE /*!32312 IF NOT EXISTS*/ `new_db_name` /*!40100 DEFAULT CHARACTER SET utf8*/;
     3  
     4  USE `new_db_name`;
     5  
     6  SET @queryCount = (
     7  SELECT COUNT(*) FROM information_schema.TABLES
     8  WHERE table_schema IN ('old_db_name'));
     9  
    10  drop procedure  if exists test_move;
    11  CREATE PROCEDURE test_move ()
    12  BEGIN
    13  
    14  DECLARE i INT DEFAULT 0;
    15  WHILE i < @queryCount 
    16  DO
    17  	SET @queryString := (
    18  	SELECT CONCAT('RENAME TABLE ',table_schema,'.',table_name, ' TO ','new_db_name.',table_name,';')
    19  		FROM information_schema.TABLES WHERE table_schema IN ('old_db_name') ORDER BY table_name LIMIT 1 );
    20  	PREPARE stmt FROM @queryString;
    21  	select @queryString as ``;
    22  	select i as ``;
    23  	EXECUTE stmt;
    24  	SET i=i+1;
    25  END WHILE ;
    26  commit;
    27  
    28  END;;
    29  delimiter ;
    30  CALL test_move();
    31