Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/migrations/LegacyContentRelationRebuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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);
}

Expand Down
13 changes: 13 additions & 0 deletions src/services/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
84 changes: 84 additions & 0 deletions tests/Services/UpgradeRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading