Skip to content

Commit

Permalink
Merge pull request #34 from utopia-php/fix-improve-migration-exceptions
Browse files Browse the repository at this point in the history
 Improve Migration CLI and Exception classes
  • Loading branch information
abnegate authored Jun 13, 2024
2 parents 7bbab0a + 65bd29d commit 4c6f44d
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 45 deletions.
39 changes: 31 additions & 8 deletions bin/MigrationCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class MigrationCLI
{
protected Transfer $transfer;

protected Appwrite $source;

protected DestinationsAppwrite $destination;

/**
* Prints the current status of migrations as a table after wiping the screen
*/
Expand All @@ -24,10 +28,29 @@ public function drawFrame()
$statusCounters = $this->transfer->getStatusCounters();

$mask = "| %15.15s | %-7.7s | %10.10s | %7.7s | %7.7s | %8.8s |\n";
printf($mask, 'Resource', 'Pending', 'Processing', 'Skipped', 'Warning', 'Success');
printf($mask, '-------------', '-------------', '-------------', '-------------', '-------------', '-------------');
printf($mask, 'Resource', 'Pending', 'Processing', 'Skipped', 'Warning', 'Error', 'Success');
printf($mask, '-------------', '-------------', '-------------', '-------------', '-------------', '-------------', '-------------');
foreach ($statusCounters as $resource => $data) {
printf($mask, $resource, $data['pending'], $data['processing'], $data['skip'], $data['warning'], $data['success']);
printf($mask, $resource, $data['pending'], $data['processing'], $data['skip'], $data['warning'], $data['error'], $data['success']);
}

// Render Errors
$destErrors = $this->destination->getErrors();
if (! empty($destErrors)) {
echo "\n\nDestination Errors:\n";
foreach ($destErrors as $error) {
/** @var Utopia\Migration\Exception $error */
echo $error->getResourceName().'['.$error->getResourceId().'] - '.$error->getMessage()."\n";
}
}

$sourceErrors = $this->source->getErrors();
if (! empty($sourceErrors)) {
echo "\n\nSource Errors:\n";
foreach ($sourceErrors as $error) {
/** @var Utopia\Migration\Exception $error */
echo $error->getResourceType().'['.$error->getResourceId().'] - '.$error->getMessage()."\n";
}
}
}

Expand All @@ -39,15 +62,15 @@ public function start()
/**
* Initialise All Source Adapters
*/
$source = new Appwrite(
$this->source = new Appwrite(
$_ENV['SOURCE_APPWRITE_TEST_PROJECT'],
$_ENV['SOURCE_APPWRITE_TEST_ENDPOINT'],
$_ENV['SOURCE_APPWRITE_TEST_KEY']
);

// $source->report();

$destination = new DestinationsAppwrite(
$this->destination = new DestinationsAppwrite(
$_ENV['DESTINATION_APPWRITE_TEST_PROJECT'],
$_ENV['DESTINATION_APPWRITE_TEST_ENDPOINT'],
$_ENV['DESTINATION_APPWRITE_TEST_KEY']
Expand All @@ -57,15 +80,15 @@ public function start()
* Initialise Transfer Class
*/
$this->transfer = new Transfer(
$source,
$destination
$this->source,
$this->destination
);

/**
* Run Transfer
*/
$this->transfer->run(
$source->getSupportedResources(),
$this->source->getSupportedResources(),
function (array $resources) {
$this->drawFrame();
}
Expand Down
6 changes: 4 additions & 2 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,12 @@ protected function import(array $resources, callable $callback): void
} else {
$resource->setStatus(Resource::STATUS_ERROR, $e->getMessage());
$this->addError(new Exception(
resourceType: $resource->getGroup(),
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: $e->getMessage(),
code: $e->getCode()
code: $e->getCode(),
previous: $e
));
}

Expand Down
18 changes: 13 additions & 5 deletions src/Migration/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@

class Exception extends \Exception
{
public string $resourceType;
public string $resourceName;

public string $resourceGroup;

public string $resourceId;

public function __construct(string $resourceType, string $message, int $code = 0, ?\Throwable $previous = null, string $resourceId = '')
public function __construct(string $resourceName, string $resourceGroup, string $message, int $code = 0, ?\Throwable $previous = null, string $resourceId = '')
{
$this->resourceName = $resourceName;
$this->resourceId = $resourceId;
$this->resourceType = $resourceType;
$this->resourceGroup = $resourceGroup;

parent::__construct($message, $code, $previous);
}

public function getResourceType(): string
public function getResourceName(): string
{
return $this->resourceName;
}

public function getResourceGroup(): string
{
return $this->resourceType;
return $this->resourceGroup;
}

public function getResourceId(): string
Expand Down
68 changes: 54 additions & 14 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ protected function exportGroupAuth(int $batchSize, array $resources)
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_USER,
$e->getMessage()
Transfer::GROUP_AUTH,
$e->getMessage(),
$e->getCode(),
$e
));
}

Expand All @@ -292,7 +295,10 @@ protected function exportGroupAuth(int $batchSize, array $resources)
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_TEAM,
$e->getMessage()
Transfer::GROUP_AUTH,
$e->getMessage(),
$e->getCode(),
$e
));
}

