create extension if not exists pgcrypto; create table if not exists users ( id uuid primary key default gen_random_uuid(), email text unique, display_name text not null default '', avatar_url text, password_hash text, created_at timestamptz not null default now(), updated_at timestamptz not null default now() ); create table if not exists identities ( id uuid primary key default gen_random_uuid(), user_id uuid not null references users(id) on delete cascade, provider text not null check (provider in ('apple', 'google', 'email')), provider_subject text not null, email text, created_at timestamptz not null default now(), unique(provider, provider_subject) ); create table if not exists premium_entitlements ( user_id uuid primary key references users(id) on delete cascade, source text not null default 'app_store', product_id text not null, active boolean not null default false, expires_at timestamptz, updated_at timestamptz not null default now() ); create index if not exists identities_user_id_idx on identities(user_id); create index if not exists premium_entitlements_active_idx on premium_entitlements(active);