-- Additive upgrade: apply after 001_creator.sql in your existing Supabase project. -- Preserves existing projects, account balances, credit prices, refunds, and policies. begin; alter table public.creator_projects add column if not exists video_settings jsonb; create or replace function public.creator_reserve_video( p_user uuid, p_id uuid, p_prompt text, p_ratio text, p_image_path text, p_video_settings jsonb ) returns public.creator_projects language plpgsql set search_path = public as $$ declare prior public.creator_projects; project public.creator_projects; begin if p_video_settings is null or jsonb_typeof(p_video_settings) <> 'object' or not (p_video_settings ?& array['modelId','duration','resolution','audio']) or (p_video_settings - array['modelId','duration','resolution','audio']) <> '{}'::jsonb or jsonb_typeof(p_video_settings->'modelId') <> 'string' or jsonb_typeof(p_video_settings->'duration') <> 'number' or jsonb_typeof(p_video_settings->'resolution') <> 'string' or jsonb_typeof(p_video_settings->'audio') <> 'boolean' then raise exception 'Invalid video settings'; end if; -- Serialize with the existing account lock and credit reservation function. perform creator_ensure_account(p_user); perform 1 from creator_accounts where id=p_user for update; select * into prior from creator_projects where id=p_id; if found and ( prior.user_id <> p_user or coalesce(prior.video_settings, '{"modelId":"ltx-2.5","duration":5,"resolution":"480p","audio":false}'::jsonb) is distinct from p_video_settings ) then raise exception 'Request ID was already used with different inputs'; end if; select * into project from creator_reserve(p_user,p_id,p_prompt,'video',p_ratio,p_image_path); update creator_projects set video_settings=p_video_settings where id=project.id returning * into project; return project; end; $$; revoke all on function public.creator_reserve_video(uuid,uuid,text,text,text,jsonb) from public, anon, authenticated; grant execute on function public.creator_reserve_video(uuid,uuid,text,text,text,jsonb) to service_role; commit;