-- V035 Enterprise Lakehouse
-- SQLAlchemy creates the portable schema in native mode. The policies below harden PostgreSQL.
DO $$
DECLARE t text;
BEGIN
  FOREACH t IN ARRAY ARRAY['lakehouse_catalogs_v035','lakehouse_tables_v035','lakehouse_snapshots_v035','lakehouse_data_files_v035','lakehouse_maintenance_runs_v035']
  LOOP
    IF to_regclass(t) IS NOT NULL THEN
      EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY',t);
      EXECUTE format('ALTER TABLE %I FORCE ROW LEVEL SECURITY',t);
      EXECUTE format('DROP POLICY IF EXISTS tenant_isolation_v035 ON %I',t);
      EXECUTE format('CREATE POLICY tenant_isolation_v035 ON %I USING (tenant_id::text = current_setting(''app.current_tenant_id'', true)) WITH CHECK (tenant_id::text = current_setting(''app.current_tenant_id'', true))',t);
    END IF;
  END LOOP;
END $$;
