Skip to content
30 changes: 25 additions & 5 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,17 @@ public function importFunctionResource(Resource $resource): Resource
*/
private function importDeployment(Deployment $deployment): Resource
{
$functionId = $deployment->getFunction()->getId();
$function = $deployment->getFunction();

// Deployment API always creates a new deployment, so unlike other resources
// there's no duplicate detection. Skip if the parent function wasn't imported successfully.
if ($function->getStatus() !== Resource::STATUS_SUCCESS) {
$deployment->setStatus(Resource::STATUS_SKIPPED, 'Parent function "' . $function->getId() . '" failed to import');

return $deployment;
}

$functionId = $function->getId();

$response = null;

Expand Down Expand Up @@ -1508,7 +1518,7 @@ private function importDeployment(Deployment $deployment): Resource
[
'functionId' => $functionId,
'code' => new \CURLFile('data://application/gzip;base64,' . base64_encode($deployment->getData()), 'application/gzip', 'deployment.tar.gz'),
'activate' => $deployment->getActivated(),
'activate' => $deployment->getActivated() ? 'true' : 'false',
'entrypoint' => $deployment->getEntrypoint(),
]
);
Expand Down Expand Up @@ -1674,7 +1684,17 @@ public function importSiteResource(Resource $resource): Resource
*/
private function importSiteDeployment(SiteDeployment $deployment): Resource
{
$siteId = $deployment->getSite()->getId();
$site = $deployment->getSite();

// Deployment API always creates a new deployment, so unlike other resources
// there's no duplicate detection. Skip if the parent site wasn't imported successfully.
if ($site->getStatus() !== Resource::STATUS_SUCCESS) {
$deployment->setStatus(Resource::STATUS_SKIPPED, 'Parent site "' . $site->getId() . '" failed to import');

return $deployment;
}

$siteId = $site->getId();

if ($deployment->getSize() <= Transfer::STORAGE_MAX_CHUNK_SIZE) {
$response = $this->client->call(
Expand All @@ -1686,7 +1706,7 @@ private function importSiteDeployment(SiteDeployment $deployment): Resource
[
'siteId' => $siteId,
'code' => new \CURLFile('data://application/gzip;base64,' . base64_encode($deployment->getData()), 'application/gzip', 'deployment.tar.gz'),
'activate' => $deployment->getActivated(),
'activate' => $deployment->getActivated() ? 'true' : 'false',
]
);

Expand All @@ -1710,7 +1730,7 @@ private function importSiteDeployment(SiteDeployment $deployment): Resource
[
'siteId' => $siteId,
'code' => new \CURLFile('data://application/gzip;base64,' . base64_encode($deployment->getData()), 'application/gzip', 'deployment.tar.gz'),
'activate' => $deployment->getActivated(),
'activate' => $deployment->getActivated() ? 'true' : 'false',
]
);

Expand Down
28 changes: 18 additions & 10 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1518,9 +1518,8 @@ protected function exportGroupFunctions(int $batchSize, array $resources): void
}

try {
if (\in_array(Resource::TYPE_DEPLOYMENT, $resources)) {
$this->exportDeployments($batchSize, true);
}
$exportOnlyActive = !\in_array(Resource::TYPE_DEPLOYMENT, $resources);
$this->exportDeployments($batchSize, $exportOnlyActive);
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_DEPLOYMENT,
Expand Down Expand Up @@ -1549,9 +1548,8 @@ protected function exportGroupSites(int $batchSize, array $resources): void
}

try {
if (\in_array(Resource::TYPE_SITE_DEPLOYMENT, $resources)) {
$this->exportSiteDeployments($batchSize, true);
}
$exportOnlyActive = !\in_array(Resource::TYPE_SITE_DEPLOYMENT, $resources);
$this->exportSiteDeployments($batchSize, $exportOnlyActive);
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_SITE_DEPLOYMENT,
Expand Down Expand Up @@ -1643,8 +1641,13 @@ private function exportDeployments(int $batchSize, bool $exportOnlyActive = fals
/** @var Func $func */
$lastDocument = null;

if ($exportOnlyActive && $func->getActiveDeployment()) {
$deployment = $this->functions->getDeployment($func->getId(), $func->getActiveDeployment());
if ($exportOnlyActive) {
$activeDeploymentId = $func->getActiveDeployment();
if (empty($activeDeploymentId)) {
continue; // active-only mode: nothing to export for this function
}

$deployment = $this->functions->getDeployment($func->getId(), $activeDeploymentId);

try {
$this->exportDeploymentData($func, $deployment);
Expand Down Expand Up @@ -1865,8 +1868,13 @@ private function exportSiteDeployments(int $batchSize, bool $exportOnlyActive =
/** @var Site $site */
$lastDocument = null;

if ($exportOnlyActive && $site->getActiveDeployment()) {
$deployment = $this->sites->getDeployment($site->getId(), $site->getActiveDeployment());
if ($exportOnlyActive) {
$activeDeploymentId = $site->getActiveDeployment();
if (empty($activeDeploymentId)) {
continue; // active-only mode: nothing to export for this site
}

$deployment = $this->sites->getDeployment($site->getId(), $activeDeploymentId);

try {
$this->exportSiteDeploymentData($site, $deployment);
Expand Down