make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); use Illuminate\Support\Facades\DB;use App\Models\{User,Plan,Server,ServerMachine}; if(DB::connection()->getDatabaseName()!=='fuc_xb_test')throw new RuntimeException('Wrong target database'); $summary=json_decode(file_get_contents($dir.'/projection-summary.json'),true,512,JSON_THROW_ON_ERROR); if(!hash_equals($summary['projection_sha256'],$options['apply'])||!hash_equals($options['apply'],hash_file('sha256',$dir.'/projection.json')))throw new RuntimeException('Projection checksum changed'); $backup=json_decode(file_get_contents($private.'/refresh-20261105-before/receipt.json'),true,512,JSON_THROW_ON_ERROR); if(!$backup['passed']||!$backup['all_table_rows_and_content_hashes_equal']||!hash_equals($backup['backup_sha256'],hash_file('sha256',$private.'/refresh-20261105-before/fuc_xb_test.sql.gz')))throw new RuntimeException('Verified backup required'); $marker=$dir.'/refresh-applied.json';if(file_exists($marker))throw new RuntimeException('Refresh already journaled; verify before retry'); $data=json_decode(file_get_contents($dir.'/projection.json'),true,512,JSON_THROW_ON_ERROR); if(count($data['users'])!==$summary['users']||count($data['vip_openings'])!==$summary['users'])throw new RuntimeException('Projection count mismatch'); foreach(['v2_order','v2_commission_log','personal_access_tokens','v2_stat_user','v2_stat_server']as$table)if(DB::table($table)->exists())throw new RuntimeException('Unexpected target business rows: '.$table); if(Server::count()||ServerMachine::count()||DB::table('fuc_vip_entries')->where('kind','!=','opening')->exists())throw new RuntimeException('Unexpected test node or VIP activity'); $qaFile=$private.'/qa-credentials.json';$qa=json_decode(file_get_contents($qaFile),true,512,JSON_THROW_ON_ERROR);$qaUser=User::findOrFail($qa['user_id']);$qaPlan=Plan::findOrFail($qa['plan_id']); if($qaUser->email!=='qa@fuc-xb.invalid'||$qaUser->is_admin||$qaUser->is_staff)throw new RuntimeException('QA identity mismatch'); $adminRows=array_values(array_filter($data['users'],fn($r)=>$r['email']===$secrets['username']));if(count($adminRows)!==1)throw new RuntimeException('Default admin missing or duplicated');$admin=$adminRows[0]; if(!App\Utils\Helper::multiPasswordVerify($admin['password_algo'],$admin['password_salt'],$secrets['password'],$admin['password']))throw new RuntimeException('Source default admin credential changed; not overwritten'); $qaRow=$qaUser->getAttributes();$qaPlanRow=$qaPlan->getAttributes();$qaRow['id']=max(array_column($data['users'],'id'))+1;$qaRow['plan_id']=null;$qaRow['group_id']=null;$qaPlanRow['id']=max(array_column($data['plans'],'id'))+1; $qaRow['balance']=100000;$qaRow['u']=$qaRow['d']=0;$qaRow['transfer_enable']=0;$qaRow['expired_at']=0;$qaRow['discount']=null; $qa['user_id']=$qaRow['id'];$qa['plan_id']=$qaPlanRow['id']; app(App\Services\Plugin\PluginManager::class)->initializeEnabledPlugins(); $before=['users'=>User::count(),'plans'=>Plan::count(),'groups'=>DB::table('v2_server_group')->count()]; file_put_contents($dir.'/refresh-intent.json',json_encode(['projection_sha256'=>$options['apply'],'before'=>$before,'backup_sha256'=>$backup['backup_sha256']])); function normalizeRefresh($r):array{return array_map(fn($v)=>$v===null?null:(string)$v,(array)$r);} DB::transaction(function()use($data,$summary,$qaRow,$qaPlanRow,$admin){ foreach(['v2_traffic_reset_logs','fuc_vip_entries','fuc_vip_accounts','v2_user','v2_plan','v2_server_group']as$t)DB::table($t)->delete(); foreach(['v2_server_group'=>'groups','v2_plan'=>'plans','v2_user'=>'users']as$t=>$key){ foreach($data[$key]as$row)DB::table($t)->insert($row); foreach($data[$key]as$row){$actual=DB::table($t)->where('id',$row['id'])->first();foreach($row as$k=>$v)if(normalizeRefresh([$v])!==normalizeRefresh([$actual->$k]))throw new RuntimeException('Full-field refresh mismatch: '.$t.'.'.$k);} } foreach($data['vip_openings']as$v)app(Plugin\VipLoyalty\Service::class)->opening($v['user_id'],$v['uuid'],$v['opening_cumulative_cents'],$data['snapshot_sha256']); if((int)User::sum('balance')!==$summary['wallet_cents']||(int)User::sum('commission_balance')!==$summary['commission_cents']||DB::table('fuc_vip_accounts')->count()!==$summary['users'])throw new RuntimeException('Refresh money or VIP mismatch'); DB::table('v2_user')->where('id',$admin['id'])->update(['is_admin'=>1]);DB::table('v2_user')->insert($qaRow);DB::table('v2_plan')->insert($qaPlanRow); }); $temp=$qaFile.'.refresh';file_put_contents($temp,json_encode($qa));chmod($temp,0600);rename($temp,$qaFile); $result=['passed'=>true,'snapshot_sha256'=>$data['snapshot_sha256'],'migrated_users'=>$summary['users'],'hidden_plans'=>$summary['hidden_legacy_plans'],'groups'=>$summary['groups'],'wallet_cents'=>$summary['wallet_cents'],'commission_cents'=>$summary['commission_cents'],'prepaid_refund_orders'=>$summary['prepaid_orders'],'prepaid_refund_cents'=>$summary['prepaid_refund_cents'],'vip_openings'=>DB::table('fuc_vip_accounts')->count(),'total_users'=>User::count(),'admins'=>User::where('is_admin',1)->count(),'full_field_readback'=>true,'qa_credentials_preserved'=>true,'native_orders'=>DB::table('v2_order')->count(),'source_writes'=>0,'shared_core_changed'=>false]; file_put_contents($marker,json_encode($result,JSON_PRETTY_PRINT));chmod($marker,0600);echo json_encode($result).PHP_EOL;