ADD disable foreign key for cleanup command
This commit is contained in:
parent
de1eb871b9
commit
ab76515822
@ -115,47 +115,53 @@ public function handle(): int
|
||||
|
||||
$this->info('Törlés megkezdése...');
|
||||
|
||||
// Orders törlése
|
||||
Order::where('deliveryDate', '<', $date)->delete();
|
||||
$this->info('Megrendelések törölve.');
|
||||
\Illuminate\Support\Facades\Schema::disableForeignKeyConstraints();
|
||||
|
||||
// Archivált adatok törlése optimalizáltan
|
||||
$this->info('Archivált adatok törlése...');
|
||||
$bar = $this->output->createProgressBar($archivesCount);
|
||||
$bar->start();
|
||||
try {
|
||||
// Orders törlése
|
||||
Order::where('deliveryDate', '<', $date)->delete();
|
||||
$this->info('Megrendelések törölve.');
|
||||
|
||||
OrderArchive::where('deliveryDate', '<', $date)->chunkById(1000, function ($archives) use ($bar) {
|
||||
$archiveIds = $archives->pluck('id')->toArray();
|
||||
// Archivált adatok törlése optimalizáltan
|
||||
$this->info('Archivált adatok törlése...');
|
||||
$bar = $this->output->createProgressBar($archivesCount);
|
||||
$bar->start();
|
||||
|
||||
// Kapcsolódó tételek törlése tömegesen
|
||||
OrderArchivesItem::whereIn('order_archive_id', $archiveIds)->delete();
|
||||
OrderArchive::where('deliveryDate', '<', $date)->chunkById(1000, function ($archives) use ($bar) {
|
||||
$archiveIds = $archives->pluck('id')->toArray();
|
||||
|
||||
// Csatolmányok és fizikai fájlok kezelése
|
||||
$attachments = Attachment::whereIn('attachable_id', $archiveIds)
|
||||
->where('attachable_type', OrderArchive::class)
|
||||
->get();
|
||||
// Kapcsolódó tételek törlése tömegesen
|
||||
OrderArchivesItem::whereIn('order_archive_id', $archiveIds)->delete();
|
||||
|
||||
if ($attachments->isNotEmpty()) {
|
||||
$filePaths = $attachments->map(function ($attachment) {
|
||||
return $attachment->path . DIRECTORY_SEPARATOR . $attachment->filename;
|
||||
})->toArray();
|
||||
// Csatolmányok és fizikai fájlok kezelése
|
||||
$attachments = Attachment::whereIn('attachable_id', $archiveIds)
|
||||
->where('attachable_type', OrderArchive::class)
|
||||
->get();
|
||||
|
||||
// Fizikai fájlok törlése a tárhelyről kötegelve
|
||||
Storage::disk('attachments')->delete($filePaths);
|
||||
if ($attachments->isNotEmpty()) {
|
||||
$filePaths = $attachments->map(function ($attachment) {
|
||||
return $attachment->path . DIRECTORY_SEPARATOR . $attachment->filename;
|
||||
})->toArray();
|
||||
|
||||
// Attachment rekordok törlése az adatbázisból
|
||||
Attachment::whereIn('id', $attachments->pluck('id'))->delete();
|
||||
}
|
||||
// Fizikai fájlok törlése a tárhelyről kötegelve
|
||||
Storage::disk('attachments')->delete($filePaths);
|
||||
|
||||
// Maguk az archívum rekordok törlése tömegesen
|
||||
OrderArchive::whereIn('id', $archiveIds)->delete();
|
||||
// Attachment rekordok törlése az adatbázisból
|
||||
Attachment::whereIn('id', $attachments->pluck('id'))->delete();
|
||||
}
|
||||
|
||||
$bar->advance($archives->count());
|
||||
});
|
||||
// Maguk az archívum rekordok törlése tömegesen
|
||||
OrderArchive::whereIn('id', $archiveIds)->delete();
|
||||
|
||||
$bar->finish();
|
||||
$this->info("");
|
||||
$this->info('Archivált megrendelések és kapcsolódó elemek törölve.');
|
||||
$bar->advance($archives->count());
|
||||
});
|
||||
|
||||
$bar->finish();
|
||||
$this->info("");
|
||||
$this->info('Archivált megrendelések és kapcsolódó elemek törölve.');
|
||||
} finally {
|
||||
\Illuminate\Support\Facades\Schema::enableForeignKeyConstraints();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -91,7 +91,7 @@ public function handle(): int
|
||||
}
|
||||
|
||||
$this->info('Törlés megkezdése...');
|
||||
|
||||
\Illuminate\Support\Facades\Schema::disableForeignKeyConstraints();
|
||||
// PriceLists törlése optimalizáltan
|
||||
if ($priceListsCount > 0) {
|
||||
$this->info('Árlisták és kapcsolódó árak törlése...');
|
||||
@ -151,7 +151,7 @@ public function handle(): int
|
||||
}
|
||||
|
||||
$this->info('A tisztítás sikeresen befejeződött.');
|
||||
|
||||
\Illuminate\Support\Facades\Schema::enableForeignKeyConstraints();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user