-- Lumen & Gesso database schema, rebuilt from the app code on 2026-11-02 -- after the original Supabase project (srehbbvqhdthmgmvsxsq) disappeared. -- Source: app/lib/supabase/types.ts plus every column the app reads/writes. -- -- All table access goes through the service-role client (supabaseAdmin); -- the cookie client (supabaseServer) is only used for auth.getUser(). -- So RLS is ON with no policies: anon/authenticated keys can't read rows. create type public.order_status as enum ( 'pending_payment', 'abandoned', 'pending_design', 'awaiting_approval', 'revision_requested', 'approved', 'in_production', 'shipped', 'cancelled', 'delivered' -- legacy label, never set by new code ); create table public.orders ( id text primary key, -- lg_xxxxxxxxxx user_id uuid not null references auth.users (id) on delete cascade, user_email text not null, status public.order_status not null default 'pending_payment', config jsonb not null, price_cents integer not null, pet_photo_urls text[] not null default '{}', custom_style_url text, custom_style_description text, custom_style_analyzed_at timestamptz, current_generation_id uuid, revision_note text, shipping_address jsonb, stripe_session_id text, stripe_payment_intent_id text, prodigi_order_id text, tracking_number text, tracking_url text, carrier text, terms_accepted_at timestamptz, rejected_at timestamptz, cart_reminders_sent integer not null default 0, created_at timestamptz not null default now(), updated_at timestamptz not null default now() ); create table public.generations ( id uuid primary key default gen_random_uuid(), order_id text not null references public.orders (id) on delete cascade, prompt text not null, model text not null, clean_url text not null, watermarked_url text not null, published boolean not null default false, rejected_at timestamptz, created_at timestamptz not null default now() ); alter table public.orders add constraint orders_current_generation_fk foreign key (current_generation_id) references public.generations (id) on delete set null; -- Key/value store for third-party tokens (Pinterest refresh token). create table public.integrations ( key text primary key, value text, updated_at timestamptz not null default now() ); create index orders_user_id_idx on public.orders (user_id); create index orders_status_idx on public.orders (status); create index orders_stripe_session_id_idx on public.orders (stripe_session_id); create index orders_prodigi_order_id_idx on public.orders (prodigi_order_id); create index generations_order_id_idx on public.generations (order_id); -- Keep updated_at current. create or replace function public.touch_updated_at() returns trigger language plpgsql set search_path = '' as $$ begin new.updated_at := now(); return new; end $$; create trigger orders_touch_updated_at before update on public.orders for each row execute function public.touch_updated_at(); create trigger integrations_touch_updated_at before update on public.integrations for each row execute function public.touch_updated_at(); alter table public.orders enable row level security; alter table public.generations enable row level security; alter table public.integrations enable row level security; -- Hourly sweep: unpaid checkouts older than 7 days become 'abandoned' -- (matches MAX_AGE_MS in app/api/cron/cart-reminders/route.ts). create extension if not exists pg_cron; create or replace function public.sweep_abandoned_checkouts() returns void language sql set search_path = '' as $$ update public.orders set status = 'abandoned' where status = 'pending_payment' and created_at < now() - interval '7 days'; $$; select cron.schedule('sweep_abandoned_checkouts', '0 * * * *', $$select public.sweep_abandoned_checkouts()$$);