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