Add Site Safety Assistant (ssa) role replacing reporter, supervisor and capa_owner. Portals move under /ssa (overview, my-reports, incidents, capas). Update RLS, schema, API role checks, notification recipients, nav and tests. Add db/ops/2026-11-06-merge-roles-into-ssa.sql (not yet applied; needs owner go-ahead and a pg_dump first). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
38 lines
1.7 KiB
PL/PgSQL
38 lines
1.7 KiB
PL/PgSQL
-- Merge reporter + supervisor + capa_owner into one role: ssa (Site Safety Assistant).
|
|
-- Run by hand on the production DB with the owner's go-ahead. Take a pg_dump first.
|
|
-- After this, re-apply db/rls.sql policies that reference role names (drop + recreate; see below).
|
|
BEGIN;
|
|
|
|
ALTER TYPE user_role ADD VALUE IF NOT EXISTS 'ssa';
|
|
COMMIT; -- new enum value must be committed before use
|
|
|
|
BEGIN;
|
|
UPDATE users SET role = 'ssa' WHERE role IN ('reporter', 'supervisor', 'capa_owner');
|
|
|
|
-- Recreate enum without legacy values.
|
|
ALTER TYPE user_role RENAME TO user_role_old;
|
|
CREATE TYPE user_role AS ENUM ('ssa', 'hse', 'management', 'admin');
|
|
|
|
-- Policies reference auth_user_role() (returns user_role); drop them so the column/function can switch type.
|
|
-- Easiest: drop all policies from db/rls.sql, change types, then re-run db/rls.sql (policies are recreated).
|
|
-- 1) DROP POLICY ... for every policy in db/rls.sql (or: DROP OWNED-style loop below)
|
|
DO $$
|
|
DECLARE r record;
|
|
BEGIN
|
|
FOR r IN SELECT schemaname, tablename, policyname FROM pg_policies WHERE schemaname = 'public' LOOP
|
|
EXECUTE format('DROP POLICY %I ON %I.%I', r.policyname, r.schemaname, r.tablename);
|
|
END LOOP;
|
|
END $$;
|
|
|
|
ALTER TABLE users ALTER COLUMN role DROP DEFAULT;
|
|
ALTER TABLE users ALTER COLUMN role TYPE user_role USING role::text::user_role;
|
|
ALTER TABLE users ALTER COLUMN role SET DEFAULT 'ssa';
|
|
|
|
-- If auth_user_role() is declared RETURNS user_role, recreate it after the type swap
|
|
-- (copy its definition from db/schema.sql), then DROP TYPE user_role_old.
|
|
-- DROP TYPE user_role_old; -- only after nothing depends on it
|
|
COMMIT;
|
|
|
|
-- Finally: psql -f db/rls.sql (recreates all policies with 'ssa')
|
|
-- Users also need their ims_session cookie refreshed (role is in the JWT): ask them to log out and in.
|