Skip to content

Commit c76de9a

Browse files
committed
Two-way relationships: sync partner-side onDelete on UpdateInPlace
utopia's updateRelationship updates the physical constraint on both sides, but the Appwrite-level attributes meta doc on the partner side keeps the old options.onDelete value. Reads from the related collection's attribute list (Appwrite API, validators, subsequent re-migration pre-checks) see stale data. Mirrors the partner-side cleanup pattern already used in dropAttributeForRecreate. One-way unaffected (onDelete change on one-way already routes to DropAndRecreate).
1 parent a36d95f commit c76de9a

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/Migration/Destinations/Appwrite.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,9 +1444,56 @@ private function updateRelationshipInPlace(
14441444
]));
14451445

14461446
$this->purgeTableCaches($database, $table, $dbForDatabases);
1447+
1448+
// utopia's updateRelationship synced the physical constraint on both sides but
1449+
// not the Appwrite-level partner meta doc — refresh it so reads from the related
1450+
// collection don't see stale onDelete.
1451+
if ($isTwoWay) {
1452+
$this->refreshTwoWayPartnerOnDelete($database, $destOptions, $sourceOptions, $updatedAt, $dbForDatabases);
1453+
}
1454+
14471455
return true;
14481456
}
14491457

1458+
private function refreshTwoWayPartnerOnDelete(
1459+
UtopiaDocument $database,
1460+
array $destOptions,
1461+
array $sourceOptions,
1462+
string $updatedAt,
1463+
UtopiaDatabase $dbForDatabases,
1464+
): void {
1465+
$relatedTableId = (string) ($destOptions['relatedCollection'] ?? '');
1466+
$partnerKey = (string) ($destOptions['twoWayKey'] ?? '');
1467+
if ($relatedTableId === '' || $partnerKey === '') {
1468+
return;
1469+
}
1470+
1471+
$relatedTable = $this->dbForProject->getDocument(
1472+
$this->databaseCollectionId($database),
1473+
$relatedTableId,
1474+
);
1475+
if ($relatedTable->isEmpty()) {
1476+
return;
1477+
}
1478+
1479+
$partnerMeta = $this->dbForProject->getDocument(
1480+
'attributes',
1481+
$this->attributeIndexMetaId($database, $relatedTable, $partnerKey),
1482+
);
1483+
if ($partnerMeta->isEmpty()) {
1484+
return;
1485+
}
1486+
1487+
$partnerOptions = $partnerMeta->getAttribute('options', []);
1488+
$this->dbForProject->updateDocument('attributes', $partnerMeta->getId(), new UtopiaDocument([
1489+
'options' => array_merge($partnerOptions, [
1490+
'onDelete' => $sourceOptions['onDelete'] ?? $partnerOptions['onDelete'] ?? null,
1491+
]),
1492+
'$updatedAt' => $updatedAt,
1493+
]));
1494+
$this->purgeTableCaches($database, $relatedTable, $dbForDatabases);
1495+
}
1496+
14501497
/**
14511498
* `lengths` is intentionally not compared: it's derived per-column from
14521499
* the adapter's index validator and not settable on the Index resource.

0 commit comments

Comments
 (0)