Skip to content

Commit

Permalink
Merge pull request #15 from utopia-php/fix-firebase-duplicate-attributes
Browse files Browse the repository at this point in the history
Fix Duplicate Attributes in Firebase
  • Loading branch information
christyjacob4 authored Sep 6, 2023
2 parents 49a28ad + 6444285 commit 45bd831
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Migration/Destinations/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function import(array $resources, callable $callback): void
case Resource::TYPE_DEPLOYMENT:
/** @var Deployment $resource */
if ($resource->getStart() === 0) {
$this->data[$resource->getGroup()][$resource->getName()][$resource->getInternalId()] = $resource->asArray();
$this->data[$resource->getGroup()][$resource->getName()][] = $resource->asArray();
}

file_put_contents($this->path.'deployments/'.$resource->getId().'.tar.gz', $resource->getData(), FILE_APPEND);
Expand Down Expand Up @@ -135,7 +135,7 @@ protected function import(array $resources, callable $callback): void
$resource->setData('');
break;
default:
$this->data[$resource->getGroup()][$resource->getName()][$resource->getInternalId()] = $resource->asArray();
$this->data[$resource->getGroup()][$resource->getName()][] = $resource->asArray();
break;
}

Expand Down
21 changes: 19 additions & 2 deletions src/Migration/Sources/Firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private function exportUsers(int $batchSize)
$user['localId'] ?? '',
$user['email'] ?? '',
$user['displayName'] ?? $user['email'] ?? '',
new Hash($user['passwordHash'] ?? '', $user['salt'] ?? '', Hash::ALGORITHM_SCRYPT_MODIFIED, $hashConfig['saltSeparator'], $hashConfig['signerKey']),
new Hash($user['passwordHash'] ?? '', $user['salt'] ?? '', Hash::ALGORITHM_SCRYPT_MODIFIED, $hashConfig['saltSeparator'] ?? '', $hashConfig['signerKey'] ?? ''),
$user['phoneNumber'] ?? '',
$this->calculateUserType($user['providerUserInfo'] ?? []),
'',
Expand Down Expand Up @@ -394,6 +394,7 @@ private function exportCollection(Collection $collection, int $batchSize, bool $
$nextPageToken = null;

$documentSchema = [];
$createdSchema = [];

// Transfer Documents and Calculate Schemas
while (true) {
Expand Down Expand Up @@ -426,7 +427,23 @@ private function exportCollection(Collection $collection, int $batchSize, bool $

// Transfer Documents
if ($transferDocuments) {
$this->callback(array_values($documentSchema));
$cachedAtrributes = $this->cache->get(Attribute::getName());

$attributesToCreate = $documentSchema;

foreach ($documentSchema as $key => $attribute) {
foreach ($cachedAtrributes as $cachedAttribute) {
/** @var Attribute $cachedAttribute */
if ($cachedAttribute->getKey() == $attribute->getKey() && $cachedAttribute->getCollection()->getId() == $attribute->getCollection()->getId()) {
unset($attributesToCreate[$key]);
}
}
}

if (count($attributesToCreate) > 0) {
$this->callback(array_values($attributesToCreate));
}

$this->callback($documents);
}

Expand Down

0 comments on commit 45bd831

Please sign in to comment.