Expand All @@ -303,7 +309,10 @@ protected function exportGroupAuth(int $batchSize, array $resources)
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_MEMBERSHIP,
$e->getMessage()
Transfer::GROUP_AUTH,
$e->getMessage(),
$e->getCode(),
$e
));
}
}
Expand Down Expand Up @@ -459,7 +468,10 @@ protected function exportGroupDatabases(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_DATABASE,
$e->getMessage()
Transfer::GROUP_DATABASES,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand All @@ -472,7 +484,10 @@ protected function exportGroupDatabases(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_COLLECTION,
$e->getMessage()
Transfer::GROUP_DATABASES,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand All @@ -485,7 +500,10 @@ protected function exportGroupDatabases(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_ATTRIBUTE,
$e->getMessage()
Transfer::GROUP_DATABASES,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand All @@ -498,7 +516,10 @@ protected function exportGroupDatabases(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_INDEX,
$e->getMessage()
Transfer::GROUP_DATABASES,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand All @@ -511,7 +532,10 @@ protected function exportGroupDatabases(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_DOCUMENT,
$e->getMessage()
Transfer::GROUP_DATABASES,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand Down Expand Up @@ -952,7 +976,10 @@ protected function exportGroupStorage(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_BUCKET,
$e->getMessage()
Transfer::GROUP_STORAGE,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand All @@ -965,7 +992,10 @@ protected function exportGroupStorage(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_FILE,
$e->getMessage()
Transfer::GROUP_STORAGE,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand All @@ -978,7 +1008,10 @@ protected function exportGroupStorage(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_BUCKET,
$e->getMessage()
Transfer::GROUP_STORAGE,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand Down Expand Up @@ -1052,7 +1085,8 @@ private function exportFiles(int $batchSize)
));
} catch (\Throwable $e) {
$this->addError(new Exception(
resourceType: Resource::TYPE_FILE,
resourceName: Resource::TYPE_FILE,
resourceGroup: Transfer::GROUP_STORAGE,
message: $e->getMessage(),
code: $e->getCode(),
resourceId: $file['$id']
Expand Down Expand Up @@ -1116,7 +1150,10 @@ protected function exportGroupFunctions(int $batchSize, array $resources)
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_FUNCTION,
$e->getMessage()
Transfer::GROUP_FUNCTIONS,
$e->getMessage(),
$e->getCode(),
$e
));
}

Expand All @@ -1127,7 +1164,10 @@ protected function exportGroupFunctions(int $batchSize, array $resources)
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_DEPLOYMENT,
$e->getMessage()
Transfer::GROUP_FUNCTIONS,
$e->getMessage(),
$e->getCode(),
$e
));
}
}
Expand Down
25 changes: 20 additions & 5 deletions src/Migration/Sources/Firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ protected function exportGroupAuth(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_USER,
$e->getMessage()
Transfer::GROUP_AUTH,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand Down Expand Up @@ -258,7 +261,10 @@ protected function exportGroupDatabases(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_DATABASE,
$e->getMessage()
Transfer::GROUP_DATABASES,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand All @@ -271,7 +277,10 @@ protected function exportGroupDatabases(int $batchSize, array $resources)
$this->addError(
new Exception(
Resource::TYPE_COLLECTION,
$e->getMessage()
Transfer::GROUP_DATABASES,
$e->getMessage(),
$e->getCode(),
$e
)
);
}
Expand Down Expand Up @@ -529,7 +538,10 @@ protected function exportGroupStorage(int $batchSize, array $resources)
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_BUCKET,
$e->getMessage()
Transfer::GROUP_STORAGE,
$e->getMessage(),
$e->getCode(),
$e
));
}

Expand All @@ -540,7 +552,10 @@ protected function exportGroupStorage(int $batchSize, array $resources)
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_FILE,
$e->getMessage()
Transfer::GROUP_STORAGE,
$e->getMessage(),
$e->getCode(),
$e
));
}

Expand Down
Loading

0 comments on commit 4c6f44d

Please sign in to comment.