Files
junhong_cmp_fiber/migrations/archive/000043_simplify_commission_allocation.up.sql

62 lines
2.3 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 简化佣金分配模型:删除 ShopSeriesAllocation 层,使用 ShopPackageAllocation 直接管理
-- 重构原因:业务模型只需要套餐级别的分配,不需要系列级别的中间层
-- ============================================
-- 1. 为 ShopPackageAllocation 添加新字段
-- ============================================
-- 添加 series_id 字段(记录套餐所属系列,便于查询)
ALTER TABLE tb_shop_package_allocation
ADD COLUMN IF NOT EXISTS series_id BIGINT DEFAULT 0 NOT NULL;
COMMENT ON COLUMN tb_shop_package_allocation.series_id IS '套餐系列ID冗余字段便于查询';
-- 添加 allocator_shop_id 字段记录是谁分配的0表示平台分配
ALTER TABLE tb_shop_package_allocation
ADD COLUMN IF NOT EXISTS allocator_shop_id BIGINT DEFAULT 0 NOT NULL;
COMMENT ON COLUMN tb_shop_package_allocation.allocator_shop_id IS '分配者店铺ID0表示平台分配';
-- ============================================
-- 2. 从现有数据迁移 series_id 和 allocator_shop_id
-- ============================================
-- 通过 package 表获取 series_id
UPDATE tb_shop_package_allocation spa
SET series_id = p.series_id
FROM tb_package p
WHERE spa.package_id = p.id AND spa.series_id = 0;
-- 通过 shop_series_allocation 获取 allocator_shop_id
UPDATE tb_shop_package_allocation spa
SET allocator_shop_id = ssa.allocator_shop_id
FROM tb_shop_series_allocation ssa
WHERE spa.allocation_id = ssa.id AND spa.allocator_shop_id = 0;
-- ============================================
-- 3. 删除废弃的 allocation_id 字段
-- ============================================
ALTER TABLE tb_shop_package_allocation
DROP COLUMN IF EXISTS allocation_id;
-- ============================================
-- 4. 添加索引优化查询性能
-- ============================================
CREATE INDEX IF NOT EXISTS idx_shop_package_allocation_series_id
ON tb_shop_package_allocation(series_id);
CREATE INDEX IF NOT EXISTS idx_shop_package_allocation_allocator_shop_id
ON tb_shop_package_allocation(allocator_shop_id);
-- ============================================
-- 5. 删除废弃的表
-- ============================================
-- 删除系列分配配置表
DROP TABLE IF EXISTS tb_shop_series_allocation_config;
-- 删除系列分配表
DROP TABLE IF EXISTS tb_shop_series_allocation;