deep-risk/backend/migrations/add_entity_fields.sql
2025-12-14 20:08:27 +08:00

61 lines
2.1 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.

-- =====================================================
-- 数据库迁移脚本:为用户表添加实体关联字段
-- =====================================================
-- 执行日期: 2024-11-29
-- 描述: 为 sys_user 表添加 entity_id 和 entity_type 字段用于实现实体ID自动绑定功能
-- =====================================================
-- 1. 添加字段
-- =====================================================
-- 添加 entity_id 字段
ALTER TABLE sys_user
ADD COLUMN entity_id VARCHAR(50) NULL COMMENT '关联的企业实体IDMCN机构或主播';
-- 添加 entity_type 字段
ALTER TABLE sys_user
ADD COLUMN entity_type VARCHAR(20) NULL COMMENT '关联实体类型mcn-机构streamer-主播';
-- =====================================================
-- 2. 创建索引(可选,用于查询优化)
-- =====================================================
-- entity_id 索引
CREATE INDEX ix_sys_user_entity_id ON sys_user(entity_id);
-- entity_type 索引
CREATE INDEX ix_sys_user_entity_type ON sys_user(entity_type);
-- =====================================================
-- 3. 更新现有用户数据(示例)
-- =====================================================
-- 注意以下SQL是示例请根据实际情况修改用户ID和实体ID
-- 示例1: 更新主播用户
-- UPDATE sys_user
-- SET entity_id = 'streamer_001', entity_type = 'streamer'
-- WHERE username = 'streamer1';
-- 示例2: 更新MCN用户
-- UPDATE sys_user
-- SET entity_id = 'mcn_001', entity_type = 'mcn'
-- WHERE username = 'mcnadmin';
-- =====================================================
-- 4. 验证迁移结果
-- =====================================================
-- 查看表结构
-- DESCRIBE sys_user;
-- 查看索引
-- SHOW INDEX FROM sys_user;
-- =====================================================
-- 5. 回滚脚本(如需要)
-- =====================================================
-- DROP INDEX ix_sys_user_entity_type ON sys_user;
-- DROP INDEX ix_sys_user_entity_id ON sys_user;
-- ALTER TABLE sys_user DROP COLUMN entity_type;
-- ALTER TABLE sys_user DROP COLUMN entity_id;