refactor: enhance company migration command to include partner media document migration and optimize legacy data fetching
This commit is contained in:
parent
ce95dccaf2
commit
516e6b3de5
@ -8,6 +8,7 @@
|
||||
use App\Enums\RoleEnum;
|
||||
use App\Enums\VerificationStatus;
|
||||
use App\Models\Company;
|
||||
use App\Models\PartnerMedia;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
@ -36,6 +37,11 @@ class MigrateCompanyCommand extends Command
|
||||
*/
|
||||
private array $migratedIds = [];
|
||||
|
||||
/**
|
||||
* Cache for migrated partner media IDs.
|
||||
*/
|
||||
private array $migratedPartnerMediaIds = [];
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
@ -44,7 +50,7 @@ public function handle()
|
||||
$this->info('Starting optimized company migration with document support...');
|
||||
|
||||
$legacyConn = DB::connection('mysql_second');
|
||||
$query = $legacyConn->table('companies');
|
||||
$query = $legacyConn->table('companies')->whereNull('deleted_at');
|
||||
|
||||
$totalCount = $query->count();
|
||||
if ($totalCount === 0) {
|
||||
@ -63,6 +69,13 @@ public function handle()
|
||||
->flip()
|
||||
->all();
|
||||
|
||||
$this->migratedPartnerMediaIds = DB::table('media')
|
||||
->where('model_type', PartnerMedia::class)
|
||||
->where('collection_name', 'partnerMedia')
|
||||
->pluck('model_id')
|
||||
->flip()
|
||||
->all();
|
||||
|
||||
$query->orderBy('id')->chunk(50, function ($chunk) {
|
||||
foreach ($chunk as $legacy) {
|
||||
$this->processCompany($legacy);
|
||||
@ -210,6 +223,14 @@ private function handlePartnerMedia($legacy): void
|
||||
'deleted_at' => $legacyMedia->deleted_at,
|
||||
]
|
||||
);
|
||||
|
||||
// Migrate documents for each partner media
|
||||
if (! isset($this->migratedPartnerMediaIds[$legacyMedia->id])) {
|
||||
$partnerMedia = PartnerMedia::withTrashed()->find($legacyMedia->id);
|
||||
if ($partnerMedia) {
|
||||
$this->migrateMediaDocuments($partnerMedia, $legacyMedia);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -279,8 +300,67 @@ private function importOneDocument(Company $company, string $sourcePath, string
|
||||
}
|
||||
}
|
||||
|
||||
// ... mapping methods (mapVerificationStatus, mapDecision, mapMediaType, mapMediaClassification, extractMediaLink) remain as previously defined
|
||||
/**
|
||||
* Migrate all media documents from S3.
|
||||
*/
|
||||
private function migrateMediaDocuments(PartnerMedia $media, $legacyMedia): void
|
||||
{
|
||||
$docMapping = [
|
||||
'doc_of_organization' => 'journalism-organization',
|
||||
'doc_of_certificate' => 'press-council-certificate',
|
||||
];
|
||||
|
||||
foreach ($docMapping as $legacyField => $docType) {
|
||||
$legacyPath = trim($legacyMedia->{$legacyField} ?? '');
|
||||
if (empty($legacyPath) || $legacyPath === '-' || $legacyPath === 'no-image.png') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Path mapping: old data/media/ + path from db
|
||||
$sourcePath = 'old data/media/'.$legacyPath;
|
||||
|
||||
$this->importOneMediaDocument($media, $sourcePath, $docType, $legacyMedia);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a single media document to Media Library.
|
||||
*/
|
||||
private function importOneMediaDocument(PartnerMedia $media, string $sourcePath, string $docType, $legacyMedia): void
|
||||
{
|
||||
$attempts = 0;
|
||||
$maxAttempts = 3;
|
||||
$exists = false;
|
||||
|
||||
while ($attempts < $maxAttempts) {
|
||||
try {
|
||||
$exists = Storage::disk('s3')->exists($sourcePath);
|
||||
break;
|
||||
} catch (\Exception $e) {
|
||||
$attempts++;
|
||||
usleep(200000);
|
||||
}
|
||||
}
|
||||
|
||||
if (! $exists) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$media->addMediaFromDisk($sourcePath, 's3')
|
||||
->withCustomProperties([
|
||||
'feature' => 'media',
|
||||
'date' => Carbon::parse($legacyMedia->created_at ?? now())->toDateString(),
|
||||
'doc_type' => $docType, // Match MediaSaveService slug pattern
|
||||
])
|
||||
->preservingOriginal()
|
||||
->toMediaCollection('partnerMedia', 's3');
|
||||
} catch (\Exception $e) {
|
||||
// Log quietly or handle error
|
||||
}
|
||||
}
|
||||
|
||||
// ... mapping methods (mapVerificationStatus, mapDecision, mapMediaType, mapMediaClassification, extractMediaLink) remain as previously defined
|
||||
private function mapVerificationStatus($legacyStatus): int
|
||||
{
|
||||
return match ((string) $legacyStatus) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user