feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 客服、通知、短信、文件和站点配置按经营租户隔离。
|
||||
-- 历史数据优先按业务关系回填,无法推导的旧数据归大锤。
|
||||
|
||||
ALTER TABLE chat_conversations
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_chat_conversations_tenant_created (tenant_id, created_at);
|
||||
|
||||
UPDATE chat_conversations c
|
||||
JOIN rental_orders o ON o.id = c.order_id
|
||||
SET c.tenant_id = o.buyer_tenant_id
|
||||
WHERE c.tenant_id = 0 AND o.buyer_tenant_id > 0;
|
||||
|
||||
UPDATE chat_conversations c
|
||||
JOIN rental_listings l ON l.id = c.listing_id
|
||||
SET c.tenant_id = l.source_tenant_id
|
||||
WHERE c.tenant_id = 0 AND l.source_tenant_id > 0;
|
||||
|
||||
UPDATE chat_conversations
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE chat_qrcode_pool
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_chat_qrcode_pool_tenant_site_status (tenant_id, distribution_site_id, status);
|
||||
|
||||
UPDATE chat_qrcode_pool
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE chat_qrcode_delivery_tasks
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_chat_qrcode_delivery_tenant_status (tenant_id, status, created_at);
|
||||
|
||||
UPDATE chat_qrcode_delivery_tasks t
|
||||
JOIN chat_conversations c ON c.id = t.conversation_id
|
||||
SET t.tenant_id = c.tenant_id
|
||||
WHERE t.tenant_id = 0;
|
||||
|
||||
UPDATE chat_qrcode_delivery_tasks
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE chat_support_groups
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
DROP KEY uk_chat_support_groups_code,
|
||||
ADD UNIQUE KEY uk_chat_support_groups_tenant_code (tenant_id, code),
|
||||
ADD KEY idx_chat_support_groups_tenant_status (tenant_id, status, sort_order);
|
||||
|
||||
UPDATE chat_support_groups
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
INSERT INTO chat_support_groups (tenant_id, code, name, description, status, sort_order)
|
||||
SELECT t.id, src.code, src.name, src.description, src.status, src.sort_order
|
||||
FROM tenants t
|
||||
JOIN chat_support_groups src
|
||||
ON src.tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE t.status = 'active'
|
||||
AND t.id <> src.tenant_id
|
||||
ON DUPLICATE KEY UPDATE
|
||||
name = VALUES(name),
|
||||
description = VALUES(description);
|
||||
|
||||
ALTER TABLE chat_quick_replies
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_chat_quick_replies_tenant_admin_sort (tenant_id, admin_user_id, sort_order);
|
||||
|
||||
UPDATE chat_quick_replies
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE notifications
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_notifications_tenant_user_read (tenant_id, user_id, read_at, id);
|
||||
|
||||
UPDATE notifications n
|
||||
JOIN users u ON u.id = n.user_id
|
||||
SET n.tenant_id = u.tenant_id
|
||||
WHERE n.tenant_id = 0;
|
||||
|
||||
UPDATE notifications
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE admin_notifications
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_admin_notifications_tenant_user_read (tenant_id, admin_user_id, is_read, id);
|
||||
|
||||
UPDATE admin_notifications n
|
||||
JOIN admin_user_roles aur ON aur.admin_user_id = n.admin_user_id AND aur.tenant_id > 0
|
||||
SET n.tenant_id = aur.tenant_id
|
||||
WHERE n.tenant_id = 0;
|
||||
|
||||
UPDATE admin_notifications
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE sms_messages
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
DROP KEY uk_sms_messages_node_biz_user,
|
||||
ADD UNIQUE KEY uk_sms_messages_tenant_node_biz_user (tenant_id, node, biz_type, biz_id, user_id),
|
||||
ADD KEY idx_sms_messages_tenant_status_id (tenant_id, status, id);
|
||||
|
||||
UPDATE sms_messages s
|
||||
JOIN users u ON u.id = s.user_id
|
||||
SET s.tenant_id = u.tenant_id
|
||||
WHERE s.tenant_id = 0;
|
||||
|
||||
UPDATE sms_messages
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE file_upload_owners
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_file_upload_owners_tenant_user (tenant_id, user_id, object_key);
|
||||
|
||||
UPDATE file_upload_owners f
|
||||
JOIN users u ON u.id = f.user_id
|
||||
SET f.tenant_id = u.tenant_id
|
||||
WHERE f.tenant_id = 0;
|
||||
|
||||
UPDATE file_upload_owners
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE announcements
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_announcements_tenant_site (tenant_id, distribution_site_id, status, is_pinned, priority, published_at);
|
||||
|
||||
UPDATE announcements
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE system_configs
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
DROP KEY uk_system_configs_key,
|
||||
ADD UNIQUE KEY uk_system_configs_tenant_key (tenant_id, `key`);
|
||||
|
||||
UPDATE system_configs
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
ALTER TABLE system_configs
|
||||
DROP KEY uk_system_configs_tenant_key,
|
||||
ADD UNIQUE KEY uk_system_configs_key (`key`),
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE announcements
|
||||
DROP KEY idx_announcements_tenant_site,
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE file_upload_owners
|
||||
DROP KEY idx_file_upload_owners_tenant_user,
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE sms_messages
|
||||
DROP KEY idx_sms_messages_tenant_status_id,
|
||||
DROP KEY uk_sms_messages_tenant_node_biz_user,
|
||||
ADD UNIQUE KEY uk_sms_messages_node_biz_user (node, biz_type, biz_id, user_id),
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE admin_notifications
|
||||
DROP KEY idx_admin_notifications_tenant_user_read,
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE notifications
|
||||
DROP KEY idx_notifications_tenant_user_read,
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE chat_quick_replies
|
||||
DROP KEY idx_chat_quick_replies_tenant_admin_sort,
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
DELETE dst
|
||||
FROM chat_support_groups dst
|
||||
JOIN tenants t ON t.id = dst.tenant_id
|
||||
WHERE t.code <> 'dachui';
|
||||
|
||||
ALTER TABLE chat_support_groups
|
||||
DROP KEY idx_chat_support_groups_tenant_status,
|
||||
DROP KEY uk_chat_support_groups_tenant_code,
|
||||
ADD UNIQUE KEY uk_chat_support_groups_code (code),
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE chat_qrcode_delivery_tasks
|
||||
DROP KEY idx_chat_qrcode_delivery_tenant_status,
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE chat_qrcode_pool
|
||||
DROP KEY idx_chat_qrcode_pool_tenant_site_status,
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE chat_conversations
|
||||
DROP KEY idx_chat_conversations_tenant_created,
|
||||
DROP COLUMN tenant_id;
|
||||
Reference in New Issue
Block a user