Skip to content

Commit

Permalink
Fix #866
Browse files Browse the repository at this point in the history
  • Loading branch information
ttempleton committed Mar 21, 2024
1 parent 7b514e8 commit 77faa5b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 17 additions & 19 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down

0 comments on commit 77faa5b

Please sign in to comment.