Skip to content

Commit

Permalink
Merge pull request #13412 from craftcms/bugfix/13383-gc-for-orphaned-…
Browse files Browse the repository at this point in the history
…entries

GC for orphaned site entries
  • Loading branch information
brandonkelly authored Jul 14, 2023
2 parents 5cad5ed + d5836ea commit 30c6610
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Improved performance when loading users with eager-loaded `addresses`. ([#13400](https://github.com/craftcms/cms/issues/13400))
- `createDraft` GraphQL mutations now support a `creatorId` argument. ([#13401](https://github.com/craftcms/cms/issues/13401))
- Garbage collection now deletes entries for sites that aren’t supported by their section. ([#13383](https://github.com/craftcms/cms/issues/13383))
- Added `craft\elements\Address::setOwner()`.
- `craft\base\ElementInterface::eagerLoadingMap()` can now include a `createElement` key in the returned array, which defines a target element factory function.
- Fixed a bug where entry titles could overflow within Entries fields with “Maintain hierarchy” enabled. ([#13382](https://github.com/craftcms/cms/issues/13382))
Expand Down
58 changes: 58 additions & 0 deletions src/services/Gc.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ public function run(bool $force = false): void
$this->deletePartialElements(Tag::class, Table::CONTENT, 'elementId');
$this->deletePartialElements(User::class, Table::CONTENT, 'elementId');

$this->_deleteUnsupportedSiteEntries();

$this->_deleteOrphanedDraftsAndRevisions();
$this->_deleteOrphanedSearchIndexes();
$this->_deleteOrphanedRelations();
Expand Down Expand Up @@ -416,6 +418,62 @@ private function _deleteStaleAnnouncements(): void
}


/**
* Deletes entries for sites that aren’t enabled by their section.
*
* This can happen if you entrify a category group, disable one of the sites in the newly-created section’s
* settings, then deploy those changes to another environment, apply project config changes, and re-run the
* entrify command. (https://github.com/craftcms/cms/issues/13383)
*/
private function _deleteUnsupportedSiteEntries(): void
{
$this->_stdout(' > deleting entries in unsupported sites ... ');

$sectionsToCheck = [];
$siteIds = Craft::$app->getSites()->getAllSiteIds(true);

// get sections that are not enabled for given site
foreach (Craft::$app->getSections()->getAllSections() as $section) {
$sectionSettings = $section->getSiteSettings();
foreach ($siteIds as $siteId) {
if (!isset($sectionSettings[$siteId])) {
$sectionsToCheck[] = [
'siteId' => $siteId,
'sectionId' => $section->id,
];
}
}
}

if (!empty($sectionsToCheck)) {
$elementsSitesTable = Table::ELEMENTS_SITES;
$entriesTable = Table::ENTRIES;

if ($this->db->getIsMysql()) {
$sql = <<<SQL
DELETE [[es]].* FROM $elementsSitesTable [[es]]
LEFT JOIN $entriesTable [[en]] ON [[en.id]] = [[es.elementId]]
WHERE [[en.sectionId]] = :sectionId AND [[es.siteId]] = :siteId
SQL;
} else {
$sql = <<<SQL
DELETE FROM $elementsSitesTable
USING $elementsSitesTable [[es]]
LEFT JOIN $entriesTable [[en]] ON [[en.id]] = [[es.elementId]]
WHERE
$elementsSitesTable.[[id]] = [[es.id]] AND
[[en.sectionId]] = :sectionId AND [[es.siteId]] = :siteId
SQL;
}

foreach ($sectionsToCheck as $params) {
$this->db->createCommand($sql, $params)->execute();
}
}

$this->_stdout("done\n", Console::FG_GREEN);
}

/**
* Deletes any orphaned rows in the `drafts` and `revisions` tables.
*/
Expand Down

0 comments on commit 30c6610

Please sign in to comment.