migration_drainage_waterlogging_point_devices.sql 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. -- 易积水点:属性精简 + 关联对象由「摄像头」改为「设备」(KingbaseES, schema app_user)
  2. --
  3. -- 变更内容
  4. -- 1. 下线四个属性:
  5. -- waterlogging_level(积水程度)、water_depth(积水深度 cm)、
  6. -- water_volume(积水量 m³)、water_area(积水面积 m²);
  7. -- 2. 关联列 camera_ids 更名为 equipment_ids,语义由「摄像头」扩大为「设备」,
  8. -- 仅允许 equipment_type.id 为 16(雷达液位计)与 20(视频监控-排口+内涝点)。
  9. -- 存量数据直接搬迁:历史只存过 20 类摄像头设备,搬迁后仍在允许范围内。
  10. --
  11. -- 执行顺序(重要)
  12. -- 后端实体已改为读 equipment_ids,**本脚本必须先于新版本服务启动执行**,
  13. -- 否则易积水点相关接口会报「字段 equipment_ids 不存在」。
  14. --
  15. -- 幂等
  16. -- 每一步都先查 information_schema 判断列是否存在,重复执行只输出 NOTICE。
  17. -- 删列会连带删除依赖它的索引(idx_dwp_level),无需手工 DROP INDEX。
  18. --
  19. -- 2026-11-13 修订(必须用本版)
  20. -- 初版第 3、4 步用了 FOREACH + format(%I) 和跨行的 EXECUTE 'xxx' || 'yyy',
  21. -- 在本库的 KingbaseES 上 plpgsql 把该表达式解析成了非法语句,
  22. -- 报「语法错误 在 "f" 或附近的 / QUERY: f」(f 来自 format 的首字母),
  23. -- 整个 DO 块因此回滚,等于什么都没改。
  24. -- 本版改为:删列用静态 ALTER(逐个 IF EXISTS 守卫),注释用单行 EXECUTE 字面量,
  25. -- 不再有任何动态拼接 / format / FOREACH。
  26. --
  27. -- 字典
  28. -- drainage_waterlogging_level 保留不删(历史数据与其它模块可能引用)。
  29. -- 确认无用后可单独执行:
  30. -- DELETE FROM app_user.sys_dict_data WHERE dict_type = 'drainage_waterlogging_level';
  31. -- DELETE FROM app_user.sys_dict_type WHERE dict_type = 'drainage_waterlogging_level';
  32. --
  33. -- 前置:结束上一次失败执行遗留的 aborted 事务(事务外执行只会告警,不报错)
  34. ROLLBACK;
  35. BEGIN;
  36. SET search_path TO app_user, public;
  37. DO $$
  38. DECLARE
  39. has_equipment_ids boolean;
  40. has_camera_ids boolean;
  41. moved int := 0;
  42. BEGIN
  43. IF to_regclass('app_user.drainage_waterlogging_point') IS NULL THEN
  44. RAISE NOTICE '表 app_user.drainage_waterlogging_point 不存在,跳过';
  45. RETURN;
  46. END IF;
  47. SELECT EXISTS (SELECT 1 FROM information_schema.columns
  48. WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
  49. AND column_name = 'equipment_ids') INTO has_equipment_ids;
  50. SELECT EXISTS (SELECT 1 FROM information_schema.columns
  51. WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
  52. AND column_name = 'camera_ids') INTO has_camera_ids;
  53. -- 1. 新增 equipment_ids
  54. IF NOT has_equipment_ids THEN
  55. ALTER TABLE app_user.drainage_waterlogging_point ADD COLUMN equipment_ids varchar(500);
  56. RAISE NOTICE '1. 已新增列 equipment_ids';
  57. ELSE
  58. RAISE NOTICE '1. equipment_ids 已存在,跳过新增';
  59. END IF;
  60. -- 2. 存量关联从 camera_ids 搬到 equipment_ids(仅搬空值,避免覆盖手工修正)
  61. IF has_camera_ids THEN
  62. UPDATE app_user.drainage_waterlogging_point
  63. SET equipment_ids = camera_ids
  64. WHERE (equipment_ids IS NULL OR btrim(equipment_ids) = '')
  65. AND camera_ids IS NOT NULL
  66. AND btrim(camera_ids) <> '';
  67. GET DIAGNOSTICS moved = ROW_COUNT;
  68. RAISE NOTICE '2. 已从 camera_ids 搬迁 % 条关联记录', moved;
  69. ELSE
  70. RAISE NOTICE '2. camera_ids 已不存在,跳过数据搬迁';
  71. END IF;
  72. -- 3. 逐个删除下线字段与旧关联列(静态 DDL,不用动态 SQL)
  73. IF EXISTS (SELECT 1 FROM information_schema.columns
  74. WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
  75. AND column_name = 'camera_ids') THEN
  76. ALTER TABLE app_user.drainage_waterlogging_point DROP COLUMN camera_ids;
  77. RAISE NOTICE '3. 已删除列 camera_ids';
  78. ELSE
  79. RAISE NOTICE '3. camera_ids 已不存在,跳过';
  80. END IF;
  81. IF EXISTS (SELECT 1 FROM information_schema.columns
  82. WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
  83. AND column_name = 'waterlogging_level') THEN
  84. ALTER TABLE app_user.drainage_waterlogging_point DROP COLUMN waterlogging_level;
  85. RAISE NOTICE '3. 已删除列 waterlogging_level';
  86. ELSE
  87. RAISE NOTICE '3. waterlogging_level 已不存在,跳过';
  88. END IF;
  89. IF EXISTS (SELECT 1 FROM information_schema.columns
  90. WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
  91. AND column_name = 'water_depth') THEN
  92. ALTER TABLE app_user.drainage_waterlogging_point DROP COLUMN water_depth;
  93. RAISE NOTICE '3. 已删除列 water_depth';
  94. ELSE
  95. RAISE NOTICE '3. water_depth 已不存在,跳过';
  96. END IF;
  97. IF EXISTS (SELECT 1 FROM information_schema.columns
  98. WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
  99. AND column_name = 'water_volume') THEN
  100. ALTER TABLE app_user.drainage_waterlogging_point DROP COLUMN water_volume;
  101. RAISE NOTICE '3. 已删除列 water_volume';
  102. ELSE
  103. RAISE NOTICE '3. water_volume 已不存在,跳过';
  104. END IF;
  105. IF EXISTS (SELECT 1 FROM information_schema.columns
  106. WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
  107. AND column_name = 'water_area') THEN
  108. ALTER TABLE app_user.drainage_waterlogging_point DROP COLUMN water_area;
  109. RAISE NOTICE '3. 已删除列 water_area';
  110. ELSE
  111. RAISE NOTICE '3. water_area 已不存在,跳过';
  112. END IF;
  113. -- 4. 列注释(单行字面量,不拼接)
  114. EXECUTE 'COMMENT ON COLUMN app_user.drainage_waterlogging_point.equipment_ids IS ''关联设备ID(equipment_base.equipment_id,多个用逗号分隔;仅允许 equipment_type.id = 16 雷达液位计 / 20 视频监控)''';
  115. RAISE NOTICE '4. 已更新 equipment_ids 列注释';
  116. END $$;
  117. COMMIT;
  118. -- =====================================================================
  119. -- 5. 存量核对(只读;放在 COMMIT 之后,即使这一步有问题,上面的结构变更也已提交)
  120. -- 正常 bad 应为 0;非 0 时页面仍能展示,但这些关联再次保存会被后端拦截,
  121. -- 需要在「关联设备」里重新选择。
  122. -- =====================================================================
  123. DO $$
  124. DECLARE
  125. v_points int := 0;
  126. v_bad int := 0;
  127. BEGIN
  128. IF to_regclass('app_user.drainage_waterlogging_point') IS NULL THEN
  129. RAISE NOTICE '5. 表不存在,跳过核对';
  130. RETURN;
  131. END IF;
  132. SELECT count(*) INTO v_points
  133. FROM app_user.drainage_waterlogging_point
  134. WHERE equipment_ids IS NOT NULL AND btrim(equipment_ids) <> '';
  135. SELECT count(*) INTO v_bad
  136. FROM app_user.drainage_waterlogging_point p
  137. WHERE EXISTS (
  138. SELECT 1
  139. FROM unnest(string_to_array(coalesce(p.equipment_ids, ''), ',')) AS t(eid)
  140. WHERE btrim(t.eid) <> ''
  141. AND NOT EXISTS (
  142. SELECT 1 FROM app_user.equipment_base e
  143. WHERE e.equipment_id = btrim(t.eid)
  144. AND e.equipment_type_id IN (16, 20)
  145. )
  146. );
  147. RAISE NOTICE '5. 已关联设备的易积水点 % 个,其中含无效或类型越界设备关联的 % 个', v_points, v_bad;
  148. END $$;
  149. -- =====================================================================
  150. -- 核验(可选,取消注释后单独执行)
  151. -- =====================================================================
  152. -- SELECT column_name, data_type FROM information_schema.columns
  153. -- WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
  154. -- ORDER BY ordinal_position;
  155. --
  156. -- SELECT point_code, point_name, equipment_ids FROM app_user.drainage_waterlogging_point
  157. -- ORDER BY create_time DESC LIMIT 20;