-- 腾讯地图 Key 搬进系统参数:sys_param 种子脚本(可选,不在启动期自动执行) -- -- 背景:取值原先硬编码在两侧 —— 后端 application.yml 的 unisbase.app.tencent-map.key -- (默认值取环境变量 TENCENT_MAP_KEY),前端 tencentMap.ts / tencentMapGl.ts 里各写一份 -- DEFAULT_TENCENT_MAP_KEY。现在两侧都读 sys_param 的 crm.tencent-map.key: -- 管理台「系统设置 → 参数管理」改一行即可,不发版不重启。 -- -- 为什么这份脚本是可选的:后端 TencentMapConfigBootstrapInitializer 启动时会自己补种 -- (库里没有这一行、且 yml/环境变量确实有值时,把那个真正在生效的值种成一行)。 -- 所以不跑本脚本,参数管理页第一次打开也看得到这一项。要显式控制落库时机/值才跑本脚本。 -- -- 口径与 sql/20261119_idp_sys_param_seed.sql 一致:值放临时表、列名自适应、主键序列自检、 -- 存在即跳过(不覆盖已有行 —— 已有行可能是运维改过的值,覆盖等于把生产 Key 换回来)。 -- -- ★ 跑之前先给 Key,两种给法(不给就整段中止,不会静默建一条空值行): -- A. 改下面 insert 里的那个空串; -- B. 同一次 psql 调用里先设一个 GUC,脚本优先读它: -- psql -d your_database -c "SET unis.tencent_map_key = 'XXXXX-XXXX-XXXX'" \ -- -f sql/20261126_tencent_map_key_sys_param.sql -- -c 必须写在 -f **前面**:psql 按命令行顺序执行,写后面就是脚本跑完才设值(等于没设)。 -- 两者共用同一个会话,所以 SET 在脚本里读得到;只给 -f 则走 A。 -- 不想让 Key 落进 shell history:用 A,或 `psql -f ... ` 之外再 `\set` 手工会话里跑。 -- -- 为什么"不给就中止"而不是"建成空值":有这一行就以它为准、不再退回 yml(这是设计决定,见 -- TencentMapConfigService 类注释),所以空值行 = 主动关闭打卡地图。让一个忘了填值的 -- 人顺手关掉生产地图,是不该有的副作用。 -- -- 生效:走管理台页面改值不用管缓存(框架 update 自带 deleteParamCache);直接改库要自己 -- del sys:param:crm.tencent-map.key,否则最坏 24 小时才被发现(正缓存 TTL 86400s)。 -- 回滚:见文件末尾,只碰这一行。 begin; -- 先 drop 再 create:只写 create ... on commit drop 的话,同一个会话里被外层事务包着跑第二遍 -- 会撞 "relation tmk_seed already exists"(脚本自带的 begin/commit 各自成事务时不会), -- 这层幂等不值钱,但省掉一次"脚本在我这儿跑不通"的排查。 drop table if exists tmk_seed; create temporary table tmk_seed ( param_key text primary key, param_value text not null default '', description text not null ) on commit drop; insert into tmk_seed (param_key, param_value, description) values ('crm.tencent-map.key', '', '腾讯地图 Key(地点逆解析 + 打卡地图 + H5 定位组件共用);留空则地点解析与地图展示关闭'); do $$ declare seed record; v_from_guc text; has_param_type boolean; has_is_system boolean; has_description boolean; has_status boolean; has_is_deleted boolean; has_created boolean; has_updated boolean; v_sequence_name text; statement text; v_exists boolean; v_exists_tail text; inserted integer := 0; seeded integer := 0; skipped integer := 0; v_message text; begin if to_regclass('public.sys_param') is null then raise exception 'public.sys_param 不存在,本脚本不适用(已整段跳过)'; end if; -- GUC 优先于表里的字面量:同一会话里 -c "SET ..." 就能传值,不用改文件。 v_from_guc := nullif(btrim(coalesce(current_setting('unis.tencent_map_key', true), '')), ''); -- 主键序列自检:只上调不下调,重复执行无害(这张表的序列会落后于数据,撞过才知道要防)。 select pg_get_serial_sequence('public.sys_param', 'param_id') into v_sequence_name; if v_sequence_name is not null and to_regclass(v_sequence_name) is not null then execute format('select setval(%L, coalesce((select max(param_id) from public.sys_param), 0) + 1, false)', v_sequence_name); end if; select count(*) = 1 into has_param_type from information_schema.columns where table_schema = 'public' and table_name = 'sys_param' and column_name = 'param_type'; select count(*) = 1 into has_is_system from information_schema.columns where table_schema = 'public' and table_name = 'sys_param' and column_name = 'is_system'; select count(*) = 1 into has_description from information_schema.columns where table_schema = 'public' and table_name = 'sys_param' and column_name = 'description'; select count(*) = 1 into has_status from information_schema.columns where table_schema = 'public' and table_name = 'sys_param' and column_name = 'status'; select count(*) = 1 into has_is_deleted from information_schema.columns where table_schema = 'public' and table_name = 'sys_param' and column_name = 'is_deleted'; select count(*) = 1 into has_created from information_schema.columns where table_schema = 'public' and table_name = 'sys_param' and column_name = 'created_at'; select count(*) = 1 into has_updated from information_schema.columns where table_schema = 'public' and table_name = 'sys_param' and column_name = 'updated_at'; for seed in select * from tmk_seed order by param_key loop -- 「有没有这一行」也必须走动态 SQL:静态写法在探测出没有 is_deleted 列的库上会 -- 直接解析失败,那身自适应就白做了(同下面 INSERT 的列名自适应一个道理)。 v_exists_tail := case when has_is_deleted then ' and coalesce(p.is_deleted, 0) = 0' else '' end; execute format('select exists (select 1 from sys_param p' || ' where p.param_key = %L' || v_exists_tail || ')', seed.param_key) into v_exists; if not v_exists then if v_from_guc is null and btrim(seed.param_value) = '' then -- RAISE 的格式串只能是一个字面量,拼好的串得先进变量再用 '%s' 带出去 -- (直接在 raise exception 后面写 a || b 是语法错误,本脚本在真库上就这么炸过一次)。 v_message := '没给 Key(GUC unis.tencent_map_key 与 ' || seed.param_key || ' 的 param_value 都是空)。空值会建成"显式关闭地图"那一行,' || '不是"沿用现有配置",所以宁可中止:请改脚本里的 insert,' || '或加 -c "SET unis.tencent_map_key = ''你的Key''"'; raise exception '%', v_message; end if; -- 值一律 quote_literal 内联,不走 $n:带哪几列是探测出来的,参数个数会跟着变, -- 而 EXECUTE ... USING 的个数与动态 SQL 里实际引用的 $n 不一致时 PG 直接报错。 statement := 'insert into sys_param (param_key, param_value'; if has_param_type then statement := statement || ', param_type'; end if; if has_is_system then statement := statement || ', is_system'; end if; if has_description then statement := statement || ', description'; end if; if has_status then statement := statement || ', status'; end if; if has_is_deleted then statement := statement || ', is_deleted'; end if; if has_created then statement := statement || ', created_at'; end if; if has_updated then statement := statement || ', updated_at'; end if; statement := statement || ') select ' || quote_literal(seed.param_key) || ', ' || quote_literal(coalesce(v_from_guc, btrim(seed.param_value))); if has_param_type then statement := statement || ', ''STRING'''; end if; if has_is_system then statement := statement || ', 1'; end if; if has_description then statement := statement || ', ' || quote_literal(seed.description); end if; if has_status then statement := statement || ', 1'; end if; if has_is_deleted then statement := statement || ', 0'; end if; if has_created then statement := statement || ', now()'; end if; if has_updated then statement := statement || ', now()'; end if; execute statement; get diagnostics inserted = row_count; seeded := seeded + inserted; else -- 存在即跳过且不覆盖:已有值可能是运维改过的,覆盖等于把生产 Key 换回脚本里这一份。 skipped := skipped + 1; end if; end loop; raise notice '腾讯地图 Key 参数种子:新建 % 行、已存在跳过 % 行(值不打印,避免进 CI 日志)', seeded, skipped; end $$; -- 建完行请回管理台「系统设置 → 参数管理」核对一眼(键名 crm.tencent-map.key)。 commit; -- 回滚(只删这一行;删掉之后后端退回读 yml/环境变量,前端地图与定位报「未配置」)。 -- update sys_param set is_deleted = 1, updated_at = now() -- where param_key = 'crm.tencent-map.key' and coalesce(is_deleted, 0) = 0;