Files
tyapi-server/scripts/migrate_whitelist.sql

39 lines
1.2 KiB
MySQL
Raw Normal View History

2025-12-04 16:17:27 +08:00
-- 白名单数据结构迁移脚本
2025-12-04 18:10:14 +08:00
-- 将旧的字符串数组格式转换为新的结构体数组格式包含IP、添加时间和备注
2025-12-04 16:17:27 +08:00
--
-- 执行前请备份数据库!
--
-- 使用方法:
-- psql -U your_user -d your_database -f migrate_whitelist.sql
-- 开始事务
BEGIN;
-- 更新 api_users 表中的 white_list 字段
-- 将旧的字符串数组格式: ["ip1", "ip2"]
2025-12-04 18:10:14 +08:00
-- 转换为新格式: [{"ip_address": "ip1", "added_at": "2025-12-04T15:20:19Z", "remark": ""}, ...]
2025-12-04 16:17:27 +08:00
UPDATE api_users
SET white_list = (
SELECT json_agg(
json_build_object(
'ip_address', ip_value,
2025-12-04 18:10:14 +08:00
'added_at', COALESCE(api_users.updated_at, api_users.created_at, NOW()),
'remark', ''
2025-12-04 16:17:27 +08:00
)
2025-12-04 18:10:14 +08:00
ORDER BY ip_value -- 保持顺序
2025-12-04 16:17:27 +08:00
)
2025-12-04 18:10:14 +08:00
FROM json_array_elements_text(api_users.white_list::json) AS ip_value
2025-12-04 16:17:27 +08:00
)
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;
-- 验证迁移结果(可选)
2025-12-04 18:10:14 +08:00
-- SELECT id, white_list, updated_at, created_at FROM api_users WHERE white_list IS NOT NULL LIMIT 5;
2025-12-04 16:17:27 +08:00