topfans/backend/scripts/migrate_avatar_to_fan_profiles.sql
2026-04-07 22:29:48 +08:00

26 lines
957 B
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.

-- 迁移脚本:将头像从 users 表移至 fan_profiles 表
-- 执行前请备份数据库!
-- 步骤1给 fan_profiles 表添加 avatar_url 字段
ALTER TABLE fan_profiles ADD COLUMN IF NOT EXISTS avatar_url VARCHAR(500);
-- 步骤2将 users 表的头像数据迁移到 fan_profiles
-- 注意每个用户在其每个star档案下都会继承原头像
UPDATE fan_profiles fp
SET avatar_url = u.avatar_url
FROM users u
WHERE fp.user_id = u.id
AND u.avatar_url IS NOT NULL
AND fp.avatar_url IS NULL;
-- 步骤3可选删除 users 表的 avatar_url 字段
-- 注意:执行此步骤前请确保所有依赖 users.avatar_url 的代码已迁移完成
-- ALTER TABLE users DROP COLUMN IF EXISTS avatar_url;
-- 验证迁移结果
-- SELECT fp.user_id, fp.star_id, fp.nickname, fp.avatar_url, u.avatar_url as original_avatar
-- FROM fan_profiles fp
-- JOIN users u ON fp.user_id = u.id
-- WHERE fp.avatar_url IS NOT NULL
-- LIMIT 10;