<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';$check(!is_file($runtime.'/fixture-journal.json'),'No fixture replay');$f=json_decode(file_get_contents($runtime.'/fixture-values.json'),true);$gate=json_decode(file_get_contents($runtime.'/preflight.json'),true);$check(hash_file('sha256',$runtime.'/fixture-values.json')===$gate['fixture_sha256'],'Frozen values');$before=$own();foreach($before as $r)$check(count($r)===0,'Live fixture collision');$journal=['lane'=>$lane,'before_protect'=>$protect(),'owned'=>[],'dml'=>[],'committed'=>false];$persist('fixture-journal.json',$journal);
$insert=static function($table,$row)use($db,$query,$pks,$persist,&$journal,$dm){$pk=$pks[$table];$cols=array_keys($row);$quote=static function($k)use($dm){return $dm?'"'.strtoupper($k).'"':'`'.$k.'`';};$sql='INSERT INTO yoshop_'.$table.' ('.implode(',',array_map($quote,$cols)).') VALUES ('.implode(',',array_fill(0,count($cols),'?')).')';$s=$db->prepare($sql);$s->execute(array_values($row));$journal['dml'][]=['table'=>$table,'values'=>$row,'pk_omitted'=>!array_key_exists($pk,$row)];$where='store_id=?';$args=[$row['store_id']];if($table==='comment'){$where.=' AND content=? AND score=?';$args[]=$row['content'];$args[]=$row['score'];}elseif($table==='comment_image'){$where.=' AND comment_id=?';$args[]=$row['comment_id'];}$r=$query($db,'SELECT '.$pk.' FROM yoshop_'.$table.' WHERE '.$where,$args);if(count($r)!==1)throw new RuntimeException('Generated row mapping '.$table);return (int)$r[0][$pk];};
$db->beginTransaction();try{foreach($tenants as $module=>$tenant){$v=$f['templates'][$module];$id=[];$id['upload_file']=$insert('upload_file',$v['upload_file']);$v['user']['avatar_id']=$id['upload_file'];$id['user']=$insert('user',$v['user']);$id['goods']=$insert('goods',$v['goods']);$v['order']['user_id']=$id['user'];$id['order']=$insert('order',$v['order']);$v['order_goods']=array_replace($v['order_goods'],['user_id'=>$id['user'],'goods_id'=>$id['goods'],'order_id'=>$id['order'],'image_id'=>$id['upload_file']]);$id['order_goods']=$insert('order_goods',$v['order_goods']);$id['comment']=[];foreach($f['comment_rows'] as $spec){$row=array_replace($v['comment'],$spec,['user_id'=>$id['user'],'goods_id'=>$id['goods'],'order_id'=>$id['order'],'order_goods_id'=>$id['order_goods']]);$id['comment'][]=$insert('comment',$row);}$v['comment_image']=array_replace($v['comment_image'],['comment_id'=>$id['comment'][0],'image_id'=>$id['upload_file']]);$id['comment_image']=$insert('comment_image',$v['comment_image']);$journal['owned'][$module]=$id;$persist('fixture-journal.json',$journal);}$db->commit();$journal['committed']=true;$journal['after']=$own();$journal['after_protect']=$protect();$check($journal['after_protect']===$journal['before_protect'],'Protected originals');$persist('fixture-journal.json',$journal);echo json_encode(['lane'=>$lane,'fixture_dml'=>count($journal['dml']),'owned'=>$journal['owned'],'counts'=>array_map('count',$journal['after']),'protected_unchanged'=>true]),"\n";}catch(Throwable $e){if($db->inTransaction())$db->rollBack();$journal['failed']=get_class($e);$persist('fixture-journal.json',$journal);throw $e;}
