From 5148a8bbb911232bc9dc524b2bcc08d53455103f Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 2 Oct 2019 18:03:58 +0200 Subject: [PATCH] Fix #2480 --- .../Commands/Correction/DeleteEmptyGroups.php | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/app/Console/Commands/Correction/DeleteEmptyGroups.php b/app/Console/Commands/Correction/DeleteEmptyGroups.php index d56851f16a..a0558a9e09 100644 --- a/app/Console/Commands/Correction/DeleteEmptyGroups.php +++ b/app/Console/Commands/Correction/DeleteEmptyGroups.php @@ -56,13 +56,28 @@ class DeleteEmptyGroups extends Command { $start = microtime(true); $groups = array_unique(TransactionJournal::get(['transaction_group_id'])->pluck('transaction_group_id')->toArray()); - $count = TransactionGroup::whereNull('deleted_at')->whereNotIn('id', $groups)->count(); - if (0 === $count) { + + // make chunks so SQLite can take the strain: + $chunks = array_chunk($groups, 500); + $total = 0; + foreach ($chunks as $chunk) { + $count = TransactionGroup::whereNull('deleted_at')->whereNotIn('id', $chunk)->count(); + $total += $count; + } + if (0 === $total) { $this->info('No empty transaction groups.'); } - if ($count > 0) { - $this->info(sprintf('Deleted %d empty transaction group(s).', $count)); - TransactionGroup::whereNull('deleted_at')->whereNotIn('id', $groups)->delete(); + + unset($chunks, $chunk, $count); + + if ($total > 0) { + $this->info(sprintf('Deleted %d empty transaction group(s).', $total)); + + // again, chunks for SQLite. + $chunks = array_chunk($groups, 500); + foreach ($chunks as $chunk) { + TransactionGroup::whereNull('deleted_at')->whereNotIn('id', $chunk)->delete(); + } } $end = round(microtime(true) - $start, 2); $this->info(sprintf('Verified empty groups in %s seconds', $end));