Fix two greptile-flagged P1 bugs in createField

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.
This commit is contained in:
Prem Palanisamy committed 2026-04-28 07:03:39 +01:00
1 parent 6ec2c45a40
commit aa1e7c7063
1 file changed
+15 -2
+15 -2
View File
@@ -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']));
}