172 lines
12 KiB
PL/PgSQL
172 lines
12 KiB
PL/PgSQL
-- 统一认证(Hub)运行时配置:sys_param 种子行(可选脚本,不在启动期自动执行)
|
||
--
|
||
-- 为什么默认不执行:
|
||
-- 1) 本轮纪律是「不往 sys_param 插种子行,避免脏库」。配置页的清单/类型/取值范围全部来自
|
||
-- backend/src/main/java/com/unis/crm/service/IdpRuntimeConfig.java 里的 CATALOG(单一事实源),
|
||
-- 库里一行 sys_param 都没有,GET/PUT /api/idp/admin/config 也照常工作 —— 这些种子不是功能前提。
|
||
-- 2) 插下去等于把这些键的来源从 yml 变成 sys_param(sys_param 优先级更高)。
|
||
-- application.yml 的 crm.idp.enabled / base-url / app-code / integration-key / sso.enabled /
|
||
-- increment.enabled / provision.mode / login-fallback.mode / account-max-length 都留了
|
||
-- ${CRM_IDP_*} 环境变量口子,本地冒烟脚本靠它们注入。种子一落,环境变量就压不过了。
|
||
-- → 跑冒烟之前别执行;确实要「管理台首次接管」时再执行。
|
||
-- 3) 密钥 crm.idp.integration-key 刻意不种子:明文 SQL 进 git 等于把密钥多抄一份到第二个地方,
|
||
-- 而且页面只回掩码(****末4位),运维看不出它被种子覆盖了。
|
||
-- 4) crm.idp.provision.hub-org-allowlist 当前是空列表;空值在读取侧等于「没有覆盖」,种子无意义。
|
||
-- 5) 只读项(cron / 超时 / 熔断 / 间隔 / tenant-id / schema.enabled / ticket.*)不种子:
|
||
-- 写了也不生效,只会多出现在 ignoredSysParamKeys 里让人误判成已生效。
|
||
--
|
||
-- 幂等:同键已有未删除行就跳过(不 UPDATE、不把 updated_at 刷一遍),重复执行零变化。
|
||
-- 列名自适应:sys_param 的建表 SQL 在框架侧(unisbase jar 里没有 .sql),仓内只能确认
|
||
-- param_key / param_value / param_id(@TableId(value="param_id", type=AUTO)),
|
||
-- 且 SysParam.tenantId 是 @TableField(exist=false) —— 所以列名一律现场探测,缺列就不写那一列,
|
||
-- 不拿猜测的列名去撞「column does not exist」。param_id 交给库生成,不写。
|
||
-- 执行:psql -f sql/20261119_idp_sys_param_seed.sql
|
||
-- 回滚:文件末尾,删的就是本脚本插的那批键,与插入等量。
|
||
|
||
begin;
|
||
|
||
create temporary table idp_seed (
|
||
param_key text primary key,
|
||
param_value text not null,
|
||
description text not null
|
||
) on commit drop;
|
||
|
||
-- 值取当前 application.yml / 代码安全默认:插完这一行的生效值 == 插之前的生效值,
|
||
-- 种子本身不改变行为,只是把「可改」交到管理台。
|
||
insert into idp_seed (param_key, param_value, description) values
|
||
('crm.idp.enabled', 'true', '统一认证总开关'),
|
||
('crm.idp.base-url', 'https://hub.oa.unissense.tech', 'Hub 基地址'),
|
||
('crm.idp.app-code', 'scc', '调用方应用编码'),
|
||
('crm.idp.portal-url', 'https://hub.oa.unissense.tech/', '统一门户地址(只回吐外链,不含密钥)'),
|
||
('crm.idp.sso.enabled', 'false', '免密登录灰度开关'),
|
||
('crm.idp.account-max-length', '20', 'Hub 账号长度上限(官方 §24.2)'),
|
||
('crm.idp.sync.enabled', 'true', '每日全量同步开关'),
|
||
('crm.idp.sync.member-limit', '5000', '目录单次人数上限'),
|
||
('crm.idp.provision.mode', 'all', '建号模式 all/allowlist/login'),
|
||
('crm.idp.provision.default-role-code', 'crm_xs','新建账号默认角色编码(SELF 范围,可逗号分隔写多个)'),
|
||
('crm.idp.increment.enabled', 'true', '增量同步开关'),
|
||
('crm.idp.increment.limit', '500', '增量单批条数上限'),
|
||
('crm.idp.guard.max-created-per-round', '5', '单轮新建人数上限'),
|
||
('crm.idp.guard.first-batch-created-must-be-zero','true', '首切批次新建数必须为 0'),
|
||
('crm.idp.guard.pulled-change-threshold', '50', '单轮变更量突增阈值'),
|
||
('crm.idp.guard.wecom-ratio-drop-threshold', '30', '企微绑定覆盖率跌幅阈值'),
|
||
('crm.idp.guard.max-disable-per-round', '10', '单轮停用人数上限'),
|
||
('crm.idp.guard.disable-after-rounds', '3', '连续缺失几轮才停用'),
|
||
('crm.idp.guard.auto-disable-enabled', 'false', '允许自动停用'),
|
||
('crm.idp.bind-token-ttl-seconds', '600', 'NEED_BIND 一次性 bindToken 寿命(秒)'),
|
||
-- 本地登录已收敛成内置账号(契约 §30):三档兑底的 crm.idp.login-fallback.mode / .ttl-minutes
|
||
-- 不再种子(代码已不读它们,插进去就是库里第一行没人读的孤儿参数)。
|
||
-- 内置账号只种子 enabled / username / reset-password 三项;password 刻意不种子,理由同
|
||
-- crm.idp.integration-key:明文口令进 git 等于多抄一份,而且 sys_param 优先级高于环境变量,
|
||
-- 种子一落,CRM_IDP_LOCAL_ADMIN_PASSWORD 就再也压不过它了。
|
||
('crm.idp.local-admin.enabled', 'true', '内置本地管理员入口开关'),
|
||
('crm.idp.local-admin.username', 'localadmin', '唯一能用本地口令的账号名'),
|
||
('crm.idp.local-admin.reset-password', 'false', '按配置重置内置口令的一次性开关');
|
||
|
||
do $$
|
||
declare
|
||
has_description boolean;
|
||
has_param_type boolean;
|
||
has_is_system boolean;
|
||
has_status boolean;
|
||
has_is_deleted boolean;
|
||
has_created boolean;
|
||
has_updated boolean;
|
||
statement text;
|
||
seed record;
|
||
v_sequence_name text;
|
||
inserted integer := 0;
|
||
rows integer := 0;
|
||
begin
|
||
if to_regclass('public.sys_param') is null then
|
||
raise exception 'public.sys_param 不存在,本脚本不适用(已整段跳过)';
|
||
end if;
|
||
|
||
-- 主键序列自检:开发库实测 sys_param 有 5 行 / max(param_id)=5 而序列 last_value=2,
|
||
-- 不自检就直接 INSERT 必撞 sys_param_pkey(本脚本空跑就是死在这)。
|
||
-- 守卫按判据 C 与 setval 写在同一条语句里;单独修库用 sql/20261119_idp_sys_param_setval.sql。
|
||
select pg_get_serial_sequence('public.sys_param', 'param_id') into v_sequence_name;
|
||
if 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_description from information_schema.columns
|
||
where table_schema = 'public' and table_name = 'sys_param' and column_name = 'description';
|
||
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_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 idp_seed order by param_key loop
|
||
-- 值一律 quote_literal 内联,不走 $n:带哪几列是探测出来的,参数个数会跟着变,
|
||
-- 而 EXECUTE ... USING 的个数与动态 SQL 里实际引用的 $n 不一致时 PG 直接报错
|
||
-- ("bind message supplies 3 parameters, but ... requires 2")。
|
||
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(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;
|
||
|
||
-- 存在即跳过:本仓没取证 param_key 上是否有唯一约束,所以不依赖 on conflict ——
|
||
-- 没有唯一约束时 on conflict do nothing 根本不拦,重复执行会多插一行。
|
||
statement := statement || ' where not exists (select 1 from sys_param p'
|
||
|| ' where p.param_key = ' || quote_literal(seed.param_key)
|
||
|| case when has_is_deleted
|
||
then ' and coalesce(p.is_deleted, 0) = 0' else '' end
|
||
|| ')';
|
||
|
||
execute statement;
|
||
get diagnostics inserted = row_count;
|
||
rows := rows + inserted;
|
||
end loop;
|
||
|
||
raise notice 'IdP sys_param 种子:候选 % 个键,本次新建 % 行(重复执行应为 0 行)',
|
||
(select count(*) from idp_seed), rows;
|
||
end
|
||
$$;
|
||
|
||
commit;
|
||
|
||
-- 回滚(等量删除:删的就是上面那批键,别的 sys_param 行一律不碰)。
|
||
-- 注意是逻辑删还是物理删:本脚本按框架的 @TableLogic 口径做逻辑删(is_deleted = 1);
|
||
-- 库里没有 is_deleted 列时下面这句会报错,那时改成 delete 即可,别静默跳过。
|
||
-- begin;
|
||
-- update sys_param set is_deleted = 1, updated_at = now()
|
||
-- where param_key in (
|
||
-- 'crm.idp.enabled','crm.idp.base-url','crm.idp.app-code','crm.idp.portal-url',
|
||
-- 'crm.idp.sso.enabled','crm.idp.account-max-length',
|
||
-- 'crm.idp.sync.enabled','crm.idp.sync.member-limit','crm.idp.provision.mode',
|
||
-- 'crm.idp.provision.default-role-code','crm.idp.increment.enabled','crm.idp.increment.limit',
|
||
-- 'crm.idp.guard.max-created-per-round','crm.idp.guard.first-batch-created-must-be-zero',
|
||
-- 'crm.idp.guard.pulled-change-threshold','crm.idp.guard.wecom-ratio-drop-threshold',
|
||
-- 'crm.idp.guard.max-disable-per-round','crm.idp.guard.disable-after-rounds',
|
||
-- 'crm.idp.guard.auto-disable-enabled','crm.idp.bind-token-ttl-seconds',
|
||
-- 'crm.idp.local-admin.enabled','crm.idp.local-admin.username',
|
||
-- 'crm.idp.local-admin.reset-password')
|
||
-- and coalesce(is_deleted, 0) = 0;
|
||
-- commit;
|