zhibo/migrate_follow_data.sql
2026-01-03 15:32:31 +08:00

57 lines
2.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.

-- 迁移关注数据脚本
-- 步骤1: 创建新的关注记录表
CREATE TABLE IF NOT EXISTS `eb_follow_record` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`follower_id` int(11) NOT NULL COMMENT '关注者用户ID',
`follower_nickname` varchar(50) DEFAULT NULL COMMENT '关注者昵称',
`follower_phone` varchar(20) DEFAULT NULL COMMENT '关注者手机号',
`followed_id` int(11) NOT NULL COMMENT '被关注者用户ID',
`followed_nickname` varchar(50) DEFAULT NULL COMMENT '被关注者昵称',
`followed_phone` varchar(20) DEFAULT NULL COMMENT '被关注者手机号',
`follow_status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '关注状态1-已关注 0-已取消',
`is_deleted` tinyint(4) NOT NULL DEFAULT '0' COMMENT '逻辑删除0-未删除 1-已删除',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`ext_field1` varchar(100) DEFAULT NULL COMMENT '扩展字段1关注来源/渠道',
`ext_field2` int(11) DEFAULT NULL COMMENT '扩展字段2关注类型/优先级',
`ext_field3` varchar(200) DEFAULT NULL COMMENT '扩展字段3特殊标记/备注',
`ext_field4` bigint(20) DEFAULT NULL COMMENT '扩展字段4关联数据ID',
`ext_field5` text COMMENT '扩展字段5JSON扩展数据',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_follower_followed` (`follower_id`,`followed_id`),
KEY `idx_follower_id` (`follower_id`),
KEY `idx_followed_id` (`followed_id`),
KEY `idx_follow_status` (`follow_status`),
KEY `idx_is_deleted` (`is_deleted`),
KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='关注记录表';
-- 步骤2: 检查 eb_user_follow 表是否存在数据
-- 如果存在,迁移数据到新表
INSERT IGNORE INTO eb_follow_record (follower_id, follower_nickname, follower_phone, followed_id, followed_nickname, followed_phone, follow_status, is_deleted, create_time)
SELECT
uf.user_id as follower_id,
u1.nickname as follower_nickname,
u1.phone as follower_phone,
uf.follow_user_id as followed_id,
u2.nickname as followed_nickname,
u2.phone as followed_phone,
1 as follow_status,
0 as is_deleted,
uf.create_time
FROM eb_user_follow uf
LEFT JOIN eb_user u1 ON uf.user_id = u1.uid
LEFT JOIN eb_user u2 ON uf.follow_user_id = u2.uid
WHERE EXISTS (SELECT 1 FROM eb_user_follow LIMIT 1);
-- 步骤3: 验证数据迁移
SELECT
'原表记录数' as description,
COUNT(*) as count
FROM eb_user_follow
UNION ALL
SELECT
'新表记录数' as description,
COUNT(*) as count
FROM eb_follow_record;