-- V037 - API Gateway and Integration Studio
DO $$ DECLARE t text; BEGIN
  FOREACH t IN ARRAY ARRAY['api_catalog_v037','api_credentials_v037','api_policies_v037','api_subscriptions_v037','webhook_endpoints_v037','api_call_audits_v037'] 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_v037 ON %I',t);
      EXECUTE format('CREATE POLICY tenant_isolation_v037 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 $$;
