From aa1e7c706326542a050354635f4290df43f06443 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 07:03:39 +0100 Subject: [PATCH] Fix two greptile-flagged P1 bugs in createField MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Stale $table after dropAttributeForRecreate caused false LimitException $table is loaded once at the top of createField. After dropAttributeForRecreate runs (UpdateInPlace fallthrough), the meta doc + physical column are gone from the DB but the in-memory $table object still has the dropped entry in its 'attributes' array. utopia's checkAttribute clones $table and appends the new attribute, then asks the adapter for getCountOfAttributes — at the adapter ceiling this throws LimitException even though the net change is zero. Fix: re-fetch $table after dropAttributeForRecreate. 2. Partner meta-doc deleted from wrong table when relatedCollection changes dropAttributeForRecreate looked up the partner meta via $resource->getOptions() — source's options. But it runs precisely when RELATIONSHIP_STRUCTURAL_FIELDS differ (relatedCollection, twoWayKey, etc), so source's options point to the NEW partner table while dest's existing partner meta lives on the OLD partner table. The OLD partner meta was left orphaned forever. Fix: pass $existingAttr through to dropAttributeForRecreate and use its 'options' (dest's current state) for partner resolution. --- src/Migration/Destinations/Appwrite.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index d215452d8d..eae78d9971 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -837,7 +837,14 @@ class Appwrite extends Destination } if ($action === SchemaAction::UpdateInPlace) { - $this->dropAttributeForRecreate($database, $table, $resource, $dbForDatabases); + $this->dropAttributeForRecreate($database, $table, $resource, $dbForDatabases, $existingAttr); + // Re-fetch $table: the in-memory copy still holds the dropped + // attribute, so checkAttribute below would over-count and trip + // the adapter's LimitException at the column ceiling. + $table = $this->dbForProject->getDocument( + $this->databaseCollectionId($database), + $table->getId(), + ); } } @@ -1332,6 +1339,7 @@ class Appwrite extends Destination UtopiaDocument $table, Column|Attribute $resource, UtopiaDatabase $dbForDatabases, + ?UtopiaDocument $existingAttr = null, ): void { $collectionId = $this->tableCollectionId($database, $table); $attributeMetaId = $this->attributeIndexMetaId($database, $table, $resource->getKey()); @@ -1346,8 +1354,13 @@ class Appwrite extends Destination $this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $attributeMetaId); // deleteRelationship only touches utopia's _metadata; clear the Appwrite-level partner meta doc too. + // Use dest's options ($existingAttr): we drop precisely when relatedCollection/twoWayKey differ, + // so source's options point to the NEW partner; the OLD partner meta lives where dest currently has it. if ($isRelationship) { - $partner = $this->resolveTwoWayPartner($database, $resource->getOptions()); + $options = $existingAttr !== null && !$existingAttr->isEmpty() + ? (array) $existingAttr->getAttribute('options', []) + : $resource->getOptions(); + $partner = $this->resolveTwoWayPartner($database, $options); if ($partner !== null) { $this->bestEffort(fn () => $this->dbForProject->deleteDocument(self::META_ATTRIBUTES, $partner['partnerMetaId'])); }