Skip to content

Commit

Permalink
Merge pull request #14967 from craftcms/bugfix/14960-nested-elements-gc
Browse files Browse the repository at this point in the history
Bugfix/14960 nested elements gc
  • Loading branch information
brandonkelly authored May 13, 2024
2 parents c64566f + d09be13 commit bd1025e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Fixed a bug where entry selection modals could list all entries when no sources were available for the selected site. ([#14956](https://github.com/craftcms/cms/issues/14956))
- Fixed a bug where element cards could get duplicate status indicators. ([#14958](https://github.com/craftcms/cms/issues/14958))
- Fixed a bug where element chips could overflow their containers. ([#14924](https://github.com/craftcms/cms/issues/14924))
- Fixed a bug where soft-deleted elements that belonged to a revision could be deleted by garbage collection. ([#14967](https://github.com/craftcms/cms/pull/14967))

## 5.1.2 - 2024-05-07

Expand Down
36 changes: 35 additions & 1 deletion src/services/Gc.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,64 @@ public function hardDeleteElements(): void
}

if (!empty($nestedElementTypes)) {
// Only hard-delete nested elements that don't have any revisions
$elementsTable = Table::ELEMENTS;
$revisionsTable = Table::REVISIONS;
$elementsOwnersTable = Table::ELEMENTS_OWNERS;

// first hard-delete nested elements which are not nested (owned) and that don't have any revisions
$params = [];
$conditionSql = $this->db->getQueryBuilder()->buildCondition([
'and',
$this->_hardDeleteCondition('e'),
[
'e.type' => $nestedElementTypes,
'r.id' => null,
'eo.elementId' => null,
],
], $params);

if ($this->db->getIsMysql()) {
$sql = <<<SQL
DELETE [[e]].* FROM $elementsTable [[e]]
LEFT JOIN $revisionsTable [[r]] ON [[r.canonicalId]] = [[e.id]]
LEFT JOIN $elementsOwnersTable [[eo]] ON [[eo.elementId]] = COALESCE([[e.canonicalId]], [[e.id]])
WHERE $conditionSql
SQL;
} else {
$sql = <<<SQL
DELETE FROM $elementsTable
USING $elementsTable [[e]]
LEFT JOIN $revisionsTable [[r]] ON [[r.canonicalId]] = [[e.id]]
LEFT JOIN $elementsOwnersTable [[eo]] ON [[eo.elementId]] = COALESCE([[e.canonicalId]], [[e.id]])
WHERE
$elementsTable.[[id]] = [[e.id]] AND $conditionSql
SQL;
}

$this->db->createCommand($sql, $params)->execute();

// then hard-delete any nested elements that don't have any revisions, including nested ones
$params = [];
$conditionSql = $this->db->getQueryBuilder()->buildCondition([
'and',
$this->_hardDeleteCondition('e'),
[
'e.type' => $nestedElementTypes,
'r.id' => null,
],
], $params);

if ($this->db->getIsMysql()) {
$sql = <<<SQL
DELETE [[e]].* FROM $elementsTable [[e]]
LEFT JOIN $revisionsTable [[r]] ON [[r.canonicalId]] = COALESCE([[e.canonicalId]], [[e.id]])
WHERE $conditionSql
SQL;
} else {
$sql = <<<SQL
DELETE FROM $elementsTable
USING $elementsTable [[e]]
LEFT JOIN $revisionsTable [[r]] ON [[r.canonicalId]] = COALESCE([[e.canonicalId]], [[e.id]])
WHERE
$elementsTable.[[id]] = [[e.id]] AND $conditionSql
SQL;
Expand Down

0 comments on commit bd1025e

Please sign in to comment.