From 64442855e83abf65c95566b5f0302485d176e5bf Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 31 Aug 2023 18:07:26 +0100 Subject: [PATCH] Fixes two issues 1. A potential type error due to a null value 2. Cached Attributes will be checked for duplicates before creating them --- src/Migration/Destinations/Local.php | 4 ++-- src/Migration/Sources/Firebase.php | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Migration/Destinations/Local.php b/src/Migration/Destinations/Local.php index 11156d89..457ea904 100644 --- a/src/Migration/Destinations/Local.php +++ b/src/Migration/Destinations/Local.php @@ -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); @@ -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; } diff --git a/src/Migration/Sources/Firebase.php b/src/Migration/Sources/Firebase.php index c7a9b9ec..718952e8 100644 --- a/src/Migration/Sources/Firebase.php +++ b/src/Migration/Sources/Firebase.php @@ -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'] ?? []), '', @@ -394,6 +394,7 @@ private function exportCollection(Collection $collection, int $batchSize, bool $ $nextPageToken = null; $documentSchema = []; + $createdSchema = []; // Transfer Documents and Calculate Schemas while (true) { @@ -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); }