Files
tyapi-server/scripts/migrate_whitelist.sql
2025-12-04 16:17:27 +08:00

40 lines
1.1 KiB
PL/PgSQL
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.

-- 白名单数据结构迁移脚本
-- 将旧的字符串数组格式转换为新的结构体数组格式包含IP和添加时间
--
-- 执行前请备份数据库!
--
-- 使用方法:
-- psql -U your_user -d your_database -f migrate_whitelist.sql
-- 开始事务
BEGIN;
-- 更新 api_users 表中的 white_list 字段
-- 将旧的字符串数组格式: ["ip1", "ip2"]
-- 转换为新格式: [{"ip_address": "ip1", "added_at": "2025-12-04T15:20:19Z"}, ...]
UPDATE api_users
SET white_list = (
SELECT json_agg(
json_build_object(
'ip_address', ip_value,
'added_at', COALESCE(
(SELECT updated_at FROM api_users WHERE id = api_users.id),
NOW()
)
)
)
FROM json_array_elements_text(white_list::json) AS ip_value
)
WHERE white_list IS NOT NULL
AND white_list != '[]'::json
AND white_list::text NOT LIKE '[{%' -- 排除已经是新格式的数据
AND json_array_length(white_list::json) > 0;
-- 提交事务
COMMIT;
-- 验证迁移结果(可选)
-- SELECT id, white_list FROM api_users WHERE white_list IS NOT NULL LIMIT 5;