2025-11-17 15:27:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
2025-11-20 11:54:08 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2025-11-17 15:27:21 +01:00
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
2025-11-20 11:54:08 +01:00
|
|
|
use App\Jobs\SyncBackendDataPool;
|
|
|
|
|
use App\Jobs\TransformDataPoolToProduction;
|
2025-11-17 15:27:21 +01:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
2025-11-20 11:54:08 +01:00
|
|
|
use function Laravel\Prompts\confirm;
|
|
|
|
|
use function Laravel\Prompts\info;
|
|
|
|
|
use function Laravel\Prompts\spin;
|
|
|
|
|
use function Laravel\Prompts\warning;
|
|
|
|
|
|
2025-11-17 15:27:21 +01:00
|
|
|
class RebuildFromDataPoolCommand extends Command
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The name and signature of the console command.
|
|
|
|
|
*/
|
2025-11-20 11:54:08 +01:00
|
|
|
protected $signature = 'backend:rebuild
|
|
|
|
|
{--batch-size=1000 : Number of records to process per batch}
|
|
|
|
|
{--transform-batch-size=100 : Number of records to process per batch for transformation}
|
|
|
|
|
{--queue : Dispatch jobs to the queue instead of running synchronously}';
|
2025-11-17 15:27:21 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The console command description.
|
|
|
|
|
*/
|
2025-11-20 11:54:08 +01:00
|
|
|
protected $description = 'Complete rebuild: Full sync from backend + transformation to production tables';
|
2025-11-17 15:27:21 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*/
|
2025-11-20 11:54:08 +01:00
|
|
|
public function handle(): int
|
2025-11-17 15:27:21 +01:00
|
|
|
{
|
2025-11-20 11:54:08 +01:00
|
|
|
info('Preparing for COMPLETE REBUILD');
|
|
|
|
|
warning('This will:');
|
|
|
|
|
warning('1. Truncate and rebuild backend_data_pool (full sync)');
|
|
|
|
|
warning('2. Transform all data into companies and transactions');
|
|
|
|
|
|
|
|
|
|
if (! $this->option('no-interaction') && ! confirm('Do you want to continue?', true)) {
|
|
|
|
|
info('Rebuild cancelled.');
|
|
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$batchSize = (int) $this->option('batch-size');
|
|
|
|
|
$transformBatchSize = (int) $this->option('transform-batch-size');
|
|
|
|
|
$useQueue = $this->option('queue');
|
|
|
|
|
|
|
|
|
|
// Step 1: Full Sync
|
|
|
|
|
info('Step 1/2: Full Sync from Backend');
|
|
|
|
|
$syncJob = new SyncBackendDataPool(
|
|
|
|
|
fullSync: true,
|
|
|
|
|
batchSize: $batchSize
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($useQueue) {
|
|
|
|
|
info('Dispatching sync job to queue...');
|
|
|
|
|
dispatch($syncJob);
|
|
|
|
|
info('Sync job dispatched!');
|
|
|
|
|
warning('Please wait for the sync job to complete before transformation.');
|
|
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
|
|
|
|
} else {
|
|
|
|
|
spin(
|
|
|
|
|
fn () => $syncJob->handle(),
|
|
|
|
|
'Syncing data from backend...'
|
|
|
|
|
);
|
|
|
|
|
info('✓ Sync completed!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->newLine();
|
|
|
|
|
|
|
|
|
|
// Step 2: Transform Data
|
|
|
|
|
info('Step 2/2: Transform Data Pool to Production');
|
|
|
|
|
$transformJob = new TransformDataPoolToProduction(batchSize: $transformBatchSize);
|
|
|
|
|
|
|
|
|
|
spin(
|
|
|
|
|
fn () => $transformJob->handle(),
|
|
|
|
|
'Transforming data...'
|
|
|
|
|
);
|
|
|
|
|
info('✓ Transformation completed!');
|
|
|
|
|
|
|
|
|
|
$this->newLine();
|
|
|
|
|
info('Complete rebuild finished successfully!');
|
|
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
2025-11-17 15:27:21 +01:00
|
|
|
}
|
|
|
|
|
}
|