This commit is contained in:
39
scripts/migrate_whitelist.sql
Normal file
39
scripts/migrate_whitelist.sql
Normal file
@@ -0,0 +1,39 @@
|
||||
-- 白名单数据结构迁移脚本
|
||||
-- 将旧的字符串数组格式转换为新的结构体数组格式(包含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;
|
||||
|
||||
Reference in New Issue
Block a user