| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- -- 易积水点:属性精简 + 关联对象由「摄像头」改为「设备」(KingbaseES, schema app_user)
- --
- -- 变更内容
- -- 1. 下线四个属性:
- -- waterlogging_level(积水程度)、water_depth(积水深度 cm)、
- -- water_volume(积水量 m³)、water_area(积水面积 m²);
- -- 2. 关联列 camera_ids 更名为 equipment_ids,语义由「摄像头」扩大为「设备」,
- -- 仅允许 equipment_type.id 为 16(雷达液位计)与 20(视频监控-排口+内涝点)。
- -- 存量数据直接搬迁:历史只存过 20 类摄像头设备,搬迁后仍在允许范围内。
- --
- -- 执行顺序(重要)
- -- 后端实体已改为读 equipment_ids,**本脚本必须先于新版本服务启动执行**,
- -- 否则易积水点相关接口会报「字段 equipment_ids 不存在」。
- --
- -- 幂等
- -- 每一步都先查 information_schema 判断列是否存在,重复执行只输出 NOTICE。
- -- 删列会连带删除依赖它的索引(idx_dwp_level),无需手工 DROP INDEX。
- --
- -- 2026-11-13 修订(必须用本版)
- -- 初版第 3、4 步用了 FOREACH + format(%I) 和跨行的 EXECUTE 'xxx' || 'yyy',
- -- 在本库的 KingbaseES 上 plpgsql 把该表达式解析成了非法语句,
- -- 报「语法错误 在 "f" 或附近的 / QUERY: f」(f 来自 format 的首字母),
- -- 整个 DO 块因此回滚,等于什么都没改。
- -- 本版改为:删列用静态 ALTER(逐个 IF EXISTS 守卫),注释用单行 EXECUTE 字面量,
- -- 不再有任何动态拼接 / format / FOREACH。
- --
- -- 字典
- -- drainage_waterlogging_level 保留不删(历史数据与其它模块可能引用)。
- -- 确认无用后可单独执行:
- -- DELETE FROM app_user.sys_dict_data WHERE dict_type = 'drainage_waterlogging_level';
- -- DELETE FROM app_user.sys_dict_type WHERE dict_type = 'drainage_waterlogging_level';
- --
- -- 前置:结束上一次失败执行遗留的 aborted 事务(事务外执行只会告警,不报错)
- ROLLBACK;
- BEGIN;
- SET search_path TO app_user, public;
- DO $$
- DECLARE
- has_equipment_ids boolean;
- has_camera_ids boolean;
- moved int := 0;
- BEGIN
- IF to_regclass('app_user.drainage_waterlogging_point') IS NULL THEN
- RAISE NOTICE '表 app_user.drainage_waterlogging_point 不存在,跳过';
- RETURN;
- END IF;
- SELECT EXISTS (SELECT 1 FROM information_schema.columns
- WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
- AND column_name = 'equipment_ids') INTO has_equipment_ids;
- SELECT EXISTS (SELECT 1 FROM information_schema.columns
- WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
- AND column_name = 'camera_ids') INTO has_camera_ids;
- -- 1. 新增 equipment_ids
- IF NOT has_equipment_ids THEN
- ALTER TABLE app_user.drainage_waterlogging_point ADD COLUMN equipment_ids varchar(500);
- RAISE NOTICE '1. 已新增列 equipment_ids';
- ELSE
- RAISE NOTICE '1. equipment_ids 已存在,跳过新增';
- END IF;
- -- 2. 存量关联从 camera_ids 搬到 equipment_ids(仅搬空值,避免覆盖手工修正)
- IF has_camera_ids THEN
- UPDATE app_user.drainage_waterlogging_point
- SET equipment_ids = camera_ids
- WHERE (equipment_ids IS NULL OR btrim(equipment_ids) = '')
- AND camera_ids IS NOT NULL
- AND btrim(camera_ids) <> '';
- GET DIAGNOSTICS moved = ROW_COUNT;
- RAISE NOTICE '2. 已从 camera_ids 搬迁 % 条关联记录', moved;
- ELSE
- RAISE NOTICE '2. camera_ids 已不存在,跳过数据搬迁';
- END IF;
- -- 3. 逐个删除下线字段与旧关联列(静态 DDL,不用动态 SQL)
- IF EXISTS (SELECT 1 FROM information_schema.columns
- WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
- AND column_name = 'camera_ids') THEN
- ALTER TABLE app_user.drainage_waterlogging_point DROP COLUMN camera_ids;
- RAISE NOTICE '3. 已删除列 camera_ids';
- ELSE
- RAISE NOTICE '3. camera_ids 已不存在,跳过';
- END IF;
- IF EXISTS (SELECT 1 FROM information_schema.columns
- WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
- AND column_name = 'waterlogging_level') THEN
- ALTER TABLE app_user.drainage_waterlogging_point DROP COLUMN waterlogging_level;
- RAISE NOTICE '3. 已删除列 waterlogging_level';
- ELSE
- RAISE NOTICE '3. waterlogging_level 已不存在,跳过';
- END IF;
- IF EXISTS (SELECT 1 FROM information_schema.columns
- WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
- AND column_name = 'water_depth') THEN
- ALTER TABLE app_user.drainage_waterlogging_point DROP COLUMN water_depth;
- RAISE NOTICE '3. 已删除列 water_depth';
- ELSE
- RAISE NOTICE '3. water_depth 已不存在,跳过';
- END IF;
- IF EXISTS (SELECT 1 FROM information_schema.columns
- WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
- AND column_name = 'water_volume') THEN
- ALTER TABLE app_user.drainage_waterlogging_point DROP COLUMN water_volume;
- RAISE NOTICE '3. 已删除列 water_volume';
- ELSE
- RAISE NOTICE '3. water_volume 已不存在,跳过';
- END IF;
- IF EXISTS (SELECT 1 FROM information_schema.columns
- WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
- AND column_name = 'water_area') THEN
- ALTER TABLE app_user.drainage_waterlogging_point DROP COLUMN water_area;
- RAISE NOTICE '3. 已删除列 water_area';
- ELSE
- RAISE NOTICE '3. water_area 已不存在,跳过';
- END IF;
- -- 4. 列注释(单行字面量,不拼接)
- EXECUTE 'COMMENT ON COLUMN app_user.drainage_waterlogging_point.equipment_ids IS ''关联设备ID(equipment_base.equipment_id,多个用逗号分隔;仅允许 equipment_type.id = 16 雷达液位计 / 20 视频监控)''';
- RAISE NOTICE '4. 已更新 equipment_ids 列注释';
- END $$;
- COMMIT;
- -- =====================================================================
- -- 5. 存量核对(只读;放在 COMMIT 之后,即使这一步有问题,上面的结构变更也已提交)
- -- 正常 bad 应为 0;非 0 时页面仍能展示,但这些关联再次保存会被后端拦截,
- -- 需要在「关联设备」里重新选择。
- -- =====================================================================
- DO $$
- DECLARE
- v_points int := 0;
- v_bad int := 0;
- BEGIN
- IF to_regclass('app_user.drainage_waterlogging_point') IS NULL THEN
- RAISE NOTICE '5. 表不存在,跳过核对';
- RETURN;
- END IF;
- SELECT count(*) INTO v_points
- FROM app_user.drainage_waterlogging_point
- WHERE equipment_ids IS NOT NULL AND btrim(equipment_ids) <> '';
- SELECT count(*) INTO v_bad
- FROM app_user.drainage_waterlogging_point p
- WHERE EXISTS (
- SELECT 1
- FROM unnest(string_to_array(coalesce(p.equipment_ids, ''), ',')) AS t(eid)
- WHERE btrim(t.eid) <> ''
- AND NOT EXISTS (
- SELECT 1 FROM app_user.equipment_base e
- WHERE e.equipment_id = btrim(t.eid)
- AND e.equipment_type_id IN (16, 20)
- )
- );
- RAISE NOTICE '5. 已关联设备的易积水点 % 个,其中含无效或类型越界设备关联的 % 个', v_points, v_bad;
- END $$;
- -- =====================================================================
- -- 核验(可选,取消注释后单独执行)
- -- =====================================================================
- -- SELECT column_name, data_type FROM information_schema.columns
- -- WHERE table_schema = 'app_user' AND table_name = 'drainage_waterlogging_point'
- -- ORDER BY ordinal_position;
- --
- -- SELECT point_code, point_name, equipment_ids FROM app_user.drainage_waterlogging_point
- -- ORDER BY create_time DESC LIMIT 20;
|