91 lines
2.7 KiB
SQL
91 lines
2.7 KiB
SQL
-- +goose Up
|
|
|
|
-- 邀请返利配置、推广链接与点击按经营租户隔离。
|
|
-- distribution_site_id > 0 的历史分销配置保持平台级 tenant_id=0。
|
|
|
|
ALTER TABLE invitation_rebate_configs
|
|
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
|
DROP KEY uk_invitation_rebate_configs_site,
|
|
ADD UNIQUE KEY uk_invitation_rebate_configs_tenant_site (tenant_id, distribution_site_id),
|
|
ADD KEY idx_invitation_rebate_configs_tenant (tenant_id);
|
|
|
|
UPDATE invitation_rebate_configs
|
|
SET tenant_id = 0
|
|
WHERE distribution_site_id > 0;
|
|
|
|
UPDATE invitation_rebate_configs
|
|
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
|
WHERE distribution_site_id = 0;
|
|
|
|
INSERT INTO invitation_rebate_configs (
|
|
tenant_id,
|
|
distribution_site_id,
|
|
first_month_rate,
|
|
second_month_rate,
|
|
subsequent_rate,
|
|
enabled,
|
|
created_at,
|
|
updated_at
|
|
)
|
|
SELECT
|
|
t.id,
|
|
0,
|
|
src.first_month_rate,
|
|
src.second_month_rate,
|
|
src.subsequent_rate,
|
|
src.enabled,
|
|
NOW(),
|
|
NOW()
|
|
FROM tenants t
|
|
JOIN invitation_rebate_configs src
|
|
ON src.tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
|
AND src.distribution_site_id = 0
|
|
WHERE t.status = 'active'
|
|
AND t.id <> src.tenant_id
|
|
ON DUPLICATE KEY UPDATE
|
|
first_month_rate = VALUES(first_month_rate),
|
|
second_month_rate = VALUES(second_month_rate),
|
|
subsequent_rate = VALUES(subsequent_rate),
|
|
enabled = VALUES(enabled);
|
|
|
|
ALTER TABLE ad_tracking_links
|
|
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
|
DROP KEY uk_ad_tracking_links_code,
|
|
ADD UNIQUE KEY uk_ad_tracking_links_tenant_code (tenant_id, code),
|
|
ADD KEY idx_ad_tracking_links_tenant_status (tenant_id, status);
|
|
|
|
UPDATE ad_tracking_links
|
|
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
|
WHERE tenant_id = 0;
|
|
|
|
ALTER TABLE ad_tracking_clicks
|
|
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
|
ADD KEY idx_ad_tracking_clicks_tenant_created (tenant_id, created_at);
|
|
|
|
UPDATE ad_tracking_clicks c
|
|
JOIN ad_tracking_links l ON l.id = c.link_id
|
|
SET c.tenant_id = l.tenant_id
|
|
WHERE c.tenant_id = 0;
|
|
|
|
-- +goose Down
|
|
|
|
ALTER TABLE ad_tracking_clicks
|
|
DROP KEY idx_ad_tracking_clicks_tenant_created,
|
|
DROP COLUMN tenant_id;
|
|
|
|
ALTER TABLE ad_tracking_links
|
|
DROP KEY idx_ad_tracking_links_tenant_status,
|
|
DROP KEY uk_ad_tracking_links_tenant_code,
|
|
ADD UNIQUE KEY uk_ad_tracking_links_code (code),
|
|
DROP COLUMN tenant_id;
|
|
|
|
DELETE c
|
|
FROM invitation_rebate_configs c
|
|
WHERE c.tenant_id <> (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1);
|
|
|
|
ALTER TABLE invitation_rebate_configs
|
|
DROP KEY idx_invitation_rebate_configs_tenant,
|
|
DROP KEY uk_invitation_rebate_configs_tenant_site,
|
|
ADD UNIQUE KEY uk_invitation_rebate_configs_site (distribution_site_id),
|
|
DROP COLUMN tenant_id;
|