Lumen & Gesso / website / supabase / schema.sql
| 1 | -- Lumen & Gesso database schema, rebuilt from the app code on 2026-11-02 |
| 2 | -- after the original Supabase project (srehbbvqhdthmgmvsxsq) disappeared. |
| 3 | -- Source: app/lib/supabase/types.ts plus every column the app reads/writes. |
| 4 | -- |
| 5 | -- All table access goes through the service-role client (supabaseAdmin); |
| 6 | -- the cookie client (supabaseServer) is only used for auth.getUser(). |
| 7 | -- So RLS is ON with no policies: anon/authenticated keys can't read rows. |
| 8 | |
| 9 | create type public.order_status as enum ( |
| 10 | 'pending_payment', |
| 11 | 'abandoned', |
| 12 | 'pending_design', |
| 13 | 'awaiting_approval', |
| 14 | 'revision_requested', |
| 15 | 'approved', |
| 16 | 'in_production', |
| 17 | 'shipped', |
| 18 | 'cancelled', |
| 19 | 'delivered' -- legacy label, never set by new code |
| 20 | ); |
| 21 | |
| 22 | create table public.orders ( |
| 23 | id text primary key, -- lg_xxxxxxxxxx |
| 24 | user_id uuid not null references auth.users (id) on delete cascade, |
| 25 | user_email text not null, |
| 26 | status public.order_status not null default 'pending_payment', |
| 27 | config jsonb not null, |
| 28 | price_cents integer not null, |
| 29 | pet_photo_urls text[] not null default '{}', |
| 30 | custom_style_url text, |
| 31 | custom_style_description text, |
| 32 | custom_style_analyzed_at timestamptz, |
| 33 | current_generation_id uuid, |
| 34 | revision_note text, |
| 35 | shipping_address jsonb, |
| 36 | stripe_session_id text, |
| 37 | stripe_payment_intent_id text, |
| 38 | prodigi_order_id text, |
| 39 | tracking_number text, |
| 40 | tracking_url text, |
| 41 | carrier text, |
| 42 | terms_accepted_at timestamptz, |
| 43 | rejected_at timestamptz, |
| 44 | cart_reminders_sent integer not null default 0, |
| 45 | created_at timestamptz not null default now(), |
| 46 | updated_at timestamptz not null default now() |
| 47 | ); |
| 48 | |
| 49 | create table public.generations ( |
| 50 | id uuid primary key default gen_random_uuid(), |
| 51 | order_id text not null references public.orders (id) on delete cascade, |
| 52 | prompt text not null, |
| 53 | model text not null, |
| 54 | clean_url text not null, |
| 55 | watermarked_url text not null, |
| 56 | published boolean not null default false, |
| 57 | rejected_at timestamptz, |
| 58 | created_at timestamptz not null default now() |
| 59 | ); |
| 60 | |
| 61 | alter table public.orders |
| 62 | add constraint orders_current_generation_fk |
| 63 | foreign key (current_generation_id) references public.generations (id) on delete set null; |
| 64 | |
| 65 | -- Key/value store for third-party tokens (Pinterest refresh token). |
| 66 | create table public.integrations ( |
| 67 | key text primary key, |
| 68 | value text, |
| 69 | updated_at timestamptz not null default now() |
| 70 | ); |
| 71 | |
| 72 | create index orders_user_id_idx on public.orders (user_id); |
| 73 | create index orders_status_idx on public.orders (status); |
| 74 | create index orders_stripe_session_id_idx on public.orders (stripe_session_id); |
| 75 | create index orders_prodigi_order_id_idx on public.orders (prodigi_order_id); |
| 76 | create index generations_order_id_idx on public.generations (order_id); |
| 77 | |
| 78 | -- Keep updated_at current. |
| 79 | create or replace function public.touch_updated_at() returns trigger |
| 80 | language plpgsql set search_path = '' as $$ |
| 81 | begin |
| 82 | new.updated_at := now(); |
| 83 | return new; |
| 84 | end $$; |
| 85 | |
| 86 | create trigger orders_touch_updated_at |
| 87 | before update on public.orders |
| 88 | for each row execute function public.touch_updated_at(); |
| 89 | |
| 90 | create trigger integrations_touch_updated_at |
| 91 | before update on public.integrations |
| 92 | for each row execute function public.touch_updated_at(); |
| 93 | |
| 94 | alter table public.orders enable row level security; |
| 95 | alter table public.generations enable row level security; |
| 96 | alter table public.integrations enable row level security; |
| 97 | |
| 98 | -- Hourly sweep: unpaid checkouts older than 7 days become 'abandoned' |
| 99 | -- (matches MAX_AGE_MS in app/api/cron/cart-reminders/route.ts). |
| 100 | create extension if not exists pg_cron; |
| 101 | |
| 102 | create or replace function public.sweep_abandoned_checkouts() returns void |
| 103 | language sql set search_path = '' as $$ |
| 104 | update public.orders |
| 105 | set status = 'abandoned' |
| 106 | where status = 'pending_payment' |
| 107 | and created_at < now() - interval '7 days'; |
| 108 | $$; |
| 109 | |
| 110 | select cron.schedule('sweep_abandoned_checkouts', '0 * * * *', |
| 111 | $$select public.sweep_abandoned_checkouts()$$); |