Files
data-engine/sql/report_add_tenant_id.sql
2026-06-11 13:06:54 +08:00

118 lines
4.6 KiB
SQL
Raw 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.
-- ============================================================
-- 报表系统表 - 新增 tenant_id 迁移脚本
-- 为已存在的5张报表系统表添加租户隔离字段
-- 执行前请确认当前数据库中表是否存在
-- ============================================================
-- ----------------------------------------------------------
-- 1. report_business_config (业务配置表)
-- ----------------------------------------------------------
-- 添加 tenant_id 列
ALTER TABLE IF EXISTS report_business_config
ADD COLUMN IF NOT EXISTS tenant_id BIGINT NOT NULL DEFAULT 0;
-- 重建唯一索引(加入 tenant_id
DROP INDEX IF EXISTS idx_business_code;
CREATE UNIQUE INDEX IF NOT EXISTS idx_business_code
ON report_business_config (tenant_id, business_code);
COMMENT ON COLUMN report_business_config.tenant_id IS '租户ID';
-- ----------------------------------------------------------
-- 2. report_report_config (报表配置表)
-- ----------------------------------------------------------
-- 添加 tenant_id 列
ALTER TABLE IF EXISTS report_report_config
ADD COLUMN IF NOT EXISTS tenant_id BIGINT NOT NULL DEFAULT 0;
-- 重建唯一约束(加入 tenant_id
ALTER TABLE IF EXISTS report_report_config
DROP CONSTRAINT IF EXISTS uk_business_report_code;
ALTER TABLE IF EXISTS report_report_config
ADD CONSTRAINT uk_business_report_code UNIQUE (tenant_id, business_code, report_code);
-- 重建索引
DROP INDEX IF EXISTS idx_report_business_code;
CREATE INDEX IF NOT EXISTS idx_report_business_code
ON report_report_config (tenant_id, business_code);
COMMENT ON COLUMN report_report_config.tenant_id IS '租户ID';
-- ----------------------------------------------------------
-- 3. report_field_config (字段配置表)
-- ----------------------------------------------------------
-- 添加 tenant_id 列
ALTER TABLE IF EXISTS report_field_config
ADD COLUMN IF NOT EXISTS tenant_id BIGINT NOT NULL DEFAULT 0;
-- 重建唯一约束(加入 tenant_id
ALTER TABLE IF EXISTS report_field_config
DROP CONSTRAINT IF EXISTS uk_business_report_field_code;
ALTER TABLE IF EXISTS report_field_config
ADD CONSTRAINT uk_business_report_field_code UNIQUE (tenant_id, business_code, report_code, field_code);
-- 重建索引
DROP INDEX IF EXISTS idx_field_business_report;
CREATE INDEX IF NOT EXISTS idx_field_business_report
ON report_field_config (tenant_id, business_code, report_code);
DROP INDEX IF EXISTS idx_field_data_type;
CREATE INDEX IF NOT EXISTS idx_field_data_type
ON report_field_config (tenant_id, data_type);
DROP INDEX IF EXISTS idx_field_field_role;
CREATE INDEX IF NOT EXISTS idx_field_field_role
ON report_field_config (tenant_id, field_role);
COMMENT ON COLUMN report_field_config.tenant_id IS '租户ID';
-- ----------------------------------------------------------
-- 4. report_extract_config (抽取配置表)
-- ----------------------------------------------------------
-- 添加 tenant_id 列
ALTER TABLE IF EXISTS report_extract_config
ADD COLUMN IF NOT EXISTS tenant_id BIGINT NOT NULL DEFAULT 0;
-- 重建唯一约束(加入 tenant_id
ALTER TABLE IF EXISTS report_extract_config
DROP CONSTRAINT IF EXISTS uk_business_report_extract_code;
ALTER TABLE IF EXISTS report_extract_config
ADD CONSTRAINT uk_business_report_extract_code UNIQUE (tenant_id, business_code, report_code, extract_code);
-- 重建索引
DROP INDEX IF EXISTS idx_extract_business_report;
CREATE INDEX IF NOT EXISTS idx_extract_business_report
ON report_extract_config (tenant_id, business_code, report_code);
COMMENT ON COLUMN report_extract_config.tenant_id IS '租户ID';
-- ----------------------------------------------------------
-- 5. report_extract_log (抽取记录表)
-- ----------------------------------------------------------
-- 添加 tenant_id 列
ALTER TABLE IF EXISTS report_extract_log
ADD COLUMN IF NOT EXISTS tenant_id BIGINT NOT NULL DEFAULT 0;
-- 重建唯一约束(加入 tenant_id
ALTER TABLE IF EXISTS report_extract_log
DROP CONSTRAINT IF EXISTS uk_extract_keys;
ALTER TABLE IF EXISTS report_extract_log
ADD CONSTRAINT uk_extract_keys UNIQUE (tenant_id, business_code, report_code, extract_code, stat_date);
-- 重建索引
DROP INDEX IF EXISTS idx_extract_log_business_report;
CREATE INDEX IF NOT EXISTS idx_extract_log_business_report
ON report_extract_log (tenant_id, business_code, report_code);
DROP INDEX IF EXISTS idx_extract_log_stat_date;
CREATE INDEX IF NOT EXISTS idx_extract_log_stat_date
ON report_extract_log (tenant_id, stat_date);
COMMENT ON COLUMN report_extract_log.tenant_id IS '租户ID';