diff --git a/playground.php b/playground.php index 5200415..9112993 100644 --- a/playground.php +++ b/playground.php @@ -69,25 +69,23 @@ * Initialise Transfer Class */ $transfer = new Transfer( - $sourceSupabase, + $sourceFirebase, $destinationLocal ); $sourceFirebase->report(); -// $sourceSupabase->report(); - // /** // * Run Transfer // */ -// $transfer->run($sourceAppwrite->getSupportedResources(), -// function (array $resources) { -// } -// ); +$transfer->run($sourceFirebase->getSupportedResources(), + function (array $resources) { + } +); -// $report = []; +$report = []; -// $cache = $transfer->getCache()->getAll(); +$cache = $transfer->getCache()->getAll(); // foreach ($cache as $type => $resources) { // foreach ($resources as $resource) { diff --git a/src/Migration/Sources/Firebase.php b/src/Migration/Sources/Firebase.php index 63884fa..8b7c51f 100644 --- a/src/Migration/Sources/Firebase.php +++ b/src/Migration/Sources/Firebase.php @@ -152,6 +152,20 @@ public function report(array $resources = []): array protected function exportGroupAuth(int $batchSize, array $resources) { + // Check if Auth is enabled + try { + $this->call('GET', 'https://identitytoolkit.googleapis.com/v1/projects'); + } catch (\Exception $e) { + $message = json_decode($e->getMessage(), true); + + if (isset($message['error']['details']) && $message['error']['details'][1]['reason'] == 'SERVICE_DISABLED') { + // IdentityKit is disabled + return; + } + + throw $e; + } + if (in_array(Resource::TYPE_USER, $resources)) { $this->exportUsers($batchSize); } @@ -181,6 +195,10 @@ private function exportUsers(int $batchSize) 'Content-Type' => 'application/json', ], $request); + if (! isset($response['users'])) { + break; + } + $result = $response['users']; $nextPageToken = $response['nextPageToken'] ?? null; @@ -234,6 +252,20 @@ private function calculateUserType(array $providerData): array protected function exportGroupDatabases(int $batchSize, array $resources) { + // Check if Firestore is enabled + try { + $this->call('GET', 'https://firestore.googleapis.com/v1/projects/'.$this->projectID.'/databases'); + } catch (\Exception $e) { + $message = json_decode($e->getMessage(), true); + + if (isset($message['error']['details']) && $message['error']['details'][1]['reason'] == 'SERVICE_DISABLED') { + // Firestore is disabled + return; + } + + throw $e; + } + if (in_array(Resource::TYPE_DATABASE, $resources)) { $database = new Database('default', 'default'); $database->setOriginalId('(default)'); @@ -254,12 +286,29 @@ private function exportDB(int $batchSize, bool $pushDocuments, Database $databas while (true) { $collections = []; - $result = $this->call('POST', $baseURL.':listCollectionIds', [ - 'Content-Type' => 'application/json', - ], [ - 'pageSize' => $batchSize, - 'pageToken' => $nextPageToken, - ]); + try { + $result = $this->call('POST', $baseURL.':listCollectionIds', [ + 'Content-Type' => 'application/json', + ], [ + 'pageSize' => $batchSize, + 'pageToken' => $nextPageToken, + ]); + + if (! isset($result['collectionIds'])) { + break; + } + } catch (\Exception $e) { + if ($e->getCode() == 403) { + $errorMessage = new Collection($database, 'firestore', 'firestore'); + + $errorMessage->setStatus(Resource::STATUS_ERROR); + $errorMessage->setMessage($e->getMessage()); + + $this->cache->add($errorMessage); + } + + break; + } // Transfer Collections foreach ($result['collectionIds'] as $collection) { @@ -437,6 +486,24 @@ private function convertDocument(Collection $collection, array $document): Docum protected function exportGroupStorage(int $batchSize, array $resources) { + // Check if storage is enabled + try { + $this->call('GET', 'https://storage.googleapis.com/storage/v1/b', [], [ + 'project' => $this->projectID, + 'maxResults' => 1, + 'alt' => 'json', + ]); + } catch (\Exception $e) { + $message = json_decode($e->getMessage(), true); + + if (isset($message['error']['details']) && $message['error']['details'][1]['reason'] == 'SERVICE_DISABLED') { + // Storage is disabled + return; + } + + throw $e; + } + if (in_array(Resource::TYPE_BUCKET, $resources)) { $this->exportBuckets($batchSize); } @@ -464,6 +531,10 @@ private function exportBuckets(int $batchsize) break; } + if (! isset($result['items'])) { + break; + } + foreach ($result['items'] as $bucket) { $this->callback([new Bucket($bucket['id'], $bucket['name'], [], false)]); } diff --git a/src/Migration/Transfer.php b/src/Migration/Transfer.php index 6c19a34..ce59c64 100644 --- a/src/Migration/Transfer.php +++ b/src/Migration/Transfer.php @@ -88,19 +88,18 @@ public function getStatusCounters() ]; } - if ($this->source->previousReport) { - foreach ($this->source->previousReport as $resource => $data) { - if ($resource != 'size' && $resource != 'version') { - $status[$resource]['pending'] = $data; - } - } - } - foreach ($this->cache->getAll() as $resources) { foreach ($resources as $resource) { /** @var resource $resource */ $status[$resource->getName()][$resource->getStatus()]++; - $status[$resource->getName()]['pending']--; + } + } + + if ($this->source->previousReport) { + foreach ($this->source->previousReport as $resource => $data) { + if ($resource != 'size' && $resource != 'version' && isset($status[$resource])) { + $status[$resource]['pending'] = $data; + } } }