Skip to content

Commit c2d0169

Browse files
authored
Merge pull request #153 from utopia-php/fix-export-inactive-deployments
2 parents 037bf4b + 0766cd9 commit c2d0169

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

src/Migration/Destinations/Appwrite.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,17 @@ public function importFunctionResource(Resource $resource): Resource
14731473
*/
14741474
private function importDeployment(Deployment $deployment): Resource
14751475
{
1476-
$functionId = $deployment->getFunction()->getId();
1476+
$function = $deployment->getFunction();
1477+
1478+
// Deployment API always creates a new deployment, so unlike other resources
1479+
// there's no duplicate detection. Skip if the parent function wasn't imported successfully.
1480+
if ($function->getStatus() !== Resource::STATUS_SUCCESS) {
1481+
$deployment->setStatus(Resource::STATUS_SKIPPED, 'Parent function "' . $function->getId() . '" failed to import');
1482+
1483+
return $deployment;
1484+
}
1485+
1486+
$functionId = $function->getId();
14771487

14781488
$response = null;
14791489

@@ -1508,7 +1518,7 @@ private function importDeployment(Deployment $deployment): Resource
15081518
[
15091519
'functionId' => $functionId,
15101520
'code' => new \CURLFile('data://application/gzip;base64,' . base64_encode($deployment->getData()), 'application/gzip', 'deployment.tar.gz'),
1511-
'activate' => $deployment->getActivated(),
1521+
'activate' => $deployment->getActivated() ? 'true' : 'false',
15121522
'entrypoint' => $deployment->getEntrypoint(),
15131523
]
15141524
);
@@ -1674,7 +1684,17 @@ public function importSiteResource(Resource $resource): Resource
16741684
*/
16751685
private function importSiteDeployment(SiteDeployment $deployment): Resource
16761686
{
1677-
$siteId = $deployment->getSite()->getId();
1687+
$site = $deployment->getSite();
1688+
1689+
// Deployment API always creates a new deployment, so unlike other resources
1690+
// there's no duplicate detection. Skip if the parent site wasn't imported successfully.
1691+
if ($site->getStatus() !== Resource::STATUS_SUCCESS) {
1692+
$deployment->setStatus(Resource::STATUS_SKIPPED, 'Parent site "' . $site->getId() . '" failed to import');
1693+
1694+
return $deployment;
1695+
}
1696+
1697+
$siteId = $site->getId();
16781698

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

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

src/Migration/Sources/Appwrite.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,9 +1518,8 @@ protected function exportGroupFunctions(int $batchSize, array $resources): void
15181518
}
15191519

15201520
try {
1521-
if (\in_array(Resource::TYPE_DEPLOYMENT, $resources)) {
1522-
$this->exportDeployments($batchSize, true);
1523-
}
1521+
$exportOnlyActive = !\in_array(Resource::TYPE_DEPLOYMENT, $resources);
1522+
$this->exportDeployments($batchSize, $exportOnlyActive);
15241523
} catch (\Throwable $e) {
15251524
$this->addError(new Exception(
15261525
Resource::TYPE_DEPLOYMENT,
@@ -1549,9 +1548,8 @@ protected function exportGroupSites(int $batchSize, array $resources): void
15491548
}
15501549

15511550
try {
1552-
if (\in_array(Resource::TYPE_SITE_DEPLOYMENT, $resources)) {
1553-
$this->exportSiteDeployments($batchSize, true);
1554-
}
1551+
$exportOnlyActive = !\in_array(Resource::TYPE_SITE_DEPLOYMENT, $resources);
1552+
$this->exportSiteDeployments($batchSize, $exportOnlyActive);
15551553
} catch (\Throwable $e) {
15561554
$this->addError(new Exception(
15571555
Resource::TYPE_SITE_DEPLOYMENT,
@@ -1643,8 +1641,13 @@ private function exportDeployments(int $batchSize, bool $exportOnlyActive = fals
16431641
/** @var Func $func */
16441642
$lastDocument = null;
16451643

1646-
if ($exportOnlyActive && $func->getActiveDeployment()) {
1647-
$deployment = $this->functions->getDeployment($func->getId(), $func->getActiveDeployment());
1644+
if ($exportOnlyActive) {
1645+
$activeDeploymentId = $func->getActiveDeployment();
1646+
if (empty($activeDeploymentId)) {
1647+
continue; // active-only mode: nothing to export for this function
1648+
}
1649+
1650+
$deployment = $this->functions->getDeployment($func->getId(), $activeDeploymentId);
16481651

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

1868-
if ($exportOnlyActive && $site->getActiveDeployment()) {
1869-
$deployment = $this->sites->getDeployment($site->getId(), $site->getActiveDeployment());
1871+
if ($exportOnlyActive) {
1872+
$activeDeploymentId = $site->getActiveDeployment();
1873+
if (empty($activeDeploymentId)) {
1874+
continue; // active-only mode: nothing to export for this site
1875+
}
1876+
1877+
$deployment = $this->sites->getDeployment($site->getId(), $activeDeploymentId);
18701878

18711879
try {
18721880
$this->exportSiteDeploymentData($site, $deployment);

0 commit comments

Comments
 (0)