Skip to content

Commit 818e9f8

Browse files
committed
fix: improve backup policy import error handling and resourceId validation
1 parent f662701 commit 818e9f8

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

src/Migration/Destinations/Appwrite.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,17 @@ public function report(array $resources = [], array $resourceIds = []): array
228228
], []);
229229
} catch (\Throwable $e) {
230230
$body = \json_decode($e->getMessage(), true);
231-
if (\is_array($body) && ($body['code'] ?? 0) === 401) {
232-
$type = $body['type'] ?? '';
233-
if ($type === 'additional_resource_not_allowed') {
234-
throw new \Exception('Backups are not available on the destination project\'s plan', previous: $e);
235-
}
231+
$code = $body['code'] ?? 0;
232+
$type = $body['type'] ?? '';
233+
234+
if ($type === 'additional_resource_not_allowed') {
235+
throw new \Exception('Backups are not available on the destination project\'s plan', previous: $e);
236+
}
237+
238+
if ($code === 401 || $code === 403) {
236239
throw new \Exception('Missing scope: ' . $scope, previous: $e);
237240
}
241+
238242
throw $e;
239243
}
240244
}
@@ -1494,10 +1498,10 @@ public function importBackupResource(Resource $resource): Resource
14941498
$collection = match ($resource->getResourceType()) {
14951499
Resource::TYPE_DATABASE => 'databases',
14961500
Resource::TYPE_BUCKET => 'buckets',
1497-
Resource::TYPE_FUNCTION => null, // Functions don't support per-resource backup policies
1498-
default => null,
1501+
default => null, // Only databases and buckets support per-resource backup policies
14991502
};
15001503

1504+
// Only pass resourceId for supported resource types
15011505
if ($collection !== null) {
15021506
$doc = $this->database->getDocument($collection, $resource->getResourceId());
15031507
if ($doc->isEmpty()) {
@@ -1508,10 +1512,9 @@ public function importBackupResource(Resource $resource): Resource
15081512
message: 'Referenced ' . $resource->getResourceType() . ' "' . $resource->getResourceId() . '" not found on destination',
15091513
);
15101514
}
1511-
}
15121515

1513-
$params['resourceId'] = $resource->getResourceId();
1514-
$params['resourceType'] = $resource->getResourceType();
1516+
$params['resourceId'] = $resource->getResourceId();
1517+
}
15151518
}
15161519

15171520
$this->call('POST', '/backups/policies', [

0 commit comments

Comments
 (0)