Skip to content

Commit

Permalink
Merge pull request #28 from utopia-php/feat-fix-source-errors
Browse files Browse the repository at this point in the history
Add improved error handling for source exceptions
  • Loading branch information
christyjacob4 authored May 1, 2024
2 parents a72f27b + dd8aaf8 commit ae3cfe9
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 63 deletions.
161 changes: 133 additions & 28 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,37 @@ public function report(array $resources = []): array
*/
protected function exportGroupAuth(int $batchSize, array $resources)
{
if (in_array(Resource::TYPE_USER, $resources)) {
$this->exportUsers($batchSize);
try {
if (in_array(Resource::TYPE_USER, $resources)) {
$this->exportUsers($batchSize);
}
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_USER,
$e->getMessage()
));
}

if (in_array(Resource::TYPE_TEAM, $resources)) {
$this->exportTeams($batchSize);
try {
if (in_array(Resource::TYPE_TEAM, $resources)) {
$this->exportTeams($batchSize);
}
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_TEAM,
$e->getMessage()
));
}

if (in_array(Resource::TYPE_MEMBERSHIP, $resources)) {
$this->exportMemberships($batchSize);
try {
if (in_array(Resource::TYPE_MEMBERSHIP, $resources)) {
$this->exportMemberships($batchSize);
}
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_MEMBERSHIP,
$e->getMessage()
));
}
}

Expand All @@ -301,7 +322,6 @@ private function exportUsers(int $batchSize)
}

$response = $usersClient->list($queries);

if ($response['total'] == 0) {
break;
}
Expand Down Expand Up @@ -349,7 +369,6 @@ private function exportTeams(int $batchSize)
}

$response = $teamsClient->list($queries);

if ($response['total'] == 0) {
break;
}
Expand Down Expand Up @@ -431,24 +450,69 @@ private function exportMemberships(int $batchSize)

protected function exportGroupDatabases(int $batchSize, array $resources)
{
if (in_array(Resource::TYPE_DATABASE, $resources)) {
$this->exportDatabases($batchSize);
try {
if (in_array(Resource::TYPE_DATABASE, $resources)) {
$this->exportDatabases($batchSize);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_DATABASE,
$e->getMessage()
)
);
}

if (in_array(Resource::TYPE_COLLECTION, $resources)) {
$this->exportCollections($batchSize);
try {
if (in_array(Resource::TYPE_COLLECTION, $resources)) {
$this->exportCollections($batchSize);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_COLLECTION,
$e->getMessage()
)
);
}

if (in_array(Resource::TYPE_ATTRIBUTE, $resources)) {
$this->exportAttributes($batchSize);
try {
if (in_array(Resource::TYPE_ATTRIBUTE, $resources)) {
$this->exportAttributes($batchSize);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_ATTRIBUTE,
$e->getMessage()
)
);
}

if (in_array(Resource::TYPE_INDEX, $resources)) {
$this->exportIndexes($batchSize);
try {
if (in_array(Resource::TYPE_INDEX, $resources)) {
$this->exportIndexes($batchSize);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_INDEX,
$e->getMessage()
)
);
}

if (in_array(Resource::TYPE_DOCUMENT, $resources)) {
$this->exportDocuments($batchSize);
try {
if (in_array(Resource::TYPE_DOCUMENT, $resources)) {
$this->exportDocuments($batchSize);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_DOCUMENT,
$e->getMessage()
)
);
}
}

Expand Down Expand Up @@ -898,16 +962,43 @@ private function calculateTypes(array $user): array

