Files
SupplierSystem/migrations/20261073_grant_legacy_public_sequences_to_rls.sql
Ubuntu 2ba17f9280
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
fix(本地库权限): 补齐 legacy_public schema/序列对 canteen_app_rls 的授权
本地新建商品分类报错(线上正常):
  [product_catalog] permission denied for sequence product_catalog_id_seq

根因:
- 通用数据网关 /api/data/query 写业务表时按 RLS 主防线 SET LOCAL ROLE canteen_app_rls;
- 租户表 id 默认值仍引用 legacy_public 的序列(如 tenant_br002.product_catalog.id
  DEFAULT nextval('legacy_public.product_catalog_id_seq'));
- 本地开发库 legacy_public 的 schema USAGE 与 4 个序列(product_catalog_id_seq /
  customer_skus_id_seq / allocation_tasks_id_seq / sku_suppliers_id_seq)都没有授给
  canteen_app_rls(历史迁移只授过 tenant_% / public 的序列);
- 生产库此前已有人工授权(ACL 含 canteen_app_rls、schema USAGE=true),所以线上正常、仅本地报错。

修复:新增幂等迁移 20261073,给 legacy_public schema USAGE + 该 schema 下全部序列
USAGE, SELECT 给 canteen_app_rls(只追加权限,无数据变更;对生产是 no-op)。

本地验证:
- canteen_app_rls SET LOCAL ROLE 后 nextval('legacy_public.product_catalog_id_seq') 成功;
- 数据网关真实 insert product_catalog 分类成功(返回 id=122、branch_code=BR002),
  并用同一网关 delete 清理(product_catalog/categories 均无残留)。
未部署;生产下次发布时该幂等迁移会自动应用(无副作用)。
2026-10-22 17:19:24 +08:00

53 lines
2.2 KiB
SQL
Raw Permalink 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.
-- ============================================
-- 补齐 legacy_public 序列对 RLS 应用角色 canteen_app_rls 的权限(开发库缺口)
--
-- 背景(2026-10-22 本地实测):
-- 租户业务表的 id 默认值仍引用 legacy_public 的序列(如
-- tenant_br002.product_catalog.id DEFAULT nextval('legacy_public.product_catalog_id_seq'))。
-- 通用数据网关(/api/data/query)写业务表时按 RLS 主防线会
-- `SET LOCAL ROLE canteen_app_rls`;若该角色对序列没有 USAGE,
-- 新建商品分类就会报:
-- [product_catalog] permission denied for sequence product_catalog_id_seq
--
-- 现状差异:
-- · 生产库:4 个 legacy_public 序列的 ACL 已含 canteen_app_rls(线上正常);
-- · 本地开发库:ACL 为 NULL,canteen_app_rls 无权限(仅本地报错)。
-- 历史迁移只给 tenant_% / public 的序列授过权,没有覆盖 legacy_public。
--
-- 本迁移只做追加授权(幂等、无数据变更):
-- 给 legacy_public 下全部序列补 USAGE, SELECT 给 canteen_app_rls。
-- 对已授权的生产库是 no-op;对新克隆/本地库补齐缺口。
-- ============================================
DO $$
DECLARE
r record;
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'canteen_app_rls') THEN
RAISE NOTICE 'skip legacy_public sequence grants: role canteen_app_rls missing';
RETURN;
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_namespace WHERE nspname = 'legacy_public') THEN
RAISE NOTICE 'skip legacy_public sequence grants: schema legacy_public missing';
RETURN;
END IF;
-- 序列权限之外还需要 schema USAGE(否则 nextval 报 permission denied for schema legacy_public)
EXECUTE 'GRANT USAGE ON SCHEMA legacy_public TO canteen_app_rls';
FOR r IN
SELECT c.relname
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'legacy_public'
AND c.relkind = 'S'
LOOP
EXECUTE format(
'GRANT USAGE, SELECT ON SEQUENCE legacy_public.%I TO canteen_app_rls',
r.relname
);
END LOOP;
RAISE NOTICE 'legacy_public schema + sequences granted to canteen_app_rls';
END $$;