Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Utopia\Migration\Resources\Auth\Membership;
use Utopia\Migration\Resources\Auth\Team;
use Utopia\Migration\Resources\Auth\User;
use Utopia\Migration\Resources\Backups\Policy;
use Utopia\Migration\Resources\Database\Column;
use Utopia\Migration\Resources\Database\Database;
use Utopia\Migration\Resources\Database\Index;
Expand Down Expand Up @@ -128,6 +129,9 @@ public static function getSupportedResources(): array
Resource::TYPE_FUNCTION,
Resource::TYPE_DEPLOYMENT,
Resource::TYPE_ENVIRONMENT_VARIABLE,

// Backups
Resource::TYPE_BACKUP_POLICY,
];
}

Expand Down Expand Up @@ -206,6 +210,35 @@ public function report(array $resources = [], array $resourceIds = []): array
throw $e;
}

// Backups (uses call() instead of SDK, so needs separate error handling)
if (\in_array(Resource::TYPE_BACKUP_POLICY, $resources)) {
try {
$scope = 'policies.read';
$this->call('GET', '/backups/policies', [
'Content-Type' => 'application/json',
'X-Appwrite-Project' => $this->project,
'X-Appwrite-Key' => $this->key,
]);

$scope = 'policies.write';
$this->call('POST', '/backups/policies', [
'Content-Type' => 'application/json',
'X-Appwrite-Project' => $this->project,
'X-Appwrite-Key' => $this->key,
], []);
} catch (\Throwable $e) {
$body = \json_decode($e->getMessage(), true);
if (\is_array($body) && ($body['code'] ?? 0) === 401) {
$type = $body['type'] ?? '';
if ($type === 'additional_resource_not_allowed') {
throw new \Exception('Backups are not available on the destination project\'s plan', previous: $e);
}
throw new \Exception('Missing scope: ' . $scope, previous: $e);
}
throw $e;
}
}
Comment thread
premtsd-code marked this conversation as resolved.
Outdated

return [];
}

Expand Down Expand Up @@ -236,6 +269,7 @@ protected function import(array $resources, callable $callback): void
Transfer::GROUP_STORAGE => $this->importFileResource($resource),
Transfer::GROUP_AUTH => $this->importAuthResource($resource),
Transfer::GROUP_FUNCTIONS => $this->importFunctionResource($resource),
Transfer::GROUP_BACKUPS => $this->importBackupResource($resource),
default => throw new \Exception('Invalid resource group'),
};
} catch (\Throwable $e) {
Expand Down Expand Up @@ -1439,6 +1473,60 @@ public function importFunctionResource(Resource $resource): Resource
return $resource;
}

/**
* @throws \Exception
*/
public function importBackupResource(Resource $resource): Resource
{
switch ($resource->getName()) {
case Resource::TYPE_BACKUP_POLICY:
/** @var Policy $resource */
$params = [
'policyId' => $resource->getId(),
'name' => $resource->getPolicyName(),
'services' => $resource->getServices(),
'enabled' => $resource->getEnabled(),
'retention' => $resource->getRetention(),
'schedule' => $resource->getSchedule(),
];

if ($resource->getResourceId()) {
$collection = match ($resource->getResourceType()) {
Resource::TYPE_DATABASE => 'databases',
Resource::TYPE_BUCKET => 'buckets',
Resource::TYPE_FUNCTION => null, // Functions don't support per-resource backup policies
default => null,
};

if ($collection !== null) {
$doc = $this->database->getDocument($collection, $resource->getResourceId());
if ($doc->isEmpty()) {
throw new Exception(
resourceName: $resource->getName(),
resourceGroup: $resource->getGroup(),
resourceId: $resource->getId(),
message: 'Referenced ' . $resource->getResourceType() . ' "' . $resource->getResourceId() . '" not found on destination',
);
}
}

$params['resourceId'] = $resource->getResourceId();
$params['resourceType'] = $resource->getResourceType();
}

$this->call('POST', '/backups/policies', [
'Content-Type' => 'application/json',
'X-Appwrite-Project' => $this->project,
'X-Appwrite-Key' => $this->key,
], $params);
break;
}

$resource->setStatus(Resource::STATUS_SUCCESS);

return $resource;
}

