Skip to content

Commit 45bd831

Browse files
Merge pull request #15 from utopia-php/fix-firebase-duplicate-attributes
Fix Duplicate Attributes in Firebase
2 parents 49a28ad + 6444285 commit 45bd831

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/Migration/Destinations/Local.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected function import(array $resources, callable $callback): void
104104
case Resource::TYPE_DEPLOYMENT:
105105
/** @var Deployment $resource */
106106
if ($resource->getStart() === 0) {
107-
$this->data[$resource->getGroup()][$resource->getName()][$resource->getInternalId()] = $resource->asArray();
107+
$this->data[$resource->getGroup()][$resource->getName()][] = $resource->asArray();
108108
}
109109

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

src/Migration/Sources/Firebase.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private function exportUsers(int $batchSize)
207207
$user['localId'] ?? '',
208208
$user['email'] ?? '',
209209
$user['displayName'] ?? $user['email'] ?? '',
210-
new Hash($user['passwordHash'] ?? '', $user['salt'] ?? '', Hash::ALGORITHM_SCRYPT_MODIFIED, $hashConfig['saltSeparator'], $hashConfig['signerKey']),
210+
new Hash($user['passwordHash'] ?? '', $user['salt'] ?? '', Hash::ALGORITHM_SCRYPT_MODIFIED, $hashConfig['saltSeparator'] ?? '', $hashConfig['signerKey'] ?? ''),
211211
$user['phoneNumber'] ?? '',
212212
$this->calculateUserType($user['providerUserInfo'] ?? []),
213213
'',
@@ -394,6 +394,7 @@ private function exportCollection(Collection $collection, int $batchSize, bool $
394394
$nextPageToken = null;
395395

396396
$documentSchema = [];
397+
$createdSchema = [];
397398

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

427428
// Transfer Documents
428429
if ($transferDocuments) {
429-
$this->callback(array_values($documentSchema));
430+
$cachedAtrributes = $this->cache->get(Attribute::getName());
431+
432+
$attributesToCreate = $documentSchema;
433+
434+
foreach ($documentSchema as $key => $attribute) {
435+
foreach ($cachedAtrributes as $cachedAttribute) {
436+
/** @var Attribute $cachedAttribute */
437+
if ($cachedAttribute->getKey() == $attribute->getKey() && $cachedAttribute->getCollection()->getId() == $attribute->getCollection()->getId()) {
438+
unset($attributesToCreate[$key]);
439+
}
440+
}
441+
}
442+
443+
if (count($attributesToCreate) > 0) {
444+
$this->callback(array_values($attributesToCreate));
445+
}
446+
430447
$this->callback($documents);
431448
}
432449

0 commit comments

Comments
 (0)