-- 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; -- Run after 001_creator.sql and 002_video_models.sql. -- Additive only: no balance changes, price changes, deleted projects, or new public buckets. begin; alter table public.creator_projects add column if not exists source_image_paths text[] not null default '{}'::text[], add column if not exists source_video_path text; create or replace function public.creator_reserve_photo_video( p_user uuid, p_id uuid, p_prompt text, p_ratio text, p_image_paths 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_image_paths is null or cardinality(p_image_paths) < 2 or cardinality(p_image_paths) > 10 or array_position(p_image_paths, null) is not null or exists(select 1 from unnest(p_image_paths) as paths(path) where path = '' or path not like p_user::text || '/%') or cardinality(p_image_paths) <> (select count(distinct path) from unnest(p_image_paths) as paths(path)) then raise exception 'Choose 2-10 distinct images belonging to this account'; end if; 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 prior.source_image_paths is distinct from p_image_paths) then raise exception 'Request ID was already used with different inputs'; end if; -- Reuse the existing atomic 25-credit reservation, limits and settings identity. select * into project from creator_reserve_video(p_user,p_id,p_prompt,p_ratio,null,p_video_settings); update creator_projects set source_image_paths=p_image_paths where id=project.id returning * into project; return project; end; $$; revoke all on function public.creator_reserve_photo_video(uuid,uuid,text,text,text[],jsonb) from public, anon, authenticated; grant execute on function public.creator_reserve_photo_video(uuid,uuid,text,text,text[],jsonb) to service_role; commit;