selectOne('SELECT current_database() AS database,inet_server_port() AS port'); if($target!==['database'=>'stability_consumers_20261011_084534_test','port'=>55441])throw new RuntimeException('Isolated QA required'); $db->beginTransaction();$db->statement('SET TRANSACTION READ ONLY'); $includeVariants=in_array('--include-B-variants',$argv,true); try{ $state=json_decode(file_get_contents(__DIR__.'/option-B-land-progress.json'),true,512,JSON_THROW_ON_ERROR); $case=App\Models\LandAssistanceCase::findOrFail($state['id']); $interest=App\Models\InterestRequest::findOrFail($case->interest_request_id); $counts=['case'=>App\Models\LandAssistanceCase::whereKey($case->id)->count(), 'interest'=>$db->table('interest_requests')->where('id',$case->interest_request_id)->count(), 'receipt'=>$db->table('interest_submission_receipts')->where('key_hash',hash('sha256',strtolower($case->crm_submission_key)))->count(), 'consent'=>$db->table('consents')->where('interest_request_id',$interest->id)->count(), 'outbox'=>$db->table('outbox_events')->where('aggregate_id',$interest->integration_id)->where('event_type','ExpressionOfInterestSubmitted')->count(), 'support_events'=>$db->table('land_assistance_events')->where('case_id',$case->id)->where('action','support_requested')->count()]; foreach($counts as $count)if($count!==1)throw new RuntimeException('Expected one preserved submission artifact'); if($case->version!==$state['current_version']||$case->payload['country']!=='Portugal'||$case->payload['declared_origin_country_code']!=='BR')throw new RuntimeException('Original submission changed'); $prior=json_decode(file_get_contents(__DIR__.'/option-B-preflight-before-enable.json'),true,512,JSON_THROW_ON_ERROR); $preserved=[]; foreach($prior['protected_table_snapshot']as$name=>$before){ if($name==='opportunities'||!$before['available'])continue; $rows=$db->table($name)->get();$serialized=$rows->map(fn($r)=>json_encode($r,JSON_THROW_ON_ERROR))->all();sort($serialized,SORT_STRING); $preserved[$name]=$rows->count()===$before['count']&&hash('sha256',implode("\n",$serialized))===$before['sha256']; } $o=App\Models\Opportunity::findOrFail(56); if($o->workflow_context['working_state']!=='published'||$o->workflow_context['publication']['visibility']!=='internal')throw new RuntimeException('QA franchise expected state differs'); $newTasks=$db->table('tasks')->where('created_at','>=',$prior['at'])->get(['id','source_system','external_id','status','assignee_user_id','due_at','created_at']); $taskRows=$db->table('tasks')->whereNotIn('id',$newTasks->pluck('id'))->get(); $taskBytes=$taskRows->map(fn($r)=>json_encode($r,JSON_THROW_ON_ERROR))->all();sort($taskBytes,SORT_STRING); $priorTasksPreserved=$taskRows->count()===$prior['protected_table_snapshot']['tasks']['count']&&hash('sha256',implode("\n",$taskBytes))===$prior['protected_table_snapshot']['tasks']['sha256']; $p=['at'=>gmdate('c'),'scope'=>'B','target'=>$target,'SQL_read_only'=>true,'active_writes'=>0, 'land_case_id'=>$case->id,'country'=>'Portugal','declared_origin'=>'BR','version_preserved_after_repeats'=>$case->version, 'one_submission_artifact_each'=>$counts,'country_and_origin_on_interest'=>['country'=>$interest->country,'origin'=>$interest->metadata['declared_origin_country_code']], 'prior_data_preserved'=>$preserved,'franchise'=>['id'=>56,'state'=>'published','visibility'=>'internal','approved_by'=>$o->workflow_context['approval']['actor_id'],'published_by'=>$o->workflow_context['publication']['actor_id']], 'path_available'=>app(App\Services\InvestorPath::class)->available(),'notifications_enabled'=>config('portal.notifications_enabled'), 'changed_snapshot_tables'=>array_keys(array_filter($preserved,fn($v)=>!$v)), 'prior_tasks_preserved'=>$priorTasksPreserved,'new_workflow_tasks'=>$newTasks->all(), 'public_maturity'=>['id'=>33,'level'=>App\Models\Opportunity::findOrFail(33)->maturity_level, 'label'=>App\Models\Opportunity::findOrFail(33)->maturity_label, 'catalogue_version'=>App\Models\Opportunity::findOrFail(33)->maturity_catalogue_version]]; if(!$priorTasksPreserved||array_keys(array_filter($preserved,fn($v)=>!$v))!==['tasks'])throw new RuntimeException('Unexpected unrelated changes'); $allowedTaskPattern=$includeVariants?'/^portal-opportunity:(30|32|33|34|56):/':'/^portal-opportunity:(33|56):/'; foreach($newTasks as$task)if($task->source_system!=='opportunity-workflow'||!preg_match($allowedTaskPattern,$task->external_id)||$task->due_at!==null)throw new RuntimeException('Unexpected task or invented deadline'); if($includeVariants){$p['category_variants']=[];foreach([30,32,33,34,56]as$id){$o=App\Models\Opportunity::findOrFail($id);if($o->workflow_context['working_state']!=='published')throw new RuntimeException('Variant still incomplete');$p['category_variants'][]=['id'=>$id,'category'=>$o->opportunity_category,'state'=>$o->workflow_context['working_state'],'visibility'=>$o->workflow_context['publication']['visibility'],'approved_by'=>$o->workflow_context['approval']['actor_id'],'published_by'=>$o->workflow_context['publication']['actor_id']];}} $output=$includeVariants?'option-B-final2-SQL.json':'option-B-final-SQL.json'; if($includeVariants&&is_file(__DIR__.'/'.$output))throw new RuntimeException('Preserve final snapshot'); file_put_contents(__DIR__.'/'.$output,json_encode($p,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo json_encode($p).PHP_EOL; }finally{$db->rollBack();}