protected function exportGroupStorage(int $batchSize, array $resources)
{
if (in_array(Resource::TYPE_BUCKET, $resources)) {
$this->exportBuckets($batchSize, false);
try {
if (in_array(Resource::TYPE_BUCKET, $resources)) {
$this->exportBuckets($batchSize, false);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_BUCKET,
$e->getMessage()
)
);
}

if (in_array(Resource::TYPE_FILE, $resources)) {
$this->exportFiles($batchSize);
try {
if (in_array(Resource::TYPE_FILE, $resources)) {
$this->exportFiles($batchSize);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_FILE,
$e->getMessage()
)
);
}

if (in_array(Resource::TYPE_BUCKET, $resources)) {
$this->exportBuckets($batchSize, true);
try {
if (in_array(Resource::TYPE_BUCKET, $resources)) {
$this->exportBuckets($batchSize, true);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_BUCKET,
$e->getMessage()
)
);
}
}

Expand Down Expand Up @@ -1036,12 +1127,26 @@ private function exportFileData(File $file)

protected function exportGroupFunctions(int $batchSize, array $resources)
{
if (in_array(Resource::TYPE_FUNCTION, $resources)) {
$this->exportFunctions($batchSize);
try {
if (in_array(Resource::TYPE_FUNCTION, $resources)) {
$this->exportFunctions($batchSize);
}
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_FUNCTION,
$e->getMessage()
));
}

if (in_array(Resource::TYPE_DEPLOYMENT, $resources)) {
$this->exportDeployments($batchSize, true);
try {
if (in_array(Resource::TYPE_DEPLOYMENT, $resources)) {
$this->exportDeployments($batchSize, true);
}
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_DEPLOYMENT,
$e->getMessage()
));
}
}

Expand Down
67 changes: 55 additions & 12 deletions src/Migration/Sources/Firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Utopia\Migration\Sources;

use Utopia\Migration\Exception;
use Utopia\Migration\Resource;
use Utopia\Migration\Resources\Auth\Hash;
use Utopia\Migration\Resources\Auth\User;
Expand Down Expand Up @@ -163,8 +164,17 @@ protected function exportGroupAuth(int $batchSize, array $resources)
throw $e;
}

if (in_array(Resource::TYPE_USER, $resources)) {
$this->exportUsers($batchSize);
try {
if (in_array(Resource::TYPE_USER, $resources)) {
$this->exportUsers($batchSize);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_USER,
$e->getMessage()
)
);
}
}

Expand Down Expand Up @@ -264,14 +274,32 @@ protected function exportGroupDatabases(int $batchSize, array $resources)
throw $e;
}

if (in_array(Resource::TYPE_DATABASE, $resources)) {
$database = new Database('default', 'default');
$database->setOriginalId('(default)');
$this->callback([$database]);
try {
if (in_array(Resource::TYPE_DATABASE, $resources)) {
$database = new Database('default', 'default');
$database->setOriginalId('(default)');
$this->callback([$database]);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_DATABASE,
$e->getMessage()
)
);
}

if (in_array(Resource::TYPE_COLLECTION, $resources)) {
$this->exportDB($batchSize, in_array(Resource::TYPE_DOCUMENT, $resources), $database);
try {
if (in_array(Resource::TYPE_COLLECTION, $resources)) {
$this->exportDB($batchSize, in_array(Resource::TYPE_DOCUMENT, $resources), $database);
}
} catch (\Throwable $e) {
$this->addError(
new Exception(
Resource::TYPE_COLLECTION,
$e->getMessage()
)
);
}
}

Expand Down Expand Up @@ -520,13 +548,28 @@ protected function exportGroupStorage(int $batchSize, array $resources)
throw $e;
}

if (in_array(Resource::TYPE_BUCKET, $resources)) {
$this->exportBuckets($batchSize);
try {
if (in_array(Resource::TYPE_BUCKET, $resources)) {
$this->exportBuckets($batchSize);
}
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_BUCKET,
$e->getMessage()
));
}

if (in_array(Resource::TYPE_FILE, $resources)) {
$this->exportFiles($batchSize);
try {
if (in_array(Resource::TYPE_FILE, $resources)) {
$this->exportFiles($batchSize);
}
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_FILE,
$e->getMessage()
));
}

}

private function exportBuckets(int $batchsize)
Expand Down
Loading

0 comments on commit ae3cfe9

Please sign in to comment.