-- V038 BPMN and Low-Code Studio
-- SQLAlchemy creates the cross-database schema in native mode.
-- PostgreSQL defense-in-depth policies for tenant-owned tables:
DO $$ DECLARE t text; BEGIN
  FOREACH t IN ARRAY ARRAY['lowcode_applications_v038','lowcode_forms_v038','bpmn_processes_v038','bpmn_process_versions_v038','process_instances_v038','human_tasks_v038','business_rules_v038'] LOOP
    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_v038 ON %I',t);
    EXECUTE format('CREATE POLICY tenant_isolation_v038 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 LOOP;
END $$;
