diff --git a/supabase/migrations/20261028110000_sheet_review_workflow.sql b/supabase/migrations/20261028110000_sheet_review_workflow.sql index e1492209..2c6a436a 100644 --- a/supabase/migrations/20261028110000_sheet_review_workflow.sql +++ b/supabase/migrations/20261028110000_sheet_review_workflow.sql @@ -477,9 +477,10 @@ begin or not exists (select 1 from public.species s where s.id = v_species) then raise exception 'sheet.species_invalid' using errcode = 'P0001'; end if; - perform private.consume_catalog_write_budget(v_actor); - v_sheet := private.insert_official_sheet(v_species, v_name, create_official_sheet.slug, v_description, null); + -- The budget row last, after the rows the insert locks through its foreign + -- keys (species), as every RPC takes it (M4). + perform private.consume_catalog_write_budget(v_actor); perform private.log_event( v_actor, 'anatomical_sheet.created', 'anatomical_sheet', v_sheet, null, null, @@ -1660,10 +1661,11 @@ begin if v_id is not null then return v_id; end if; - perform private.consume_catalog_write_budget(v_actor); insert into public.anatomical_sheet_assets (sheet_id, sha256, byte_size, file_name, created_by) values (sheet_id, sha256, byte_size, file_name, v_actor) returning id into v_id; + -- The budget row last, after the sheet row the insert locks through its key. + perform private.consume_catalog_write_budget(v_actor); perform private.log_event( v_actor, 'anatomical_sheet_asset.registered', 'anatomical_sheet_asset', v_id, null, null, jsonb_build_object('sheet_id', sheet_id, 'sha256', sha256, 'byte_size', byte_size)