-- 20261120 sys_user 账号来源标识 account_source(把"这一行归谁管"从推断变成数据) -- -- 干什么:给 sys_user 加一列 account_source,只有两个取值 —— -- 'hub' 统一认证(Hub)同步建出来的号,或本地老号被认领/人工映射之后交出去归 Hub 管的: -- 改名、调部门、停用都跟着 Hub 走; -- 'local' 本地账号,同步不碰。 -- 默认 'local',含义是"没人接管 = 本地自理":新环境、漏跑回填、以后手工建的号, -- 都不会因为这一列的存在而被误当成"交给 Hub 了"。 -- -- 为什么要单独一列:hub_user_id 只说明"认到了 Hub 里的哪个人",说明不了"谁说了算"。 -- 同步侧一直是拿"hub_user_id 非空"当托管判据在推断,这个推断有三处对不上: -- ① 平台管理员行可能被历史数据带上过 hub_user_id,推断会把它交给 Hub 停用(钥匙丢失); -- ② 人工解绑把 hub_* 清空,推断又退回本地,中间没有任何地方留下"这个人曾经/现在归谁管"; -- ③ 用户管理页要显示"同步进来的 / 原来就有的",让人对着 hub_user_id 空不空猜, -- 等于把判据摊到前端再算一遍(前端 idp.ts 已按 account_source + is_platform_admin 两个键写好了消费口径)。 -- ⇒ 归属单独存一列,同步、页面、审计读同一份事实。 -- -- 硬红线:is_platform_admin = true 的行**永远**是 'local'。这条就是"别和原有管理员账号冲突"的落点 —— -- 应急钥匙一旦因为一次同步或一次误点变成 'hub',Hub 哪天停用这个号,人就再也进不来, -- 而且退不回去(unisoa 那边为此单独设了 hub_sync_protect_admins 与 is_emergency_account, -- 见 backend/app/service/hub_sync.py 顶部"本地账号的边界"与 is_emergency_account(): -- source='hub' 的账号才会被同步改名/换部门/停用,超管与应急账号被排除在接管之外)。 -- 回填判据里显式写死 coalesce(is_platform_admin, false) = false,末尾自查把违规行数出来。 -- -- 回填判据(原文,与 IdpUserMappingService.bind / IdpUserResolver.claim 的写入判据同一口径): -- coalesce(is_deleted, 0) = 0 且 hub_user_id is not null 且 coalesce(is_platform_admin, false) = false -- 的行 → 'hub',其余留 'local';软删行只报数不动。 -- 为什么用 hub_user_id 推:它是库里既有唯一事实(认人阶梯一级锚点),与同步侧"非空即托管"完全同判据, -- 回填不会造出"标了 hub 但同步不认"的第二套标准。 -- 为什么要写 is_deleted:uk_sys_user_hub_user_id 的谓词是 where hub_user_id is not null, -- **不带 is_deleted = 0**(见 IdpSchemaInitializer 里那条 create unique index),软删行照样占着锚点。 -- 它们是"人已离职/已一键归位搬空"的尸体:同步不碰、用户列表不显示,给它们标 hub 只是往归属统计里 -- 掺进一批永远不会再动的行;但它们占用的锚点是运维真会撞的坑(人工映射抢锚点报"已绑定"), -- 所以数量必须单独报出来,让人看得见。 -- 为什么写 coalesce(is_deleted, 0):is_deleted 为 null 的行库里存在("没删"才是缺省), -- 裸写 is_deleted = 0 会把这些行静默漏掉。 -- 为什么不动 updated_at:回填的判据来自库里已有的列,业务上没人改过这些行; -- 把上百行的"最后修改时间"刷成今天,用户列表看起来就像今天被集体编辑过一遍。 -- -- 幂等:add column if not exists;约束用 do 块判重名;回填 update 带 account_source <> 'hub', -- 第二遍结构零变化、回填 0 行。 -- 事务:整份一个事务(begin/commit 自带),半途失败不会留下"有列没回填"的中间态。 -- 执行:本机没有 psql,运维用 JDBC(或临时 Java 类)整份发一次。两条真会踩的注意: -- ① do $$ ... $$ 体里的分号不能被按 ';' 裸切 —— 脚本切分器必须感知 dollar-quote, -- 否则 PL/pgSQL 体会被拦腰截断(JDBC 跑迁移最常见的坑)。最省事:整个文件作为一条 statement -- 发给 pgjdbc(simple query 模式,驱动本身按 dollar-quote 拆),或至少保证 begin/commit 成对。 -- ② 末尾那条自查 select 会返回结果集,用 execute() 时记得读 ResultSet; -- 嫌麻烦就在发之前把它注释掉,不影响迁移本身。 -- 回滚:见文件末尾注释(先删约束再删列,两条都可反复跑)。 begin; -- 跑前先把"这次到底改没改结构"报出来:运维在 JDBC 日志里能一眼看见是首次执行还是重复执行。 do $$ declare v_exists boolean; begin select exists ( select 1 from information_schema.columns where table_schema = current_schema() and table_name = 'sys_user' and column_name = 'account_source' ) into v_exists; if v_exists then raise notice '列 sys_user.account_source 已存在,本次不动结构,只回填'; else raise notice '列 sys_user.account_source 不存在,本次新建(varchar(16) not null default local)'; end if; end $$; alter table sys_user add column if not exists account_source varchar(16) not null default 'local'; comment on column sys_user.account_source is '账号归属:hub=统一认证同步建的号或已交给统一认证接管(改名/调部门/停用跟 Hub 走);local=本地账号,同步不碰;is_platform_admin=true 的行恒为 local'; do $$ begin if not exists ( select 1 from pg_constraint where conrelid = 'sys_user'::regclass and conname = 'ck_sys_user_account_source' ) then alter table sys_user add constraint ck_sys_user_account_source check (account_source in ('local', 'hub')); raise notice '已加约束 ck_sys_user_account_source(只许 local / hub)'; else raise notice '约束 ck_sys_user_account_source 已存在,跳过'; end if; end $$; do $$ declare v_backfilled integer; v_soft_anchor integer; v_hub integer; v_local integer; v_admin_hub integer; v_drift integer; v_names text; begin if to_regclass('public.sys_user') is null then raise notice 'public.sys_user 不存在,本脚本不适用(已整段跳过)'; return; end if; -- 回填:活跃、已交给 Hub 管、又不是平台管理员的行。account_source <> 'hub' 让第二遍跑变化 0 行。 update sys_user set account_source = 'hub' where coalesce(is_deleted, 0) = 0 and hub_user_id is not null and coalesce(is_platform_admin, false) = false and account_source <> 'hub'; get diagnostics v_backfilled = row_count; raise notice 'account_source 回填:活跃行改写 % 行' '(判据:coalesce(is_deleted, 0) = 0 且 hub_user_id is not null 且 is_platform_admin 不为 true)', v_backfilled; -- 软删行只报数:它们占着 hub_user_id,归属位停在 local 不动(同步与页面都不看它们)。 select count(*) into v_soft_anchor from sys_user where coalesce(is_deleted, 0) <> 0 and hub_user_id is not null; raise notice 'account_source 回填:软删行 % 行不改动,它们仍占着 hub_user_id 锚点' '(uk_sys_user_hub_user_id 的谓词不带 is_deleted = 0,人工映射抢同一个 Hub 账号时会撞这个唯一索引)', v_soft_anchor; select count(*) into v_hub from sys_user where account_source = 'hub'; select count(*) into v_local from sys_user where account_source = 'local'; raise notice '现采:account_source=hub 共 % 行,account_source=local 共 % 行', v_hub, v_local; -- 红线现采证据:平台管理员被标成 hub 的行数必须为 0。 select count(*), string_agg(username, ',' order by username) into v_admin_hub, v_names from sys_user where account_source = 'hub' and coalesce(is_platform_admin, false); if v_admin_hub > 0 then raise warning '有 % 行 is_platform_admin=true 却是 account_source=hub,应急钥匙可能被 Hub 一次停用拿走: %', v_admin_hub, coalesce(v_names, ''); else raise notice '红线自检通过:没有任何 is_platform_admin=true 的行被标成 hub'; end if; -- 漂移读数:活跃行里标着 local 却带着 Hub 锚点(且不是平台管理员) -- = 有写入口只写了 hub_user_id 没写归属位。软删行不算漂移,它们本就该停在 local。 select count(*) into v_drift from sys_user where account_source = 'local' and coalesce(is_deleted, 0) = 0 and hub_user_id is not null and coalesce(is_platform_admin, false) = false; if v_drift > 0 then raise warning '有 % 行带 hub_user_id 却仍是 account_source=local(回填之后再出现 = 某个写入口漏写归属位)', v_drift; end if; end $$; commit; -- 跑完自查:illegal_values / admin_is_hub / hub_without_anchor 必须都是 0。 -- local_with_anchor 只看活跃行,是"托管标识漂移了几行"的读数:本文件跑完应当是 0, -- 之后变正说明有新写入口只写 hub_user_id 不写 account_source(建号/认领/人工映射三处都写, -- 已绑定行的属性对齐也自愈:IdpUserResolver.applySnapshot 见到这种行会把它纠回 'hub' 并留痕, -- 所以漏跑本文件只会让页面错到下一轮同步,不会永远错下去——但清单里查无此人(ORPHAN)的行 -- 不走属性对齐,只有这一趟回填能救它们)。 -- soft_anchored_deleted 不参与判定,只把"有多少锚点被软删行占着"摆在台面上。 select (select count(*) from sys_user where account_source = 'hub') as hub_accounts, (select count(*) from sys_user where account_source = 'local') as local_accounts, (select count(*) from sys_user where account_source not in ('local', 'hub')) as illegal_values, (select count(*) from sys_user where account_source = 'hub' and coalesce(is_platform_admin, false)) as admin_is_hub, (select count(*) from sys_user where account_source = 'hub' and hub_user_id is null) as hub_without_anchor, (select count(*) from sys_user where account_source = 'local' and coalesce(is_deleted, 0) = 0 and hub_user_id is not null and coalesce(is_platform_admin, false) = false) as local_with_anchor, (select count(*) from sys_user where coalesce(is_deleted, 0) <> 0 and hub_user_id is not null) as soft_anchored_deleted; -- 回滚(两条都可反复跑;第二条例其实多余 —— 删列会连带把 check 约束带走,留着是为了 -- 运维先手工把列改成可空/删约束时仍能一键收尾)。 -- account_source 是从 hub_user_id 派生出来的标识,删掉它不丢任何原始事实,回滚安全。 -- 唯一顺序要求:**先把后端退回不读这一列的版本,再删列**。本轮后端有一处 select 它 -- (IdpUserSyncService.listUserHubMapping,用户管理页那一列的数据源), -- 列没了还跑新后端 = 用户管理页整表 500(symptom 是 column u.account_source does not exist)。 -- alter table sys_user drop constraint if exists ck_sys_user_account_source; -- alter table sys_user drop column if exists account_source;