Files
SupplierSystem/migrations/20260916_role_fk_to_definitions.sql
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

70 lines
3.0 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 角色/权限表补外键,杜绝「字典之外的角色」孤儿
--
-- 背景(2026-09-16 账号 × 角色权限 × 分公司 梳理):
-- public.user_roles.role 与 public.role_permissions.role 都是裸字符串列,**不引用**
-- public.role_definitions,因此数据库层面无法阻止「字典里没有的角色」被写进去。
-- 应用层 server/src/services/roleDefinitionService.ts 的 deleteRoleDefinition 已有守卫
-- (内置角色不可删 → 仍被账号使用不可删 → 先删 role_permissions 再删 role_definitions),
-- 但任何绕过应用的直接 SQL 都会留下孤儿。
--
-- 前置条件(2026-09-16 生产实测,均已满足):
-- · user_roles 中引用 role_definitions 之外角色的行 = 0
-- · role_permissions 中引用角色定义之外的行 = 0
-- · user_roles.role 非小写角色码行 = 0
-- · role_definitions.role_code 是 PRIMARY KEY(可作外键目标)
-- · 类型:role_definitions.role_code = text,role_permissions.role = text,
-- user_roles.role = varchar(50) —— 三者同属字符串类,外键可建。
--
-- 约束语义(与现有应用层行为对齐,只是把保证下沉到 DB):
-- user_roles.role ON DELETE RESTRICT —— 仍有账号挂该角色时不允删定义
-- role_permissions.role ON DELETE CASCADE —— 删定义时自动清掉它的权限映射
--
-- 回滚:
-- ALTER TABLE public.user_roles DROP CONSTRAINT IF EXISTS user_roles_role_fkey;
-- ALTER TABLE public.role_permissions DROP CONSTRAINT IF EXISTS role_permissions_role_fkey;
BEGIN;
-- 存在孤儿时**显式报错**而不是静默删除:静默删角色行会悄悄改变账号权限
DO $$
DECLARE
orphan_user_roles int;
orphan_role_perms int;
BEGIN
SELECT COUNT(*) INTO orphan_user_roles
FROM public.user_roles ur
WHERE NOT EXISTS (
SELECT 1 FROM public.role_definitions d WHERE d.role_code = ur.role::text
);
SELECT COUNT(*) INTO orphan_role_perms
FROM public.role_permissions rp
WHERE NOT EXISTS (
SELECT 1 FROM public.role_definitions d WHERE d.role_code = rp.role
);
IF orphan_user_roles > 0 THEN
RAISE EXCEPTION '存在 % 条 user_roles 引用了 role_definitions 之外的角色,请先清理后再执行本迁移', orphan_user_roles;
END IF;
IF orphan_role_perms > 0 THEN
RAISE EXCEPTION '存在 % 条 role_permissions 引用了 role_definitions 之外的角色,请先清理后再执行本迁移', orphan_role_perms;
END IF;
END $$;
ALTER TABLE public.user_roles
DROP CONSTRAINT IF EXISTS user_roles_role_fkey;
ALTER TABLE public.user_roles
ADD CONSTRAINT user_roles_role_fkey
FOREIGN KEY (role) REFERENCES public.role_definitions (role_code)
ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE public.role_permissions
DROP CONSTRAINT IF EXISTS role_permissions_role_fkey;
ALTER TABLE public.role_permissions
ADD CONSTRAINT role_permissions_role_fkey
FOREIGN KEY (role) REFERENCES public.role_definitions (role_code)
ON UPDATE CASCADE ON DELETE CASCADE;
COMMIT;