do $$
declare
  realtime_table text;
begin
  if not exists (
    select 1
    from pg_publication
    where pubname = 'supabase_realtime'
  ) then
    return;
  end if;

  foreach realtime_table in array array[
    'standardized_results',
    'standardization_tasks',
    'standardization_task_items',
    'final_result_products',
    'search_rounds',
    'merchant_contacts',
    'contact_logs',
    'customer_service_chats',
    'chat_messages',
    'procurement_batches'
  ] loop
    begin
      execute format('alter publication supabase_realtime add table public.%I', realtime_table);
    exception
      when duplicate_object then
        null;
      when undefined_table then
        null;
    end;
  end loop;
end $$;
