create table if not exists public.content_export_jobs ( id uuid primary key default gen_random_uuid(), tenant_id uuid not null references public.tenants(id) on delete cascade, created_by uuid references public.platform_users(id) on delete set null, export_type text not null default 'questions' check (export_type in ('questions', 'paper', 'daily_practice')), format text not null default 'json' check (format in ('json', 'paper_json', 'print_payload')), scope_type text not null check (scope_type in ('collection', 'entry', 'content_node')), scope_id uuid not null, status text not null default 'completed' check (status in ('completed', 'failed')), include_answers boolean not null default true, include_explanations boolean not null default true, include_video_refs boolean not null default false, question_count integer not null default 0 check (question_count >= 0), summary jsonb not null default '{}'::jsonb, options jsonb not null default '{}'::jsonb, output_metadata jsonb not null default '{}'::jsonb, error_message text, finished_at timestamptz, created_at timestamptz not null default now(), updated_at timestamptz not null default now() ); create index if not exists idx_content_export_jobs_tenant_created on public.content_export_jobs(tenant_id, created_at desc); create index if not exists idx_content_export_jobs_scope on public.content_export_jobs(tenant_id, scope_type, scope_id, created_at desc); alter table public.content_export_jobs enable row level security; drop policy if exists tenant_isolation on public.content_export_jobs; create policy tenant_isolation on public.content_export_jobs for all using (tenant_id = app.current_tenant_id() or app.is_platform_admin()) with check (tenant_id = app.current_tenant_id() or app.is_platform_admin()); drop trigger if exists set_updated_at on public.content_export_jobs; create trigger set_updated_at before update on public.content_export_jobs for each row execute function app.touch_updated_at();