This commit is contained in:
2025-12-04 18:10:14 +08:00
parent 9f669a9c94
commit bfedec249f
6 changed files with 810 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
-- 白名单数据结构迁移脚本
-- 将旧的字符串数组格式转换为新的结构体数组格式包含IP添加时间)
-- 将旧的字符串数组格式转换为新的结构体数组格式包含IP添加时间和备注
--
-- 执行前请备份数据库!
--
@@ -11,20 +11,19 @@ BEGIN;
-- 更新 api_users 表中的 white_list 字段
-- 将旧的字符串数组格式: ["ip1", "ip2"]
-- 转换为新格式: [{"ip_address": "ip1", "added_at": "2025-12-04T15:20:19Z"}, ...]
-- 转换为新格式: [{"ip_address": "ip1", "added_at": "2025-12-04T15:20:19Z", "remark": ""}, ...]
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()
)
'added_at', COALESCE(api_users.updated_at, api_users.created_at, NOW()),
'remark', ''
)
ORDER BY ip_value -- 保持顺序
)
FROM json_array_elements_text(white_list::json) AS ip_value
FROM json_array_elements_text(api_users.white_list::json) AS ip_value
)
WHERE white_list IS NOT NULL
AND white_list != '[]'::json
@@ -35,5 +34,5 @@ WHERE white_list IS NOT NULL
COMMIT;
-- 验证迁移结果(可选)
-- SELECT id, white_list FROM api_users WHERE white_list IS NOT NULL LIMIT 5;
-- SELECT id, white_list, updated_at, created_at FROM api_users WHERE white_list IS NOT NULL LIMIT 5;