Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/14960 nested elements gc #14967

Merged
merged 6 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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