From ed735136dd10a88ab8601a9831e9cbcf3b6d6560 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 26 Nov 2024 13:04:12 +0200 Subject: [PATCH] Fix orphan schema attributes --- src/Migration/Destinations/Appwrite.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 866c16b..8bd2f1e 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -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