274 lines
17 KiB
PL/PgSQL
274 lines
17 KiB
PL/PgSQL
-- =============================================================================
|
||
-- 2026-11-23 统一认证(Hub)上线脚本执行状态体检(只读,不写一行数据)
|
||
-- =============================================================================
|
||
-- 干什么:把 2026-11 这批脚本各自「留下的痕迹」列成探针,在当前库里逐个现查,
|
||
-- 直接回答「这套库上还有哪些 SQL 需要执行」。
|
||
-- 为什么需要它:这批脚本全靠幂等兜重复执行,但幂等不等于「知道跑没跑」——
|
||
-- dev 库实测 idp:mapping:write 这一行压根没建出来(绑定/解绑全角色 403),
|
||
-- 而结构层的列是后端启动器补建的,看结构「有」会误判成脚本跑过。
|
||
-- 判据只能落到「只有这份 SQL 会建出来的对象在不在」。
|
||
-- 怎么读结果:
|
||
-- 结果集 1(按上线顺序,一份脚本一行)
|
||
-- missing_probes = 0 -> 痕迹全在,看起来已执行
|
||
-- present_probes = 0 且 total>0 -> 痕迹全无,这份脚本在这套库上没执行过
|
||
-- 两边都有 -> 跑过旧版本或只跑了一半,看结果集 2 逐条核
|
||
-- 结果集 2:缺失明细(拿着它就知道该补跑哪几份、缺哪一个对象)
|
||
-- 结果集 3:宿主表/列不存在的探针(新库里 host_object_missing 是常态,不是故障)
|
||
-- 结果集 4:把「跑完权限 SQL 必须清缓存」这句提醒打在输出里,免得只留在注释里
|
||
-- 已知盲区(如实登记,别把「在位」当「跑过 SQL」):
|
||
-- * 结构层(列/表/普通索引)后端启动器 IdpSchemaInitializer 也能补建 ⇒ 在位只等于
|
||
-- 「结构不缺」。所以合并脚本的判据里额外放了只有 SQL 会加的 4 项:
|
||
-- 两条 CHECK 约束 + 守卫函数与触发器(启动器刻意不加 CHECK)。
|
||
-- * localadmin 那一行启动自愈(IdpLocalAdminAccountService)也会写 ⇒ 同理只算「账号在位」。
|
||
-- * 纯 UPDATE/INSERT 型修数脚本(20260828update.sql、20260903update.sql、
|
||
-- 20260916_mcp_column_comments_pg17.sql、20261029_prod_sequence_sync_pg16.sql 等)
|
||
-- 没有独有结构痕迹,本体检看不见它们,要另看业务数据或执行台账。
|
||
-- 独立性:探针走 pg_temp 函数 + undefined_table 兜底,所以在「一张表都还没建」的新库上
|
||
-- 也不会以 column does not exist 整篇失败;代价是会话里留下几个 pg_temp.* 函数,
|
||
-- 断开连接自动消失。
|
||
-- 执行:psql -d <db> -f sql/20261123_idp_apply_check.sql
|
||
-- 换库只改 -d,脚本不写死库名;不建永久表、不加锁、可反复跑。
|
||
-- =============================================================================
|
||
|
||
set search_path to public;
|
||
|
||
-- 宿主对象可能不存在的探针走函数:函数体在首次执行时才规划,
|
||
-- 缺表/缺列被 exception 兜成 null(=「无法判定」),而不是把整篇体检打断。
|
||
create or replace function pg_temp.crm_probe_permission(p_code text)
|
||
returns bigint
|
||
language plpgsql
|
||
as $fn$
|
||
begin
|
||
return (select count(*) from sys_permission sp
|
||
where sp.code = p_code and coalesce(sp.is_deleted, 0) = 0);
|
||
exception
|
||
when undefined_table then return null;
|
||
end;
|
||
$fn$;
|
||
|
||
create or replace function pg_temp.crm_probe_local_login_open(p_username text)
|
||
returns bigint
|
||
language plpgsql
|
||
as $fn$
|
||
begin
|
||
return (select count(*) from sys_user u
|
||
where u.username = p_username
|
||
and coalesce(u.is_deleted, 0) = 0
|
||
and coalesce(u.local_login_enabled, false));
|
||
exception
|
||
when undefined_column or undefined_table then return null;
|
||
end;
|
||
$fn$;
|
||
|
||
create or replace function pg_temp.crm_probe_param_row(p_key text)
|
||
returns bigint
|
||
language plpgsql
|
||
as $fn$
|
||
begin
|
||
return (select count(*) from sys_param sp
|
||
where sp.param_key = p_key and coalesce(sp.is_deleted, 0) = 0);
|
||
exception
|
||
when undefined_table then return null;
|
||
end;
|
||
$fn$;
|
||
|
||
-- 序列自检没有结构痕迹,只能问「序列还落不落后于表」:
|
||
-- 落后 = 没跑过自检(或手工插过显式 param_id),首次保存配置必撞 sys_param_pkey。
|
||
create or replace function pg_temp.crm_probe_param_seq_ahead()
|
||
returns bigint
|
||
language plpgsql
|
||
as $fn$
|
||
declare
|
||
v_seq text;
|
||
v_last bigint;
|
||
v_max bigint;
|
||
begin
|
||
if to_regclass('public.sys_param') is null then
|
||
return null;
|
||
end if;
|
||
-- 序列名不硬猜:按列的真实归属取(改名、换 schema 都能拿到)
|
||
select pg_get_serial_sequence('public.sys_param', 'param_id') into v_seq;
|
||
if v_seq is null or to_regclass(v_seq) is null then
|
||
return null;
|
||
end if;
|
||
select coalesce(max(param_id), 0) into v_max from public.sys_param;
|
||
if v_max = 0 then
|
||
return 1;
|
||
end if;
|
||
-- 不能拿 v_seq 去比 pg_sequences.sequencename:前者带 schema 限定(public.xxx_seq),
|
||
-- 后者只有裸名,比出来永远 0 行 -> 读数永远是「落后」。这里直接对取到的序列名取值。
|
||
execute format('select last_value from %s', v_seq) into v_last;
|
||
return case when coalesce(v_last, 0) >= v_max then 1 else 0 end;
|
||
exception
|
||
when undefined_object or undefined_table or insufficient_privilege then return null;
|
||
end;
|
||
$fn$;
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 结果集 1:按上线顺序,一份脚本一行
|
||
-- -----------------------------------------------------------------------------
|
||
with probe(script_file, stage, kind, obj, host_table, sql_only) as (
|
||
values
|
||
-- 1 结构层合并脚本
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'column', 'hub_user_id', 'sys_user', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'column', 'hub_account', 'sys_user', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'column', 'hub_display_name', 'sys_user', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'column', 'hub_mobile', 'sys_user', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'column', 'hub_org_id', 'sys_user', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'column', 'hub_synced_at', 'sys_user', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'column', 'account_source', 'sys_user', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'column', 'password_locally_managed', 'sys_user', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'column', 'local_login_enabled', 'sys_user', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'index', 'uk_sys_user_hub_user_id', 'sys_user', false),
|
||
-- 只有 SQL 会加这两条 CHECK(启动器刻意不加)⇒ 它们才是「跑过合并脚本」的真判据
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'constraint', 'ck_sys_user_account_source', 'sys_user', true),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'constraint', 'ck_sys_user_local_login_needs_local_password', 'sys_user', true),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'table', 'crm_idp_sync_run', null, false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'table', 'crm_sync_change_log', null, false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'table', 'crm_idp_pending', null, false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'index', 'idx_idp_pending_ignored', 'crm_idp_pending', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'function', 'crm_guard_sys_user_pk', null, false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'function', 'crm_guard_sys_user_delete', null, false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'trigger', 'trg_guard_sys_user_pk', 'sys_user', false),
|
||
('20261123_idp_schema_upgrade_pg17.sql', '1 结构', 'trigger', 'trg_guard_sys_user_delete', 'sys_user', false),
|
||
-- 2 sys_param 主键序列自检
|
||
('20261119_idp_sys_param_setval.sql', '2 序列', 'param-seq-ahead', 'sys_param', null, true),
|
||
-- 3~5 权限码:只有 SQL 会建 ⇒ 真判据
|
||
('20261119_idp_menu_perm.sql', '3 权限', 'permission', 'menu:idp-config', null, true),
|
||
('20261119_idp_config_perms.sql', '3 权限', 'permission', 'idp:config:read', null, true),
|
||
('20261119_idp_config_perms.sql', '3 权限', 'permission', 'idp:config:write', null, true),
|
||
('20261120_idp_mapping_write_perm.sql', '3 权限', 'permission', 'idp:mapping:write', null, true),
|
||
-- 4 内置本地管理员 + 本地登录放开
|
||
('20261120_idp_local_admin_backfill.sql', '4 账号', 'account', 'localadmin', null, false),
|
||
-- 探针挑 admin_crm 而不是 admin:admin 已被脚本自己的判据挡在门外(它有 hub_user_id 锚点,
|
||
-- 「已绑到 Hub 的账号不许被悄悄开出一条本地后门」),跑没跑都不会打开它;
|
||
-- admin_crm 没有锚点,只有这份 SQL 会打开它的本地登录位 => 它才是这份脚本的真判据。
|
||
('20261120_idp_local_login_flags.sql', '4 账号', 'local-login-open', 'admin_crm', null, true),
|
||
-- 5 管理端门户门禁
|
||
('20261119_admin_portal_perm.sql', '5 门户', 'permission', 'menu:admin-portal', null, true),
|
||
-- 6 手工预写映射(可选):对照表只有那份脚本会建
|
||
('20261118_idp_premark_manual_mapping.sql', '6 映射', 'table', 'crm_idp_premark', null, true),
|
||
-- 7 可选护栏:sys_log 口令掩码触发器
|
||
('20261119_idp_sys_log_password_mask.sql', '7 护栏', 'trigger', 'crm_sys_log_mask_password', 'sys_log', true),
|
||
-- 附:统一认证配置是否已由后端接管(启动期自动,不需要人跑 SQL)
|
||
('(后端自动)IdpConfigBootstrapInitializer', '8 配置', 'param-row', 'crm.idp.enabled', null, true)
|
||
),
|
||
checked as (
|
||
select p.script_file, p.stage, p.kind, p.obj, p.sql_only,
|
||
case p.kind
|
||
when 'table' then (select count(*) from pg_class c
|
||
join pg_namespace n on n.oid = c.relnamespace
|
||
where n.nspname = current_schema()
|
||
and c.relname = p.obj and c.relkind = 'r')
|
||
when 'column' then (select count(*) from information_schema.columns ic
|
||
where ic.table_schema = current_schema()
|
||
and ic.table_name = p.host_table and ic.column_name = p.obj)
|
||
when 'index' then (select count(*) from pg_indexes pi
|
||
where pi.schemaname = current_schema()
|
||
and pi.tablename = p.host_table and pi.indexname = p.obj)
|
||
when 'constraint' then (select count(*) from pg_constraint con
|
||
join pg_class c on c.oid = con.conrelid
|
||
where con.conname = p.obj and c.relname = p.host_table)
|
||
when 'function' then (select count(*) from pg_proc pr
|
||
join pg_namespace n on n.oid = pr.pronamespace
|
||
where n.nspname = current_schema() and pr.proname = p.obj)
|
||
when 'trigger' then (select count(*) from pg_trigger t
|
||
join pg_class c on c.oid = t.tgrelid
|
||
where t.tgname = p.obj and c.relname = p.host_table
|
||
and not t.tgisinternal)
|
||
when 'account' then (select count(*) from sys_user u
|
||
where u.username = p.obj and coalesce(u.is_deleted, 0) = 0)
|
||
when 'permission' then pg_temp.crm_probe_permission(p.obj)
|
||
when 'local-login-open' then pg_temp.crm_probe_local_login_open(p.obj)
|
||
when 'param-row' then pg_temp.crm_probe_param_row(p.obj)
|
||
when 'param-seq-ahead' then pg_temp.crm_probe_param_seq_ahead()
|
||
end as hits
|
||
from probe p
|
||
)
|
||
select script_file as "脚本",
|
||
stage as "阶段",
|
||
count(*) as "探针数",
|
||
count(*) filter (where coalesce(hits, 0) > 0) as "在位",
|
||
count(*) filter (where hits = 0) as "缺失",
|
||
count(*) filter (where hits is null) as "宿主对象不在",
|
||
case when count(*) filter (where hits = 0) = 0
|
||
and count(*) filter (where hits is null) = 0 then '痕迹全在 -> 看起来已执行'
|
||
when count(*) filter (where coalesce(hits, 0) > 0) = 0 then '痕迹全无 -> 需要跑'
|
||
when count(*) filter (where hits is null) > 0
|
||
and count(*) filter (where coalesce(hits, 0) > 0) = 0 then '宿主对象都还没建 -> 需要跑(新库)'
|
||
else '部分在位 -> 看结果集 2/3 逐条核' end as "结论"
|
||
from checked
|
||
group by script_file, stage
|
||
-- 按阶段排序 = 按上线顺序读,不用对着表头找「先看哪份」
|
||
order by stage, script_file;
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 结果集 2:缺失明细
|
||
-- -----------------------------------------------------------------------------
|
||
with probe(script_file, kind, obj, host_table) as (
|
||
values
|
||
('20261123_idp_schema_upgrade_pg17.sql', 'constraint', 'ck_sys_user_account_source', 'sys_user'),
|
||
('20261123_idp_schema_upgrade_pg17.sql', 'constraint', 'ck_sys_user_local_login_needs_local_password', 'sys_user'),
|
||
('20261123_idp_schema_upgrade_pg17.sql', 'table', 'crm_idp_sync_run', null),
|
||
('20261123_idp_schema_upgrade_pg17.sql', 'table', 'crm_sync_change_log', null),
|
||
('20261123_idp_schema_upgrade_pg17.sql', 'table', 'crm_idp_pending', null),
|
||
('20261123_idp_schema_upgrade_pg17.sql', 'index', 'idx_idp_pending_ignored', 'crm_idp_pending'),
|
||
('20261123_idp_schema_upgrade_pg17.sql', 'trigger', 'trg_guard_sys_user_pk', 'sys_user'),
|
||
('20261123_idp_schema_upgrade_pg17.sql', 'trigger', 'trg_guard_sys_user_delete', 'sys_user'),
|
||
('20261119_idp_menu_perm.sql', 'permission', 'menu:idp-config', null),
|
||
('20261119_idp_config_perms.sql', 'permission', 'idp:config:read', null),
|
||
('20261119_idp_config_perms.sql', 'permission', 'idp:config:write', null),
|
||
('20261120_idp_mapping_write_perm.sql', 'permission', 'idp:mapping:write', null),
|
||
('20261120_idp_local_admin_backfill.sql', 'account', 'localadmin', null),
|
||
('20261120_idp_local_login_flags.sql', 'local-login-open', 'admin_crm', null),
|
||
('20261119_admin_portal_perm.sql', 'permission', 'menu:admin-portal', null),
|
||
('20261118_idp_premark_manual_mapping.sql', 'table', 'crm_idp_premark', null),
|
||
('20261119_idp_sys_log_password_mask.sql', 'trigger', 'crm_sys_log_mask_password', 'sys_log')
|
||
),
|
||
checked as (
|
||
select p.script_file, p.kind, p.obj,
|
||
case p.kind
|
||
when 'table' then (select count(*) from pg_class c
|
||
join pg_namespace n on n.oid = c.relnamespace
|
||
where n.nspname = current_schema()
|
||
and c.relname = p.obj and c.relkind = 'r')
|
||
when 'index' then (select count(*) from pg_indexes pi
|
||
where pi.schemaname = current_schema()
|
||
and pi.tablename = p.host_table and pi.indexname = p.obj)
|
||
when 'constraint' then (select count(*) from pg_constraint con
|
||
join pg_class c on c.oid = con.conrelid
|
||
where con.conname = p.obj and c.relname = p.host_table)
|
||
when 'trigger' then (select count(*) from pg_trigger t
|
||
join pg_class c on c.oid = t.tgrelid
|
||
where t.tgname = p.obj and c.relname = p.host_table
|
||
and not t.tgisinternal)
|
||
when 'account' then (select count(*) from sys_user u
|
||
where u.username = p.obj and coalesce(u.is_deleted, 0) = 0)
|
||
when 'permission' then pg_temp.crm_probe_permission(p.obj)
|
||
when 'local-login-open' then pg_temp.crm_probe_local_login_open(p.obj)
|
||
end as hits
|
||
from probe p
|
||
)
|
||
select script_file as "脚本", kind as "探针类型", obj as "缺失对象"
|
||
from checked
|
||
where hits = 0
|
||
order by script_file, kind, obj;
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 结果集 3:宿主表/列还不存在的探针(新库上正常,说明这套库连基础表都还没建全)
|
||
-- -----------------------------------------------------------------------------
|
||
select 'local-login-open' as "探针类型",
|
||
'sys_user.local_login_enabled' as "宿主对象",
|
||
case when exists (select 1 from information_schema.columns
|
||
where table_name = 'sys_user' and column_name = 'local_login_enabled')
|
||
then '在位' else '不在(结构层还没跑)' end as "状态"
|
||
where not exists (select 1 from information_schema.columns
|
||
where table_name = 'sys_user' and column_name = 'local_login_enabled');
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 结果集 4:跑完之后别忘了的事
|
||
-- -----------------------------------------------------------------------------
|
||
select '跑过上面任何一份权限/授权 SQL 之后,必须清 Redis 键 sys:auth:perm*('
|
||
|| 'sys:auth:perm:{u}:{t}:{n} 管按钮判权、sys:auth:perm:list:{u}:{t} 管菜单清单,两族一起清),'
|
||
|| '重新登录不刷新,不清最长 45 分钟不生效' as "别忘了";
|