unis_crm/sql/20261123_idp_config_db_only...

135 lines
8.7 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

-- 统一认证(Hub)配置整体搬进数据库:一次性核对 / 清尾脚本(可选,不在启动期自动执行)
--
-- 背景(2026-11-23):
-- 1) 管理台「系统管理 / 统一认证配置」页现在可以编辑全部统一认证参数,保存即生效,不发版不重启。
-- 2) 唯一事实源是 sys_param 的同名键(crm.idp.*)。backend/src/main/resources/application.yml 里
-- 那一整块 crm.idp 已经删掉,配置文件不再保存任何统一认证参数。
-- 3) 空库首次启动由后端 IdpConfigBootstrapInitializer 自动把清单里的键按启动默认种进 sys_param,
-- 所以"种一次"这件事不需要人工跑 SQL。本脚本只做两件自动流程做不了的事:核对 + 清尾。
--
-- 本脚本做什么:
-- A. 核对清单完整性:逐个列出配置清单里的键,标出库里有没有行、当前值、是不是被框架缓存挡住了。
-- B. 清掉三类"配了但没人读"的孤儿行(逻辑删,与框架 @TableLogic 口径一致),它们会出现在
-- 配置页顶部的「配了但不生效」提示里:
-- - crm.idp.login-fallback.* 三档兑底作废(契约 §30)
-- - crm.idp.increment.interval-seconds 文档值,真正生效的是 crm.idp.increment.interval-ms
-- - crm.idp.ticket.hub-ttl-seconds 只用于展示,票据寿命由 Hub 决定
-- - crm.idp.sso.enforce 2026-11-19 拍板删键
-- C. 序列自检:只在"要新建覆盖行"时有意义,写路径后端已自带 alignParamIdSequence;
-- 本脚本只读不写覆盖行,所以这里只报状态、不动序列(要修用 sql/20261119_idp_sys_param_setval.sql)。
--
-- 幂等:全部语句可重复执行;B 段只逻辑删那四个键,别的 sys_param 行一律不碰。
-- 执行:psql -d <db> -f sql/20261123_idp_config_db_only.sql
-- (只想核对不删东西,就只跑 A 段,或把 B 段的 UPDATE 先注释掉看命中行数。)
--
-- 注意(框架缓存):sys_param 读侧第一层是 Redis(键 sys:param:<param_key>,实测正缓存 TTL 86400s、
-- 负缓存 EMPTY_MARKER 300s)。本脚本直接改库不会同步那份缓存,所以手工改完之后:
-- 要么走管理台保存一次(后端会 syncParamToCache),
-- 要么手工 del Redis 里的 sys:param:crm.idp.<...>,
-- 要么等缓存过期。只改库不处理缓存 = 最长 24 小时不生效。
-- ---------------------------------------------------------------- A. 核对清单完整性
-- 清单的权威定义在 backend/src/main/java/com/unis/crm/service/IdpRuntimeConfig.java 的 CATALOG。
-- 下面这张表是它的手工镜像(44 项),用来在不启动应用的情况下核对库。漂了以代码为准。
with catalog (param_key) as (
values
('crm.idp.enabled'), ('crm.idp.base-url'), ('crm.idp.app-code'),
('crm.idp.integration-key'), ('crm.idp.portal-url'), ('crm.idp.sso.enabled'),
('crm.idp.account-max-length'),
('crm.idp.sync.enabled'), ('crm.idp.sync.cron'), ('crm.idp.sync.member-limit'),
('crm.idp.provision.mode'), ('crm.idp.provision.hub-org-allowlist'),
('crm.idp.provision.default-role-code'),
('crm.idp.increment.enabled'), ('crm.idp.increment.interval-ms'), ('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.local-admin.enabled'), ('crm.idp.local-admin.username'),
('crm.idp.local-admin.password'), ('crm.idp.local-admin.reset-password'),
('crm.idp.bind-token-ttl-seconds'),
('crm.idp.sso.excluded-accounts'), ('crm.idp.sso.state-required'),
('crm.idp.increment.first-delay-ms'), ('crm.idp.sync-timeout-ms'), ('crm.idp.sso-timeout-ms'),
('crm.idp.connect-timeout-ms'), ('crm.idp.max-inflight'),
('crm.idp.circuit-breaker-failure-threshold'), ('crm.idp.circuit-breaker-open-ms'),
('crm.idp.circuit-breaker-probe-interval-ms'), ('crm.idp.tenant-id'),
('crm.idp.schema.enabled'), ('crm.idp.ticket.require-ntp'),
('crm.idp.increment.max-consecutive-failures'), ('crm.idp.trust-forwarded-for'),
('crm.idp.local-login-guard.enabled')
), live as (
select param_key, param_value
from sys_param
where param_key like 'crm.idp.%'
and coalesce(is_deleted, 0) = 0
)
select c.param_key,
case when l.param_key is null then 'MISSING(会用启动默认;下一次启动会被自动补种)'
when l.param_value is null or btrim(l.param_value) = '' then 'EMPTY(清除覆盖后的空行)'
else 'OK' end as state,
case when c.param_key in ('crm.idp.integration-key', 'crm.idp.local-admin.password')
then '****(密钥类不在 SQL 里回显原值)'
else left(coalesce(l.param_value, ''), 80) end as value_preview
from catalog c
left join live l on l.param_key = c.param_key
order by state desc, c.param_key;
-- 库里多出来、但清单里已经没有的 crm.idp.* 行(改过名/作废的键都长这样)。
select param_key, left(coalesce(param_value, ''), 80) as value_preview, updated_at
from sys_param
where param_key like 'crm.idp.%'
and coalesce(is_deleted, 0) = 0
and param_key not in (
'crm.idp.enabled','crm.idp.base-url','crm.idp.app-code','crm.idp.integration-key',
'crm.idp.portal-url','crm.idp.sso.enabled','crm.idp.account-max-length',
'crm.idp.sync.enabled','crm.idp.sync.cron','crm.idp.sync.member-limit',
'crm.idp.provision.mode','crm.idp.provision.hub-org-allowlist','crm.idp.provision.default-role-code',
'crm.idp.increment.enabled','crm.idp.increment.interval-ms','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.local-admin.enabled','crm.idp.local-admin.username','crm.idp.local-admin.password',
'crm.idp.local-admin.reset-password','crm.idp.bind-token-ttl-seconds',
'crm.idp.sso.excluded-accounts','crm.idp.sso.state-required','crm.idp.increment.first-delay-ms',
'crm.idp.sync-timeout-ms','crm.idp.sso-timeout-ms','crm.idp.connect-timeout-ms','crm.idp.max-inflight',
'crm.idp.circuit-breaker-failure-threshold','crm.idp.circuit-breaker-open-ms',
'crm.idp.circuit-breaker-probe-interval-ms','crm.idp.tenant-id','crm.idp.schema.enabled',
'crm.idp.ticket.require-ntp','crm.idp.increment.max-consecutive-failures',
'crm.idp.trust-forwarded-for','crm.idp.local-login-guard.enabled')
order by param_key;
-- ---------------------------------------------------------------- B. 清掉四类"配了但没人读"的孤儿行
-- 没有 is_deleted 列的库请把下面这句换成 delete from sys_param where param_key in (...)。
begin;
create temporary table idp_retired (param_key text primary key, why text not null) on commit drop;
insert into idp_retired (param_key, why) values
('crm.idp.login-fallback.mode', '三档兑底已作废(契约 §30),本地口令只剩内置账号'),
('crm.idp.login-fallback.ttl-minutes', '同上'),
('crm.idp.increment.interval-seconds', '纯文档值,真正生效的是 crm.idp.increment.interval-ms'),
('crm.idp.ticket.hub-ttl-seconds', '只用于展示,票据寿命由 Hub 决定'),
('crm.idp.sso.enforce', '2026-11-19 拍板删键:无消费方的可写开关就是陷阱');
update sys_param p
set is_deleted = 1,
updated_at = now()
from idp_retired r
where p.param_key = r.param_key
and coalesce(p.is_deleted, 0) = 0;
commit;
-- 想物理删(连行一起清掉)就改用这句,别两条都跑:
-- begin;
-- delete from sys_param where param_key in (
-- 'crm.idp.login-fallback.mode','crm.idp.login-fallback.ttl-minutes',
-- 'crm.idp.increment.interval-seconds','crm.idp.ticket.hub-ttl-seconds','crm.idp.sso.enforce');
-- commit;
-- ---------------------------------------------------------------- C. 回滚
-- B 段只把那四个键置 is_deleted=1,回滚就是把它们置回来(只影响 B 段命中的行):
-- begin;
-- update sys_param set is_deleted = 0, updated_at = now()
-- where param_key in ('crm.idp.login-fallback.mode','crm.idp.login-fallback.ttl-minutes',
-- 'crm.idp.increment.interval-seconds','crm.idp.ticket.hub-ttl-seconds','crm.idp.sso.enforce');
-- commit;