Files
accounted/supabase/migrations/20260806150000_preserve_preset_committed_at.sql
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

31 lines
1.2 KiB
PL/PgSQL

-- Byte-for-byte the same function as 20260806160000 (see that file for the
-- full rationale). Two versions exist because the PR's Supabase preview
-- branch applied 150000 while this change was still iterating: deleting the
-- file orphans the preview's migration tracker ("Remote migration versions
-- not found in local migrations directory"), and an earlier interim body
-- (preserve-for-everyone, no actor check) must not live on as a standalone
-- applyable unit. With identical content in both files, any environment that
-- applies either or both, in any order, lands on the same guarded function.
CREATE OR REPLACE FUNCTION public.set_committed_at()
RETURNS trigger
LANGUAGE plpgsql
SET search_path = public
AS $$
DECLARE
v_jwt_role text;
BEGIN
IF OLD.status = 'draft' AND NEW.status = 'posted' THEN
v_jwt_role := coalesce(
nullif(current_setting('request.jwt.claims', true), '')::jsonb ->> 'role',
nullif(current_setting('request.jwt.claim.role', true), ''),
''
);
IF NEW.committed_at IS NULL
OR NOT (v_jwt_role = '' OR v_jwt_role = 'service_role') THEN
NEW.committed_at := now();
END IF;
END IF;
RETURN NEW;
END;
$$;