3_sp_data_migration_wcmtemplatenest.sql 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. DROP PROCEDURE IF EXISTS sp_data_migration_wcmtemplatenest;
  2. DELIMITER $$
  3. CREATE PROCEDURE sp_data_migration_wcmtemplatenest(IN prevTemplateNestId BIGINT(20),
  4. IN templateNestIdOffset BIGINT(20))
  5. BEGIN
  6. DECLARE isover INT DEFAULT 0;
  7. DECLARE migr_table_name varchar(255) DEFAULT 'wcmtemplatenest';
  8. DECLARE template_migr_table_name varchar(255) DEFAULT 'wcmtemplate';
  9. DECLARE f_TEMPLATEID int(11) DEFAULT 0;
  10. DECLARE f_NESTEDTEMPLATEID int(11) DEFAULT 0;
  11. DECLARE f_TEMPLATENESTID int(11) DEFAULT 0;
  12. DECLARE cur CURSOR FOR
  13. SELECT
  14. TEMPLATEID,
  15. NESTEDTEMPLATEID,
  16. TEMPLATENESTID
  17. FROM wcmtemplatenest
  18. ORDER BY TEMPLATENESTID asc;
  19. DECLARE CONTINUE HANDLER FOR NOT FOUND SET isover = 1;
  20. OPEN cur;
  21. FETCH cur INTO
  22. f_TEMPLATEID,
  23. f_NESTEDTEMPLATEID,
  24. f_TEMPLATENESTID
  25. ;
  26. select concat('开始迁移 ', migr_table_name) info;
  27. WHILE isover= 0 DO
  28. # 断点续传
  29. IF (f_TEMPLATENESTID > prevTemplateNestId)
  30. THEN
  31. SET @TEMPLATENESTID = f_TEMPLATENESTID;
  32. SET f_TEMPLATENESTID = f_TEMPLATENESTID + templateNestIdOffset;
  33. SET @template_offset_num = ifnull((SELECT offset_num FROM data_migration_offset WHERE table_name = template_migr_table_name AND start_id < f_TEMPLATEID and end_id >= f_TEMPLATEID), 0);
  34. SET f_TEMPLATEID = f_TEMPLATEID + @template_offset_num;
  35. SET @nest_offset_num = ifnull((SELECT offset_num FROM data_migration_offset WHERE table_name = migr_table_name AND start_id < f_NESTEDTEMPLATEID and end_id >= f_NESTEDTEMPLATEID), templateNestIdOffset);
  36. SET f_NESTEDTEMPLATEID = f_NESTEDTEMPLATEID + @nest_offset_num;
  37. insert into trs_hycloud_iip.wcmtemplatenest
  38. (
  39. TEMPLATEID,
  40. NESTEDTEMPLATEID,
  41. TEMPLATENESTID
  42. )
  43. VALUES
  44. (
  45. f_TEMPLATEID,
  46. f_NESTEDTEMPLATEID,
  47. f_TEMPLATENESTID
  48. );
  49. SET @log_sql = build_migration_log_sql(migr_table_name, @TEMPLATENESTID, now());
  50. prepare stmt from @log_sql;
  51. EXECUTE stmt;
  52. deallocate prepare stmt;
  53. call setOffset(migr_table_name, templateNestIdOffset, prevTemplateNestId, @TEMPLATENESTID);
  54. END IF ;
  55. FETCH cur INTO
  56. f_TEMPLATEID,
  57. f_NESTEDTEMPLATEID,
  58. f_TEMPLATENESTID
  59. ;
  60. END WHILE;
  61. CLOSE cur;
  62. select concat('完成迁移 ', migr_table_name) info;
  63. END $$
  64. DELIMITER ;