-- Historial de cambios del Centro de Mando (agencia + cliente). -- Idempotente. create table if not exists public.change_history ( id uuid primary key default gen_random_uuid(), tenant_id uuid references public.tenants(id) on delete set null, tenant_name text, user_id uuid, user_email text not null default 'sistema', user_role text, entity_type text not null, entity_id text, action text not null, summary text not null, detail jsonb not null default '{}'::jsonb, source text, created_at timestamptz not null default now() ); create index if not exists change_history_created_idx on public.change_history (created_at desc); create index if not exists change_history_tenant_created_idx on public.change_history (tenant_id, created_at desc); create index if not exists change_history_entity_idx on public.change_history (entity_type, created_at desc); comment on table public.change_history is 'Auditoría de cambios: usuario, fecha, acción y detalle (Zona de marca, creatividades, etc.)'; alter table public.change_history enable row level security; -- Solo service role escribe/lee vía API (Centro de Mando). Sin policies abiertas. drop policy if exists change_history_agency_read on public.change_history;