From cb7baddb4adb39eb4a5bbf273453b52caf468be8 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 2 Jul 2025 11:37:00 -0400 Subject: [PATCH 1/3] Download to local --- src/Migration/Sources/CSV.php | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Migration/Sources/CSV.php b/src/Migration/Sources/CSV.php index ee7ce5de..2811ab45 100644 --- a/src/Migration/Sources/CSV.php +++ b/src/Migration/Sources/CSV.php @@ -4,7 +4,7 @@ use Utopia\Database\Database as UtopiaDatabase; use Utopia\Migration\Exception; -use Utopia\Migration\Resource; +use Utopia\Migration\Resource as UtopiaResource; use Utopia\Migration\Resources\Database\Attribute; use Utopia\Migration\Resources\Database\Collection; use Utopia\Migration\Resources\Database\Database; @@ -15,6 +15,7 @@ use Utopia\Migration\Sources\Appwrite\Reader\Database as DatabaseReader; use Utopia\Migration\Transfer; use Utopia\Storage\Device; +use Utopia\Storage\Storage; class CSV extends Source { @@ -49,7 +50,7 @@ public static function getName(): string public static function getSupportedResources(): array { return [ - Resource::TYPE_DOCUMENT, + UtopiaResource::TYPE_DOCUMENT, ]; } @@ -68,7 +69,7 @@ public function report(array $resources = []): array $file->seek(PHP_INT_MAX); $rowCount = max(0, $file->key()); - $report[Resource::TYPE_DOCUMENT] = $rowCount; + $report[UtopiaResource::TYPE_DOCUMENT] = $rowCount; return $report; } @@ -84,13 +85,13 @@ protected function exportGroupAuth(int $batchSize, array $resources): void protected function exportGroupDatabases(int $batchSize, array $resources): void { try { - if (\in_array(Resource::TYPE_DOCUMENT, $resources)) { + if (\in_array(UtopiaResource::TYPE_DOCUMENT, $resources)) { $this->exportDocuments($batchSize); } } catch (\Throwable $e) { $this->addError( new Exception( - Resource::TYPE_DOCUMENT, + UtopiaResource::TYPE_DOCUMENT, Transfer::GROUP_DATABASES, message: $e->getMessage(), code: $e->getCode(), @@ -301,21 +302,33 @@ protected function exportGroupFunctions(int $batchSize, array $resources): void throw new \Exception('Not Implemented'); } - private function withCsvStream(callable $fn): void + /** + * @param callable(resource $stream): void $callback + * @return void + */ + private function withCsvStream(callable $callback): void { - if (! $this->device->exists($this->filePath)) { + if (!$this->device->exists($this->filePath)) { return; } - $stream = fopen($this->filePath, 'r'); - if (! $stream) { + if ($this->device->getType() !== Storage::DEVICE_LOCAL) { + $this->device->transfer( + $this->filePath, + $this->filePath, + new Device\Local('/'), + ); + } + + $stream = \fopen($this->filePath, 'r'); + if (!$stream) { return; } try { - $fn($stream); + $callback($stream); } finally { - fclose($stream); + \fclose($stream); } } From 46c2c381d36165087433d57b5b52fcff71ecb08f Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 2 Jul 2025 12:46:55 -0400 Subject: [PATCH 2/3] Catch transfer failure --- src/Migration/Sources/CSV.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Migration/Sources/CSV.php b/src/Migration/Sources/CSV.php index 2811ab45..041fcf6a 100644 --- a/src/Migration/Sources/CSV.php +++ b/src/Migration/Sources/CSV.php @@ -305,6 +305,7 @@ protected function exportGroupFunctions(int $batchSize, array $resources): void /** * @param callable(resource $stream): void $callback * @return void + * @throws \Exception */ private function withCsvStream(callable $callback): void { @@ -313,11 +314,19 @@ private function withCsvStream(callable $callback): void } if ($this->device->getType() !== Storage::DEVICE_LOCAL) { - $this->device->transfer( - $this->filePath, - $this->filePath, - new Device\Local('/'), - ); + try { + $success = $this->device->transfer( + $this->filePath, + $this->filePath, + new Device\Local('/'), + ); + } catch (\Exception) { + $success = false; + } + + if (!$success) { + throw new \Exception('Failed to transfer CSV file from device to local storage.'); + } } $stream = \fopen($this->filePath, 'r'); From b7923db18cff26c5f740c5ef76bb836a73944ef9 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 2 Jul 2025 12:49:10 -0400 Subject: [PATCH 3/3] Pass previous --- src/Migration/Sources/CSV.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Migration/Sources/CSV.php b/src/Migration/Sources/CSV.php index 041fcf6a..721b84ae 100644 --- a/src/Migration/Sources/CSV.php +++ b/src/Migration/Sources/CSV.php @@ -320,12 +320,12 @@ private function withCsvStream(callable $callback): void $this->filePath, new Device\Local('/'), ); - } catch (\Exception) { + } catch (\Exception $e) { $success = false; } if (!$success) { - throw new \Exception('Failed to transfer CSV file from device to local storage.'); + throw new \Exception('Failed to transfer CSV file from device to local storage.', previous: $e ?? null); } }