/**
* @throws AppwriteException
* @throws \Exception
Expand Down
4 changes: 4 additions & 0 deletions src/Migration/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ abstract class Resource implements \JsonSerializable

public const TYPE_ENVIRONMENT_VARIABLE = 'environment-variable';

// Backups
public const TYPE_BACKUP_POLICY = 'backup-policy';

// legacy terminologies
public const TYPE_DOCUMENT = 'document';
public const TYPE_ATTRIBUTE = 'attribute';
Expand All @@ -80,6 +83,7 @@ abstract class Resource implements \JsonSerializable
self::TYPE_ENVIRONMENT_VARIABLE,
self::TYPE_TEAM,
self::TYPE_MEMBERSHIP,
self::TYPE_BACKUP_POLICY,

// legacy
self::TYPE_DOCUMENT,
Expand Down
115 changes: 115 additions & 0 deletions src/Migration/Resources/Backups/Policy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Utopia\Migration\Resources\Backups;

use Utopia\Migration\Resource;
use Utopia\Migration\Transfer;

class Policy extends Resource
{
/**
* @param string $id
* @param string $name
* @param array<string> $services
* @param int $retention
* @param string $schedule
* @param bool $enabled
* @param string $resourceId
* @param string $resourceType
*/
public function __construct(
string $id = '',
private readonly string $name = '',
private readonly array $services = [],
private readonly int $retention = 0,
private readonly string $schedule = '',
private readonly bool $enabled = true,
private readonly string $resourceId = '',
private readonly string $resourceType = '',
) {
$this->id = $id;
}

/**
* @param array<string, mixed> $array
* @return self
*/
public static function fromArray(array $array): self
{
return new self(
$array['id'] ?? '',
$array['name'] ?? '',
$array['services'] ?? [],
$array['retention'] ?? 0,
$array['schedule'] ?? '',
$array['enabled'] ?? true,
$array['resourceId'] ?? '',
$array['resourceType'] ?? '',
);
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'services' => $this->services,
'retention' => $this->retention,
'schedule' => $this->schedule,
'enabled' => $this->enabled,
'resourceId' => $this->resourceId,
'resourceType' => $this->resourceType,
];
}

public static function getName(): string
{
return Resource::TYPE_BACKUP_POLICY;
}

public function getGroup(): string
{
return Transfer::GROUP_BACKUPS;
}

public function getPolicyName(): string
{
return $this->name;
}

/**
* @return array<string>
*/
public function getServices(): array
{
return $this->services;
}

public function getRetention(): int
{
return $this->retention;
}

public function getSchedule(): string
{
return $this->schedule;
}

public function getEnabled(): bool
{
return $this->enabled;
}

public function getResourceId(): string
{
return $this->resourceId;
}

public function getResourceType(): string
{
return $this->resourceType;
}
}
17 changes: 17 additions & 0 deletions src/Migration/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function getFunctionsBatchSize(): int
return static::$defaultBatchSize;
}

public function getBackupsBatchSize(): int
{
return static::$defaultBatchSize;
}

/**
* @param array<Resource> $resources
* @return void
Expand Down Expand Up @@ -89,6 +94,7 @@ public function exportResources(array $resources): void
Transfer::GROUP_DATABASES => Transfer::GROUP_DATABASES_RESOURCES,
Transfer::GROUP_STORAGE => Transfer::GROUP_STORAGE_RESOURCES,
Transfer::GROUP_FUNCTIONS => Transfer::GROUP_FUNCTIONS_RESOURCES,
Transfer::GROUP_BACKUPS => Transfer::GROUP_BACKUPS_RESOURCES,
];

foreach ($mapping as $group => $resources) {
Expand Down Expand Up @@ -117,6 +123,9 @@ public function exportResources(array $resources): void
case Transfer::GROUP_FUNCTIONS:
$this->exportGroupFunctions($this->getFunctionsBatchSize(), $resources);
break;
case Transfer::GROUP_BACKUPS:
$this->exportGroupBackups($this->getBackupsBatchSize(), $resources);
break;
}
}
}
Expand Down Expand Up @@ -152,4 +161,12 @@ abstract protected function exportGroupStorage(int $batchSize, array $resources)
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupFunctions(int $batchSize, array $resources): void;

/**
* Export Backups Group
*
* @param int $batchSize
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupBackups(int $batchSize, array $resources): void;
}
Loading