Skip to content

Commit

Permalink
Fix orphan schema attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
fogelito committed Nov 26, 2024
1 parent 9a8c905 commit ed73513
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,31 @@ protected function createDocument(Document $resource, bool $isLast): bool
$databaseInternalId = $database->getInternalId();
$collectionInternalId = $collection->getInternalId();

/**
* This is in case an attribute was deleted from Appwrite attributes collection but was not deleted from the table
* When creating an archive we select * which will include orphan attribute from the schema
*/
foreach ($this->documentBuffer as $document) {
foreach ($document as $key => $value) {
if (\str_starts_with($key, '$')) {
continue;
}

/** @var $attribute \Utopia\Database\Document */
$found = false;
foreach ($collection->getAttribute('attributes', []) as $attribute) {
if ($attribute->getAttribute('key') == $key) {
$found = true;
break;
}
}

if (! $found) {
$document->removeAttribute($key);
}
}
}

$this->database->skipRelationshipsExistCheck(fn () => $this->database->createDocuments(
'database_' . $databaseInternalId . '_collection_' . $collectionInternalId,
$this->documentBuffer
Expand Down

0 comments on commit ed73513

Please sign in to comment.