do $$ begin if not exists(select 1 from kbx_test.environment_guard where marker='KBX_SCENARIO_TEST_ONLY') then raise exception 'KBX scenario reset refused: test environment guard is missing'; end if; end $$; -- Delete only deterministic KBX synthetic records. Never bulk-delete arbitrary host data. delete from kbx.command_receipts where idempotency_key like 'kbx-test-%'; delete from kbx.import_sessions where tenant_id='KBX-TEST-TENANT'; delete from oms.order_lines where order_id in (select id from oms.orders where order_no like 'TEST-IMP-%'); delete from audit.entries where aggregate_id in (select id from oms.orders where order_no like 'TEST-IMP-%'); delete from integration.outbox where aggregate_id in (select id from oms.orders where order_no like 'TEST-IMP-%'); delete from oms.orders where order_no like 'TEST-IMP-%'; delete from wms.scan_receipts where task_id='60000000-0000-4000-8000-000000000001'; delete from wms.quantity_receipts where task_id='60000000-0000-4000-8000-000000000001'; delete from wms.picking_exceptions where task_id='60000000-0000-4000-8000-000000000001'; delete from wms.picking_lines where task_id='60000000-0000-4000-8000-000000000001'; delete from wms.picking_tasks where id='60000000-0000-4000-8000-000000000001'; delete from oms.order_lines where order_id in ( '50000000-0000-4000-8000-000000000001','50000000-0000-4000-8000-000000000002', '50000000-0000-4000-8000-000000000003','50000000-0000-4000-8000-000000000004'); delete from audit.entries where aggregate_id in ( '50000000-0000-4000-8000-000000000001','50000000-0000-4000-8000-000000000002', '50000000-0000-4000-8000-000000000003','50000000-0000-4000-8000-000000000004'); delete from integration.outbox where aggregate_id in ( '50000000-0000-4000-8000-000000000001','50000000-0000-4000-8000-000000000002', '50000000-0000-4000-8000-000000000003','50000000-0000-4000-8000-000000000004'); delete from oms.orders where id in ( '50000000-0000-4000-8000-000000000001','50000000-0000-4000-8000-000000000002', '50000000-0000-4000-8000-000000000003','50000000-0000-4000-8000-000000000004'); delete from kbx.work_item_audit where tenant_id='KBX-TEST-TENANT'; delete from kbx.work_items where tenant_id='KBX-TEST-TENANT'; delete from kbx.reconcile_items where tenant_id='KBX-TEST-TENANT'; delete from erp.inventory_move_lines where move_id in ('70000000-0000-4000-8000-000000000001','70000000-0000-4000-8000-000000000002'); delete from erp.inventory_moves where id in ('70000000-0000-4000-8000-000000000001','70000000-0000-4000-8000-000000000002'); delete from audit.entries where aggregate_id in ('40000000-0000-4000-8000-000000000001','40000000-0000-4000-8000-000000000002'); delete from integration.outbox where aggregate_id in ('40000000-0000-4000-8000-000000000001','40000000-0000-4000-8000-000000000002'); delete from erp.item_prices where item_id in ('40000000-0000-4000-8000-000000000001','40000000-0000-4000-8000-000000000002'); delete from erp_inventory_ledger_projection where item_id in ('40000000-0000-4000-8000-000000000001','40000000-0000-4000-8000-000000000002'); delete from erp_inventory_snapshot_projection where item_id in ('40000000-0000-4000-8000-000000000001','40000000-0000-4000-8000-000000000002'); delete from oms.customers where id='20000000-0000-4000-8000-000000000001'; delete from catalog.items where id in ('40000000-0000-4000-8000-000000000001','40000000-0000-4000-8000-000000000002'); delete from inventory.warehouses where id in ('30000000-0000-4000-8000-000000000001','30000000-0000-4000-8000-000000000002'); delete from kbx.external_data_observations where tenant_id='11111111-1111-4111-8111-111111111111' and cache_key like 'TEST-%'; delete from kbx.external_data_cache where tenant_id='11111111-1111-4111-8111-111111111111' and cache_key like 'TEST-%';