示例
把tb_policy的某些数据migration到新表中
DELIMITER $$USE `hesui`$$start transaction$$DROP PROCEDURE if EXISTS proc_sender_exception_migration$$CREATE PROCEDURE proc_sender_exception_migration()BEGINDECLARE splitChar VARCHAR(2) DEFAULT ';';DECLARE tmp VARCHAR(255) DEFAULT '';DECLARE cnt INT DEFAULT 0;DECLARE i INT DEFAULT 0;DECLARE p_id INT DEFAULT 0;DECLARE s_except LONGTEXT DEFAULT '';DECLARE done INT DEFAULT 0;DECLARE sender_except_cursor CURSORFORSELECT policy_id, sender_exceptFROM tb_policyWHERE sender_except IS NOT NULLAND sender_except != '';DECLARE continue handler FOR NOT FOUND SET done = 1;OPEN sender_except_cursor;start_loop: loopfetch sender_except_cursor INTO p_id, s_except;if done = 1 thenleave start_loop;END if;SET cnt = 1+(LENGTH(s_except) - LENGTH(REPLACE(s_except,splitChar,'')));SET i = 0;while i < cnt doSET i = i + 1;SET tmp = reverse(substring_index(reverse(substring_index(s_except,splitChar,i)),splitChar,1));if tmp IS NOT NULL OR tmp != '' thenINSERT INTO tb_policy_target_exception(policy_id, sender_except, rcpt_except) VALUES (p_id, tmp, '*');END if;END while;END loop;END$$call proc_sender_exception_migration();$$DROP PROCEDURE if EXISTS proc_rcpt_exception_migration$$CREATE PROCEDURE proc_rcpt_exception_migration()BEGINDECLARE splitChar VARCHAR(2) DEFAULT ';';DECLARE tmp VARCHAR(255) DEFAULT '';DECLARE cnt INT DEFAULT 0;DECLARE i INT DEFAULT 0;DECLARE p_id INT DEFAULT 0;DECLARE r_except LONGTEXT DEFAULT '';DECLARE done INT DEFAULT 0;DECLARE rcpt_except_cursor CURSORFORSELECT policy_id, rcpt_exceptFROM tb_policyWHERE rcpt_except IS NOT NULLAND rcpt_except != '';DECLARE continue handler FOR NOT FOUND SET done = 1;OPEN rcpt_except_cursor;start_loop: loopfetch rcpt_except_cursor INTO p_id, r_except;if done = 1 thenleave start_loop;END if;SET cnt = 1+(LENGTH(r_except) - LENGTH(REPLACE(r_except,splitChar,'')));SET i = 0;while i < cnt doSET i = i + 1;SET tmp = reverse(substring_index(reverse(substring_index(r_except,splitChar,i)),splitChar,1));if tmp IS NOT NULL OR tmp != '' thenINSERT INTO tb_policy_target_exception(policy_id, sender_except, rcpt_except) VALUES (p_id, '*', tmp);END if;END while;END loop;END$$call proc_rcpt_exception_migration();$$drop procedure if exists group_policy_relation_migration$$CREATE procedure group_policy_relation_migration()BEGINdeclare done INT DEFAULT 0;declare policy_id int(10);declare gpr_id int(10);DECLARE exception_id INT(10);declare gpr_cursor_rcpt CURSORFORselect gpr.policy_id, gpr.idfrom tb_ldap_group_policy_relation gpr, tb_policywhere gpr.policy_id = tb_policy.policy_idand gpr.include = 0and tb_policy.direction = 0;declare gpr_cursor_sender CURSORFORselect gpr.policy_id, gpr.idfrom tb_ldap_group_policy_relation gpr, tb_policywhere gpr.policy_id = tb_policy.policy_idand gpr.include = 0and tb_policy.direction = 1;declare CONTINUE HANDLER FOR NOT FOUND SET done = 1;open gpr_cursor_rcpt;start_loop : loopfetch gpr_cursor_rcpt into policy_id, gpr_id;if done=1 thenleave start_loop;end if;insert into tb_policy_target_exception(policy_id, sender_except) values(policy_id, '*');select max(id) from tb_policy_target_exception into exception_id;update tb_ldap_group_policy_relation set exception_id = exception_id where id = gpr_id;end loop;open gpr_cursor_sender;start_loop : loopfetch gpr_cursor_sender into policy_id, gpr_id;if done=1 thenleave start_loop;end if;insert into tb_policy_target_exception(policy_id, rcpt_except) values(policy_id, '*');select max(id) from tb_policy_target_exception into exception_id;update tb_ldap_group_policy_relation set exception_id = exception_id where id = gpr_id;end loop;END$$call group_policy_relation_migration();$$commit$$DELIMITER ;
