-- 白名单数据结构迁移脚本 -- 将旧的字符串数组格式转换为新的结构体数组格式(包含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", "remark": ""}, ...] UPDATE api_users SET white_list = ( SELECT json_agg( json_build_object( 'ip_address', ip_value, 'added_at', COALESCE(api_users.updated_at, api_users.created_at, NOW()), 'remark', '' ) ORDER BY 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 AND white_list::text NOT LIKE '[{%' -- 排除已经是新格式的数据 AND json_array_length(white_list::json) > 0; -- 提交事务 COMMIT; -- 验证迁移结果(可选) -- SELECT id, white_list, updated_at, created_at FROM api_users WHERE white_list IS NOT NULL LIMIT 5;