Skip to content

Commit 7064f31

Browse files
Merge remote-tracking branch 'origin/main' into multitype-db
2 parents d16101a + c2d0169 commit 7064f31

2 files changed

Lines changed: 62 additions & 26 deletions

File tree

src/Migration/Destinations/Appwrite.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,17 @@ public function importFunctionResource(Resource $resource): Resource
14321432
*/
14331433
private function importDeployment(Deployment $deployment): Resource
14341434
{
1435-
$functionId = $deployment->getFunction()->getId();
1435+
$function = $deployment->getFunction();
1436+
1437+
// Deployment API always creates a new deployment, so unlike other resources
1438+
// there's no duplicate detection. Skip if the parent function wasn't imported successfully.
1439+
if ($function->getStatus() !== Resource::STATUS_SUCCESS) {
1440+
$deployment->setStatus(Resource::STATUS_SKIPPED, 'Parent function "' . $function->getId() . '" failed to import');
1441+
1442+
return $deployment;
1443+
}
1444+
1445+
$functionId = $function->getId();
14361446

14371447
$response = null;
14381448

@@ -1467,7 +1477,7 @@ private function importDeployment(Deployment $deployment): Resource
14671477
[
14681478
'functionId' => $functionId,
14691479
'code' => new \CURLFile('data://application/gzip;base64,' . base64_encode($deployment->getData()), 'application/gzip', 'deployment.tar.gz'),
1470-
'activate' => $deployment->getActivated(),
1480+
'activate' => $deployment->getActivated() ? 'true' : 'false',
14711481
'entrypoint' => $deployment->getEntrypoint(),
14721482
]
14731483
);
@@ -1633,7 +1643,17 @@ public function importSiteResource(Resource $resource): Resource
16331643
*/
16341644
private function importSiteDeployment(SiteDeployment $deployment): Resource
16351645
{
1636-
$siteId = $deployment->getSite()->getId();
1646+
$site = $deployment->getSite();
1647+
1648+
// Deployment API always creates a new deployment, so unlike other resources
1649+
// there's no duplicate detection. Skip if the parent site wasn't imported successfully.
1650+
if ($site->getStatus() !== Resource::STATUS_SUCCESS) {
1651+
$deployment->setStatus(Resource::STATUS_SKIPPED, 'Parent site "' . $site->getId() . '" failed to import');
1652+
1653+
return $deployment;
1654+
}
1655+
1656+
$siteId = $site->getId();
16371657

16381658
if ($deployment->getSize() <= Transfer::STORAGE_MAX_CHUNK_SIZE) {
16391659
$response = $this->client->call(
@@ -1645,7 +1665,7 @@ private function importSiteDeployment(SiteDeployment $deployment): Resource
16451665
[
16461666
'siteId' => $siteId,
16471667
'code' => new \CURLFile('data://application/gzip;base64,' . base64_encode($deployment->getData()), 'application/gzip', 'deployment.tar.gz'),
1648-
'activate' => $deployment->getActivated(),
1668+
'activate' => $deployment->getActivated() ? 'true' : 'false',
16491669
]
16501670
);
16511671

@@ -1669,7 +1689,7 @@ private function importSiteDeployment(SiteDeployment $deployment): Resource
16691689
[
16701690
'siteId' => $siteId,
16711691
'code' => new \CURLFile('data://application/gzip;base64,' . base64_encode($deployment->getData()), 'application/gzip', 'deployment.tar.gz'),
1672-
'activate' => $deployment->getActivated(),
1692+
'activate' => $deployment->getActivated() ? 'true' : 'false',
16731693
]
16741694
);
16751695

src/Migration/Sources/Appwrite.php

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ private function exportFileData(File $file): void
12941294
// Get the file size
12951295
$fileSize = $file->getSize();
12961296

1297-
if ($end > $fileSize) {
1297+
if ($end >= $fileSize) {
12981298
$end = $fileSize - 1;
12991299
}
13001300

@@ -1318,7 +1318,7 @@ private function exportFileData(File $file): void
13181318
$start += Transfer::STORAGE_MAX_CHUNK_SIZE;
13191319
$end += Transfer::STORAGE_MAX_CHUNK_SIZE;
13201320

1321-
if ($end > $fileSize) {
1321+
if ($end >= $fileSize) {
13221322
$end = $fileSize - 1;
13231323
}
13241324
}
@@ -1341,9 +1341,8 @@ protected function exportGroupFunctions(int $batchSize, array $resources): void
13411341
}
13421342

13431343
try {
1344-
if (\in_array(Resource::TYPE_DEPLOYMENT, $resources)) {
1345-
$this->exportDeployments($batchSize, true);
1346-
}
1344+
$exportOnlyActive = !\in_array(Resource::TYPE_DEPLOYMENT, $resources);
1345+
$this->exportDeployments($batchSize, $exportOnlyActive);
13471346
} catch (\Throwable $e) {
13481347
$this->addError(new Exception(
13491348
Resource::TYPE_DEPLOYMENT,
@@ -1372,9 +1371,8 @@ protected function exportGroupSites(int $batchSize, array $resources): void
13721371
}
13731372

13741373
try {
1375-
if (\in_array(Resource::TYPE_SITE_DEPLOYMENT, $resources)) {
1376-
$this->exportSiteDeployments($batchSize, true);
1377-
}
1374+
$exportOnlyActive = !\in_array(Resource::TYPE_SITE_DEPLOYMENT, $resources);
1375+
$this->exportSiteDeployments($batchSize, $exportOnlyActive);
13781376
} catch (\Throwable $e) {
13791377
$this->addError(new Exception(
13801378
Resource::TYPE_SITE_DEPLOYMENT,
@@ -1466,8 +1464,13 @@ private function exportDeployments(int $batchSize, bool $exportOnlyActive = fals
14661464
/** @var Func $func */
14671465
$lastDocument = null;
14681466

1469-
if ($exportOnlyActive && $func->getActiveDeployment()) {
1470-
$deployment = $this->functions->getDeployment($func->getId(), $func->getActiveDeployment());
1467+
if ($exportOnlyActive) {
1468+
$activeDeploymentId = $func->getActiveDeployment();
1469+
if (empty($activeDeploymentId)) {
1470+
continue; // active-only mode: nothing to export for this function
1471+
}
1472+
1473+
$deployment = $this->functions->getDeployment($func->getId(), $activeDeploymentId);
14711474

14721475
try {
14731476
$this->exportDeploymentData($func, $deployment);
@@ -1527,8 +1530,8 @@ private function exportDeploymentData(Func $func, array $deployment): void
15271530
$responseHeaders
15281531
);
15291532

1530-
// Content-Length header was missing, file is less than max buffer size.
1531-
if (!array_key_exists('Content-Length', $responseHeaders)) {
1533+
// content-length header missing, file is less than max buffer size
1534+
if (!array_key_exists('content-length', $responseHeaders)) {
15321535
$file = $this->call(
15331536
'GET',
15341537
"/functions/{$func->getId()}/deployments/{$deployment['$id']}/download",
@@ -1537,7 +1540,7 @@ private function exportDeploymentData(Func $func, array $deployment): void
15371540
$responseHeaders
15381541
);
15391542

1540-
$size = mb_strlen($file);
1543+
$size = strlen($file);
15411544

15421545
if ($end > $size) {
15431546
$end = $size - 1;
@@ -1560,7 +1563,11 @@ private function exportDeploymentData(Func $func, array $deployment): void
15601563
return;
15611564
}
15621565

1563-
$fileSize = $responseHeaders['Content-Length'];
1566+
$fileSize = $responseHeaders['content-length'];
1567+
1568+
if ($end >= $fileSize) {
1569+
$end = $fileSize - 1;
1570+
}
15641571

15651572
$deployment = new Deployment(
15661573
$deployment['$id'],
@@ -1595,7 +1602,7 @@ private function exportDeploymentData(Func $func, array $deployment): void
15951602
$start += Transfer::STORAGE_MAX_CHUNK_SIZE;
15961603
$end += Transfer::STORAGE_MAX_CHUNK_SIZE;
15971604

1598-
if ($end > $fileSize) {
1605+
if ($end >= $fileSize) {
15991606
$end = $fileSize - 1;
16001607
}
16011608
}
@@ -1684,8 +1691,13 @@ private function exportSiteDeployments(int $batchSize, bool $exportOnlyActive =
16841691
/** @var Site $site */
16851692
$lastDocument = null;
16861693

1687-
if ($exportOnlyActive && $site->getActiveDeployment()) {
1688-
$deployment = $this->sites->getDeployment($site->getId(), $site->getActiveDeployment());
1694+
if ($exportOnlyActive) {
1695+
$activeDeploymentId = $site->getActiveDeployment();
1696+
if (empty($activeDeploymentId)) {
1697+
continue; // active-only mode: nothing to export for this site
1698+
}
1699+
1700+
$deployment = $this->sites->getDeployment($site->getId(), $activeDeploymentId);
16891701

16901702
try {
16911703
$this->exportSiteDeploymentData($site, $deployment);
@@ -1743,7 +1755,7 @@ private function exportSiteDeploymentData(Site $site, array $deployment): void
17431755
$responseHeaders
17441756
);
17451757

1746-
if (!\array_key_exists('Content-Length', $responseHeaders)) {
1758+
if (!\array_key_exists('content-length', $responseHeaders)) {
17471759
$file = $this->call(
17481760
'GET',
17491761
"/sites/{$site->getId()}/deployments/{$deployment['$id']}/download",
@@ -1752,7 +1764,7 @@ private function exportSiteDeploymentData(Site $site, array $deployment): void
17521764
$responseHeaders
17531765
);
17541766

1755-
$size = mb_strlen($file);
1767+
$size = strlen($file);
17561768

17571769
if ($end > $size) {
17581770
$end = $size - 1;
@@ -1774,7 +1786,11 @@ private function exportSiteDeploymentData(Site $site, array $deployment): void
17741786
return;
17751787
}
17761788

1777-
$fileSize = $responseHeaders['Content-Length'];
1789+
$fileSize = $responseHeaders['content-length'];
1790+
1791+
if ($end >= $fileSize) {
1792+
$end = $fileSize - 1;
1793+
}
17781794

17791795
$siteDeployment = new SiteDeployment(
17801796
$deployment['$id'],
@@ -1805,7 +1821,7 @@ private function exportSiteDeploymentData(Site $site, array $deployment): void
18051821
$start += Transfer::STORAGE_MAX_CHUNK_SIZE;
18061822
$end += Transfer::STORAGE_MAX_CHUNK_SIZE;
18071823

1808-
if ($end > $fileSize) {
1824+
if ($end >= $fileSize) {
18091825
$end = $fileSize - 1;
18101826
}
18111827
}

0 commit comments

Comments
 (0)