pangu-user-platform/scripts/sql/V1.0.5__clean_region_data.sql

27 lines
1.2 KiB
SQL
Raw Permalink 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.

-- ================================================================
-- V1.0.5 清理非湖北省区域数据(幂等,可重复执行)
-- 执行说明:先确认湖北省 region_id=42若当前仅含湖北省则删除 0 行
-- PROD 对比: 当前 pg_region 仅湖北省及其下级120 条),执行本脚本将删除 0 行
-- ================================================================
-- 0. 确认湖北省的 region_id预期 region_id = 42
SELECT region_id, region_name, parent_id, ancestors FROM pg_region WHERE region_name = '湖北省';
-- 1. 预检查:将要删除的行数(非湖北省及其下级的区域;湖北省 region_id=42其下级 ancestors 以 0,42 开头)
SELECT COUNT(*) AS '待删除的区域数量(非湖北)' FROM pg_region
WHERE region_id != 42
AND ancestors NOT LIKE '0,42%';
-- 2. 物理删除:非湖北省及其下级(保留 region_id=42 及 ancestors 以 0,42 开头的行)
DELETE FROM pg_region
WHERE region_id != 42
AND ancestors NOT LIKE '0,42%';
-- 3. 删除其他省份根节点parent_id=0 且 region_id 非 42
DELETE FROM pg_region
WHERE parent_id = 0
AND region_id != 42;
-- 4. 验证结果
SELECT COUNT(*) AS '剩余区域数量' FROM pg_region;