From f3072f11386c2b505364ee3b58f2297994bd0b46 Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink Date: Wed, 4 Nov 2026 10:11:55 +0100 Subject: [PATCH] fix(migrations): skip deleted-site content --- CHANGELOG.md | 6 ++ .../LegacyContentRelationRebuilder.php | 12 ++- src/services/Content.php | 13 +++ tests/Services/UpgradeRelationsTest.php | 84 +++++++++++++++++++ 4 files changed, 113 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 190c09d9..b75edf64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixed + +- Fixed upgrades failing when deleted sites retain Hyper content, without changing stored content or excluding disabled sites. + ## 3.0.3 - 2026-11-01 ### Fixed diff --git a/src/migrations/LegacyContentRelationRebuilder.php b/src/migrations/LegacyContentRelationRebuilder.php index e2cc2716..b8762245 100644 --- a/src/migrations/LegacyContentRelationRebuilder.php +++ b/src/migrations/LegacyContentRelationRebuilder.php @@ -151,6 +151,13 @@ private function _superTableContentTables(HyperField $field): array return [$ownTable, $tables]; } + /** + * Check that a legacy row belongs to an indexable element and an undeleted site. + * + * @param int $ownerId + * @param int $ownerSiteId + * @return bool + */ private function _isIndexableOwner(int $ownerId, int $ownerSiteId): bool { $owner = (new Query()) @@ -164,8 +171,9 @@ private function _isIndexableOwner(int $ownerId, int $ownerSiteId): bool } return (new Query()) - ->from(Table::ELEMENTS_SITES) - ->where(['elementId' => $ownerId, 'siteId' => $ownerSiteId]) + ->from(['content' => Table::ELEMENTS_SITES]) + ->innerJoin(['sites' => Table::SITES], '[[sites.id]] = [[content.siteId]]') + ->where(['content.elementId' => $ownerId, 'content.siteId' => $ownerSiteId, 'sites.dateDeleted' => null]) ->exists($this->_db); } diff --git a/src/services/Content.php b/src/services/Content.php index 1a9979f7..b88990d5 100644 --- a/src/services/Content.php +++ b/src/services/Content.php @@ -390,11 +390,24 @@ private function _syncRelations(HyperField $field, ContentRef $ref, ?ElementInte return true; } + /** + * Resolve a stored content owner, excluding unavailable sites during index rebuilds. + * + * @param ContentRef $ref + * @param bool $forReconciliation + * @return ElementInterface|null + */ private function _resolveRelationOwner(ContentRef $ref, bool $forReconciliation = false): ?ElementInterface { $elementId = $ref->elementId; $siteId = $ref->siteId; + // Deleted sites can retain content rows that Craft can no longer hydrate. + // Disabled sites remain indexable, matching Craft's site-ID normalization. + if ($forReconciliation && $siteId && Craft::$app->getSites()->getSiteById($siteId, true) === null) { + return null; + } + // Nested Matrix/Neo owners are identified by block entry id in the JSON path. if (preg_match('/\.(\d+)\.fields\./', $ref->jsonPath, $matches)) { $elementId = (int)$matches[1]; diff --git a/tests/Services/UpgradeRelationsTest.php b/tests/Services/UpgradeRelationsTest.php index 763aa31d..4d94de2f 100644 --- a/tests/Services/UpgradeRelationsTest.php +++ b/tests/Services/UpgradeRelationsTest.php @@ -318,6 +318,90 @@ public function safeDown(): bool } }); +it('excludes deleted sites but retains disabled sites when rebuilding current and legacy relations', function(string $migrationClass, bool $deleted) { + LegacyElementCacheSeeder::ensureLegacyTable(); + [$primary, $secondary] = F::ensureSites(2); + $field = F::hyperField(); + $section = F::entrySection($field, allSites: true); + $target = F::plainEntry($section, 'Target'); + $owner = F::plainEntry($section, 'Current owner'); + $legacyOwner = F::plainEntry($section, 'Legacy owner'); + $db = Craft::$app->db; + $sites = Craft::$app->getSites(); + $siteBefore = (new Query())->select(['dateDeleted', 'enabled'])->from('{{%sites}}')->where(['id' => $secondary->id])->one(); + $uid = $owner->getFieldLayout()->getFieldByHandle($field->handle)->layoutElement->uid; + $table = '{{%hyper_test_deleted_site_content}}'; + $column = sprintf('field_%s%s', $field->handle, $field->columnSuffix ? '_' . $field->columnSuffix : ''); + $migration = new class extends Migration { + public function safeUp(): bool + { + return true; + } + + public function safeDown(): bool + { + return true; + } + }; + $migration->createTable($table, [ + 'id' => $migration->primaryKey(), + 'elementId' => $migration->integer()->notNull(), + 'siteId' => $migration->integer()->notNull(), + $column => $migration->text(), + ]); + $db->getSchema()->refresh(); + + try { + foreach ([$primary, $secondary] as $site) { + $payload = [F::entryLinkPayload($target, 'Retained site link')]; + expect($db->createCommand()->update('{{%elements_sites}}', [ + 'content' => new yii\db\JsonExpression([$uid => $payload]), + ], ['elementId' => $owner->id, 'siteId' => $site->id])->execute())->toBe(1); + $db->createCommand()->insert($table, [ + 'elementId' => $legacyOwner->id, + 'siteId' => $site->id, + $column => Json::encode($payload), + ])->execute(); + } + + $readContent = fn() => (new Query())->select(['elementId', 'siteId', 'content'])->from('{{%elements_sites}}') + ->where(['elementId' => [$owner->id, $legacyOwner->id]])->orderBy('id')->all(); + $readLegacy = fn() => (new Query())->from($table)->orderBy('id')->all(); + $readRelations = fn() => (new Query())->select(['ownerId', 'ownerSiteId', 'targetId'])->from('{{%hyper_links}}') + ->where(['fieldId' => $field->id])->orderBy(['ownerId' => SORT_ASC, 'ownerSiteId' => SORT_ASC])->all(); + $contentBefore = $readContent(); + $legacyBefore = $readLegacy(); + $relationsBefore = $readRelations(); + + // Retain both content stores exactly as they can remain after site deletion. + $db->createCommand()->update('{{%sites}}', $deleted + ? ['dateDeleted' => '2025-07-01 00:00:00'] + : ['enabled' => false], ['id' => $secondary->id])->execute(); + $sites->refreshSites(); + expect($sites->getSiteById($secondary->id, true) === null)->toBe($deleted); + + expect(Hyper::$plugin->content->reconcileRelations($field, new ModifyOptions(dryRun: true)))->toBe($deleted ? 1 : 2); + expect($readRelations())->toBe($relationsBefore); + expect((new $migrationClass())->safeUp())->toBeTrue(); + + $siteIds = $deleted ? [$primary->id] : [$primary->id, $secondary->id]; + sort($siteIds); + foreach ([$owner, $legacyOwner] as $indexedOwner) { + $rows = array_values(array_filter($readRelations(), fn(array $row) => (int)$row['ownerId'] === $indexedOwner->id)); + expect(array_map('intval', array_column($rows, 'ownerSiteId')))->toBe($siteIds); + expect(array_map('intval', array_column($rows, 'targetId')))->toBe(array_fill(0, count($siteIds), $target->id)); + } + expect($readContent())->toBe($contentBefore); + expect($readLegacy())->toBe($legacyBefore); + } finally { + Rebuild::$rebuiltByBackfill = false; + $db->createCommand()->update('{{%sites}}', $siteBefore, ['id' => $secondary->id])->execute(); + $sites->refreshSites(); + $migration->dropTableIfExists($table); + $db->getSchema()->refresh(); + } +})->with([Backfill::class, Rebuild::class])->with(['deleted' => true, 'disabled' => false]); + it('preserves unavailable owner content and resumes indexing when its element type returns', function() { LegacyElementCacheSeeder::ensureLegacyTable(); $field = F::hyperField();