feat: shard tenant topic and membership tables
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
-- Existing-merchant migration for topics, group members, subscriptions and system notifications.
|
||||
-- Run against meeting_im. The script is idempotent and keeps the shared source tables intact.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS im_topics_bak_20260923_shard LIKE im_topics;
|
||||
INSERT IGNORE INTO im_topics_bak_20260923_shard SELECT * FROM im_topics;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS im_group_members_bak_20260923_shard LIKE im_group_members;
|
||||
INSERT IGNORE INTO im_group_members_bak_20260923_shard SELECT * FROM im_group_members;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS im_subscriptions_bak_20260923_shard LIKE im_subscriptions;
|
||||
INSERT IGNORE INTO im_subscriptions_bak_20260923_shard SELECT * FROM im_subscriptions;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS im_system_notifications_bak_20260923_shard LIKE im_system_notifications;
|
||||
INSERT IGNORE INTO im_system_notifications_bak_20260923_shard SELECT * FROM im_system_notifications;
|
||||
|
||||
DROP PROCEDURE IF EXISTS migrate_additional_tenant_shards;
|
||||
DELIMITER $$
|
||||
CREATE PROCEDURE migrate_additional_tenant_shards()
|
||||
BEGIN
|
||||
DECLARE done INT DEFAULT 0;
|
||||
DECLARE current_tenant_id BIGINT;
|
||||
DECLARE tenant_cursor CURSOR FOR SELECT id FROM hotgo.im_tenant ORDER BY id;
|
||||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
|
||||
|
||||
OPEN tenant_cursor;
|
||||
tenant_loop: LOOP
|
||||
FETCH tenant_cursor INTO current_tenant_id;
|
||||
IF done = 1 THEN
|
||||
LEAVE tenant_loop;
|
||||
END IF;
|
||||
|
||||
SET @tenant_prefix = CONCAT('im_', LPAD(current_tenant_id, 3, '0'), '_');
|
||||
|
||||
SET @sql = CONCAT('CREATE TABLE IF NOT EXISTS `', @tenant_prefix, 'topics` LIKE `im_topics`');
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
SET @sql = CONCAT('ALTER TABLE `', @tenant_prefix, 'topics` MODIFY tenant_id BIGINT NOT NULL DEFAULT ', current_tenant_id, ' COMMENT ''商户ID''');
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
SET @sql = CONCAT('REPLACE INTO `', @tenant_prefix, 'topics` SELECT * FROM `im_topics` WHERE tenant_id=', current_tenant_id);
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = CONCAT('CREATE TABLE IF NOT EXISTS `', @tenant_prefix, 'group_members` LIKE `im_group_members`');
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
SET @sql = CONCAT('ALTER TABLE `', @tenant_prefix, 'group_members` MODIFY tenant_id BIGINT NOT NULL DEFAULT ', current_tenant_id, ' COMMENT ''商户ID''');
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
SET @sql = CONCAT('REPLACE INTO `', @tenant_prefix, 'group_members` SELECT * FROM `im_group_members` WHERE tenant_id=', current_tenant_id);
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = CONCAT('CREATE TABLE IF NOT EXISTS `', @tenant_prefix, 'subscriptions` LIKE `im_subscriptions`');
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
SET @sql = CONCAT('ALTER TABLE `', @tenant_prefix, 'subscriptions` MODIFY tenant_id BIGINT NOT NULL DEFAULT ', current_tenant_id, ' COMMENT ''商户ID''');
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
SET @sql = CONCAT('REPLACE INTO `', @tenant_prefix, 'subscriptions` SELECT * FROM `im_subscriptions` WHERE tenant_id=', current_tenant_id);
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
|
||||
SET @sql = CONCAT('CREATE TABLE IF NOT EXISTS `', @tenant_prefix, 'system_notifications` LIKE `im_system_notifications`');
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
SET @sql = CONCAT('ALTER TABLE `', @tenant_prefix, 'system_notifications` MODIFY tenant_id BIGINT NOT NULL DEFAULT ', current_tenant_id, ' COMMENT ''商户ID''');
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
SET @sql = CONCAT('REPLACE INTO `', @tenant_prefix, 'system_notifications` SELECT * FROM `im_system_notifications` WHERE tenant_id=', current_tenant_id);
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
END LOOP;
|
||||
CLOSE tenant_cursor;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
CALL migrate_additional_tenant_shards();
|
||||
DROP PROCEDURE migrate_additional_tenant_shards;
|
||||
Reference in New Issue
Block a user