Skip to content

Commit

Permalink
Merge pull request #14 from utopia-php/fix-attribute-document-import
Browse files Browse the repository at this point in the history
Fix attribute + document import
  • Loading branch information
abnegate authored Aug 31, 2023
2 parents af4233f + 8941752 commit 49a28ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Utopia\Migration\Destinations;

use Appwrite\AppwriteException;
use Appwrite\Client;
use Appwrite\InputFile;
use Appwrite\Services\Databases;
Expand Down Expand Up @@ -274,7 +275,15 @@ public function importDatabaseResource(Resource $resource): Resource
break;
case Resource::TYPE_ATTRIBUTE:
/** @var Attribute $resource */
$this->createAttribute($resource);
try {
$this->createAttribute($resource);
} catch (AppwriteException $e) {
if ($e->getType() === 'attribute_already_exists') {
$resource->setStatus(Resource::STATUS_SKIPPED, 'Attribute has been already created');
} else {
throw $e;
}
}
break;
case Resource::TYPE_DOCUMENT:
/** @var Document $resource */
Expand Down
7 changes: 4 additions & 3 deletions src/Migration/Sources/Firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private function exportCollection(Collection $collection, int $batchSize, bool $

$nextPageToken = null;

$knownSchema = [];
$documentSchema = [];

// Transfer Documents and Calculate Schemas
while (true) {
Expand All @@ -410,8 +410,6 @@ private function exportCollection(Collection $collection, int $batchSize, bool $
break;
}

// Calculate Schema and handle subcollections
$documentSchema = [];
foreach ($result['documents'] as $document) {
if (! isset($document['fields'])) {
continue; //TODO: Transfer Empty Documents
Expand Down Expand Up @@ -480,6 +478,9 @@ private function convertDocument(Collection $collection, array $document): Docum

$documentId = explode('/', $document['name']);
$documentId = end($documentId);
// Strip non-alphanumeric except underscore and hyphen
$documentId = preg_replace("/[^A-Za-z0-9\_\-]/", '', $documentId);
$documentId = strtolower($documentId);

return new Document($documentId, $collection->getDatabase(), $collection, $data, []);
}
Expand Down

0 comments on commit 49a28ad

Please sign in to comment.