<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';require __DIR__.'/natural.php';$check(!is_file($runtime.'/fixture-journal.json'),'No successful/unknown preparation replay');$old=json_decode(file_get_contents('/tmp/cdx-ai-operator-dictionary-read9-20261011/private/evidence/final-original.json'),true);$before=$own();$check(count($before)===32,'Actual32 original table sets');foreach($old['original'] as $t=>$rows)$check($before[$t]===$rows,'Old29 exact '.$t);foreach(['equipment','dishes','dishes_img'] as $t)$check($before[$t]===[],'Fresh own fixture tables empty '.$t);$naturalBefore=$natural();$check((int)$read('SELECT COUNT(*) n FROM yoshop_equipment WHERE type IN (50,60)')[0]['n']===0,'Only new type60 scope/empty50');$template=json_decode(file_get_contents(__DIR__.'/fixture-values-template.json'),true);$check(hash_file('sha256',__DIR__.'/fixture-values-template.json')==='ba6c09f260a117f9ece95abc60141d28e61f491da9e1d5a7f920c71e09f1ca02','Exact Root-approved78-value template');$capacities=json_decode(file_get_contents(__DIR__.'/fixture-capacity-'.$lane.'.json'),true);$check($capacities['tables']['equipment']['pending_approved_DDL_columns']===[],'All approved business columns exist');$quote=static function($k)use($dm){return $dm?'"'.strtoupper($k).'"':'`'.$k.'`';};$metadata=[];foreach(['equipment','dishes','dishes_img'] as $t){$u='YOSHOP_'.strtoupper($t);$cols=$dm?$read('SELECT COLUMN_NAME,DATA_TYPE,NULLABLE,DATA_DEFAULT,DATA_LENGTH,CHAR_LENGTH,DATA_PRECISION,DATA_SCALE FROM USER_TAB_COLUMNS WHERE TABLE_NAME=? ORDER BY COLUMN_ID',[$u]):$read('SELECT COLUMN_NAME,DATA_TYPE,IS_NULLABLE,COLUMN_DEFAULT,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE,EXTRA FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=? ORDER BY ORDINAL_POSITION',['yoshop_'.$t]);$names=array_map('strtolower',array_column($cols,'column_name'));$expected=array_keys($template[$t]);$expected[]='id';sort($names);sort($expected);$check($names===$expected,'All actual nonPK explicit '.$t);$fk=$dm?$read("SELECT COUNT(*) n FROM USER_CONSTRAINTS WHERE CONSTRAINT_TYPE='R' AND (TABLE_NAME=? OR R_CONSTRAINT_NAME IN (SELECT CONSTRAINT_NAME FROM USER_CONSTRAINTS WHERE TABLE_NAME=?))",[$u,$u]):$read('SELECT COUNT(*) n FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA=DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND (TABLE_NAME=? OR REFERENCED_TABLE_NAME=?)',['yoshop_'.$t,'yoshop_'.$t]);$tr=$dm?$read('SELECT COUNT(*) n FROM USER_TRIGGERS WHERE TABLE_NAME=?',[$u]):$read('SELECT COUNT(*) n FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_SCHEMA=DATABASE() AND EVENT_OBJECT_TABLE=?',['yoshop_'.$t]);$check((int)$fk[0]['n']===0&&(int)$tr[0]['n']===0,'Fresh FK/trigger0 '.$t);if($dm){$ddl=$read("SELECT DBMS_METADATA.GET_DDL('TABLE',?,'AIZHCT_APP') ddl FROM DUAL",[$u])[0]['ddl'];$check(preg_match('/"ID"[^,\n]*(?:IDENTITY\s*\(|AUTO_INCREMENT\b)/i',$ddl)===1,'Target generated PK '.$t);}else $check($cols[0]['extra']==='auto_increment','Source generated PK '.$t);$metadata[$t]=$cols;}
$check((int)$read('SELECT COUNT(*) n FROM yoshop_equipment WHERE code=? OR name=?',[$template['equipment']['code'],$template['equipment']['name']])[0]['n']===0&&(int)$read('SELECT COUNT(*) n FROM yoshop_dishes WHERE name=?',[$template['dishes']['name']])[0]['n']===0,'Fresh marker collision0');$journal=['lane'=>$lane,'committed'=>false,'before'=>$before,'natural_before'=>$naturalBefore,'before_protect'=>$protect(),'metadata'=>$metadata,'dml'=>[],'owned'=>[],'template_sha256'=>hash_file('sha256',__DIR__.'/fixture-values-template.json')];$persist('fixture-journal.json',$journal);$db->beginTransaction();try{foreach(['equipment','dishes','dishes_img'] as $t){$r=$template[$t];foreach($r as &$v){if($v==='@same_library_terminal_file_24452_or_9715')$v=$lane==='B'?24452:9715;elseif($v==='@same_library_terminal_nonlogin_operator_10075_or_10011')$v=$lane==='B'?10075:10011;elseif($v==='@new_same_library_dishes_generated_PK')$v=$journal['owned']['dishes'];}unset($v);$keys=array_keys($r);$sql='INSERT INTO yoshop_'.$t.' ('.implode(',',array_map($quote,$keys)).') VALUES ('.implode(',',array_fill(0,count($keys),'?')).')';$s=$db->prepare($sql);$s->execute(array_values($r));$check($s->rowCount()===1,'Exactly1 approved INSERT '.$t);$where=$t==='equipment'?['store_id'=>2147001113,'code'=>$r['code']]:($t==='dishes'?['store_id'=>2147001112,'name'=>$r['name']]:['store_id'=>2147001112,'dishes_id'=>$r['dishes_id'],'file_id'=>$r['file_id']]);$got=$query($db,'SELECT * FROM yoshop_'.$t.' WHERE '.implode(' AND ',array_map(static function($k)use($quote){return $quote($k).'=?';},array_keys($where))),array_values($where));$check(count($got)===1,'Exact own generatedPK selection '.$t);$id=(int)$got[0]['id'];$check($id>0,'Positive DB PK');foreach($r as $k=>$v){$c=array_values(array_filter($metadata[$t],static function($c)use($k){return strtolower($c['column_name'])===$k;}))[0];$type=strtolower($c['data_type']);if($type==='decimal')$check(bccomp((string)$got[0][$k],$v,2)===0,'Only exact DEC10,2 zero physical numeric value '.$k);elseif(is_int($v))$check((int)$got[0][$k]===$v,'Exact integer physical value '.$k);elseif($type==='datetime')$check($got[0][$k]===($dm?$v.'.000000':$v),'Exact zero-fraction datetime physical bytes '.$k);else $check($got[0][$k]===$v,'Full column literal bytes '.$k);}$journal['owned'][$t]=$id;$journal['dml'][]=['table'=>$t,'generated_PK'=>$id,'values'=>$r,'full_physical_inserted_row'=>$got[0]];$persist('fixture-journal.json',$journal);}
$tx=[];foreach($pks as $t=>$pk)$tx[$t]=$query($db,'SELECT * FROM yoshop_'.$t.' WHERE store_id IN (2147001111,2147001112,2147001113) ORDER BY '.$pk);foreach($before as $t=>$rows)if(in_array($t,['equipment','dishes','dishes_img'],true))$check(count($tx[$t])===1,'Only1 new own row '.$t);else $check($tx[$t]===$rows,'Old29 unchanged before commit '.$t);$check($natural()===$naturalBefore&&$protect()===$journal['before_protect'],'All natural dictionaries/digests/default exact before commit');$db->commit();$journal['committed']=true;$journal['after']=$own();$journal['natural_after']=$natural();$check($journal['after']===$tx&&$journal['natural_after']===$naturalBefore,'Independent postcommit physical32/natural exact');$persist('fixture-journal.json',$journal);echo json_encode(['lane'=>$lane,'preparation_INSERT'=>3,'UPDATE'=>0,'DELETE'=>0,'HTTP'=>0,'generated_own_PK'=>$journal['owned'],'old29_exact'=>true,'all32_independent_exact'=>true]),"\n";}catch(Throwable $e){if($db->inTransaction())$db->rollBack();$journal['failure']=['class'=>get_class($e),'message'=>$e->getMessage()];$persist('fixture-journal.json',$journal);throw $e;}
