Files
SGF/scripts/stock-prefix-plan.php
T
2026-11-04 09:37:20 +02:00

11 lines
1.8 KiB
PHP

<?php
use Illuminate\Support\Facades\DB;
use Tempo\Finance\GeneralLedger\StockValuation;
$out=$s['root'].'/prefix-plan.json';if(file_exists($out))throw new LogicException('Completed plan proof preserved');$db=DB::connection();$db->statement('SET search_path TO prefix_safe_'.substr($s['tag'],0,8));$db->beginTransaction();$db->statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
try{
$sql=null;$bindings=[];DB::listen(function($q)use(&$sql,&$bindings){if(str_starts_with($q->sql,'DECLARE stock_prefix_')){$sql=preg_replace('/^DECLARE stock_prefix_[a-f0-9]{16} NO SCROLL CURSOR FOR /','',$q->sql);$bindings=$q->bindings;}});$provider=new StockValuation($db,['tenant_id'=>1,'company_id'=>0,'unit_id'=>0],1,'metadata-only',fn()=>null,fn()=>null);(new ReflectionMethod($provider,'priorJournals'))->invoke($provider,(object)['id'=>1,'stock_item_id'=>1,'warehouse_id'=>1],(object)['id'=>10001],(object)['id'=>1]);if(!$sql)throw new LogicException('Current metadata SQL not captured');
$bad=preg_replace('/pm\.id=CASE WHEN[\s\S]+? ELSE NULL END/',"pm.id::text=(j.snapshot->'source'->>'stock_movement_id')",$sql,1,$count);if($count!==1)throw new LogicException('Controlled comparison anchor missing');$plans=[];
foreach(['text_join'=>$bad,'numeric_guarded_join'=>$sql]as$name=>$query){$r=$db->select('EXPLAIN (FORMAT JSON) DECLARE prefix_plan CURSOR FOR '.$query,$bindings);$plan=json_decode($r[0]->{'QUERY PLAN'},true,64,JSON_THROW_ON_ERROR);$plans[$name]=$plan;}
file_put_contents($out,json_encode(['passed'=>true,'explain_only'=>true,'no_analyze'=>true,'metadata_only_fixture'=>true,'plans'=>$plans],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo "Controlled cursor plans recorded without executing the regressed text join.\n";
}finally{$db->rollBack();$db->statement('SET search_path TO public');}