From 77faa5bfe44b316dfe45850bd350eda64e4088e2 Mon Sep 17 00:00:00 2001 From: Thomas Templeton Date: Thu, 21 Mar 2024 13:14:57 +1100 Subject: [PATCH] Fix #866 --- CHANGELOG.md | 5 +++++ src/Plugin.php | 36 +++++++++++++++++------------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa219f68..a91a1894 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +### Fixed +- Fixed an error that could occur during Craft garbage collection if the `neoblockstructures` table contained more than 65535 rows + ## 2.13.20 - 2024-02-23 ### Added diff --git a/src/Plugin.php b/src/Plugin.php index 03b38216..abd172c9 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -199,25 +199,23 @@ private function _registerGarbageCollection() // Delete anything in the structures table that's a Neo block structure, but doesn't exist in the // neoblockstructures table $stdout(' > deleting orphaned Neo block structure data ... '); - Db::delete(Table::STRUCTURES, [ - 'and', - [ - 'id' => (new Query()) - ->select(['structureId']) - ->from(['se' => Table::STRUCTUREELEMENTS]) - ->innerJoin(['nb' => '{{%neoblocks}}'], '[[se.elementId]] = [[nb.id]]') - ->column(), - ], - [ - 'not', - [ - 'id' => (new Query()) - ->select(['structureId']) - ->from('{{%neoblockstructures}}') - ->column(), - ] - ], - ]); + $neoStructureIds = (new Query()) + ->select(['structureId']) + ->from(['se' => Table::STRUCTUREELEMENTS]) + ->innerJoin(['nb' => '{{%neoblocks}}'], '[[se.elementId]] = [[nb.id]]') + ->column(); + $neoStructureIdsNotToDelete = (new Query()) + ->select(['structureId']) + ->from('{{%neoblockstructures}}') + ->column(); + $neoStructureIdsToDelete = array_diff($neoStructureIds, $neoStructureIdsNotToDelete); + + foreach (array_chunk($neoStructureIdsToDelete, 1000) as $neoStructureIdChunk) { + Db::delete(Table::STRUCTURES, [ + 'id' => $neoStructureIdChunk, + ]); + } + $stdout("done\n", Console::FG_GREEN); }); }