From dbe5ec2a07dea807dbf14f197ea0414a8b38cbbc Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Tue, 4 Nov 2025 01:12:12 +0000 Subject: [PATCH] Update function to bypass RLS --- ...5_6e163014-fbfd-4db7-b1fa-1de145bf5723.sql | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 supabase/migrations/20251104011155_6e163014-fbfd-4db7-b1fa-1de145bf5723.sql diff --git a/supabase/migrations/20251104011155_6e163014-fbfd-4db7-b1fa-1de145bf5723.sql b/supabase/migrations/20251104011155_6e163014-fbfd-4db7-b1fa-1de145bf5723.sql new file mode 100644 index 00000000..cd755d88 --- /dev/null +++ b/supabase/migrations/20251104011155_6e163014-fbfd-4db7-b1fa-1de145bf5723.sql @@ -0,0 +1,57 @@ +-- Update claim_specific_submission function to bypass RLS +CREATE OR REPLACE FUNCTION public.claim_specific_submission( + p_submission_id UUID, + p_moderator_id UUID, + p_lock_duration INTERVAL DEFAULT '15 minutes' +) RETURNS BOOLEAN +LANGUAGE plpgsql +SECURITY DEFINER +SET search_path = public +AS $$ +DECLARE + rows_updated INTEGER; +BEGIN + -- Temporarily disable RLS for this transaction + SET LOCAL row_security = off; + + -- Atomically update the submission if it's unclaimed or lock expired + UPDATE content_submissions + SET + assigned_to = p_moderator_id, + assigned_at = NOW(), + locked_until = NOW() + p_lock_duration, + first_reviewed_at = COALESCE(first_reviewed_at, NOW()) + WHERE id = p_submission_id + AND ( + assigned_to IS NULL + OR locked_until < NOW() + ) + AND status = 'pending'; + + GET DIAGNOSTICS rows_updated = ROW_COUNT; + + -- Re-enable RLS (will auto-reset at end of transaction anyway) + SET LOCAL row_security = on; + + -- Log the action if successful + IF rows_updated > 0 THEN + BEGIN + PERFORM log_admin_action( + p_moderator_id, + (SELECT user_id FROM content_submissions WHERE id = p_submission_id), + 'submission_claimed', + jsonb_build_object( + 'submission_id', p_submission_id, + 'claim_type', 'specific' + ) + ); + EXCEPTION WHEN OTHERS THEN + RAISE WARNING 'Failed to log submission claim audit: %', SQLERRM; + END; + + RETURN TRUE; + END IF; + + RETURN FALSE; +END; +$$; \ No newline at end of file