Skip to content
42 changes: 42 additions & 0 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Appwrite\Enums\PasswordHash;
use Appwrite\Enums\Runtime;
use Appwrite\InputFile;
use Appwrite\Services\Backups;
use Appwrite\Services\Functions;
use Appwrite\Services\Sites;
use Appwrite\Services\Storage;
Expand Down Expand Up @@ -38,6 +39,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 All @@ -60,6 +62,7 @@ class Appwrite extends Destination

protected string $key;

private Backups $backups;
private Functions $functions;
private Sites $sites;
private Storage $storage;
Expand Down Expand Up @@ -94,6 +97,7 @@ public function __construct(
->setProject($project)
->setKey($key);

$this->backups = new Backups($this->client);
$this->functions = new Functions($this->client);
$this->sites = new Sites($this->client);
$this->storage = new Storage($this->client);
Expand Down Expand Up @@ -138,6 +142,9 @@ public static function getSupportedResources(): array
Resource::TYPE_DEPLOYMENT,
Resource::TYPE_ENVIRONMENT_VARIABLE,

// Backups
Resource::TYPE_BACKUP_POLICY,

// Sites
Resource::TYPE_SITE,
Resource::TYPE_SITE_DEPLOYMENT,
Expand Down Expand Up @@ -222,6 +229,15 @@ public function report(array $resources = [], array $resourceIds = []): array
$this->sites->create('', '', Framework::OTHER(), BuildRuntime::STATIC1());
}

// Backups
if (\in_array(Resource::TYPE_BACKUP_POLICY, $resources)) {
$scope = 'policies.read';
$this->backups->listPolicies();

$scope = 'policies.write';
$this->backups->createPolicy('', [], 0, '');
}

} catch (AppwriteException $e) {
if ($e->getCode() === 403) {
throw new \Exception('Missing scope: ' . $scope, previous: $e);
Expand Down Expand Up @@ -259,6 +275,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),
Transfer::GROUP_SITES => $this->importSiteResource($resource),
default => throw new \Exception('Invalid resource group'),
};
Expand Down Expand Up @@ -1463,6 +1480,31 @@ 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 */
$this->backups->createPolicy(
policyId: 'unique()',
services: $resource->getServices(),
retention: $resource->getRetention(),
schedule: $resource->getSchedule(),
name: $resource->getPolicyName() ?: null,
resourceId: $resource->getResourceId() ?: null,
enabled: $resource->getEnabled(),
);
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 @@ -60,6 +60,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 Down Expand Up @@ -89,6 +92,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;
}

public function getSitesBatchSize(): int
{
return static::$defaultBatchSize;
Expand Down Expand Up @@ -94,6 +99,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,
Transfer::GROUP_SITES => Transfer::GROUP_SITES_RESOURCES,
];

Expand Down Expand Up @@ -123,6 +129,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;
case Transfer::GROUP_SITES:
$this->exportGroupSites($this->getSitesBatchSize(), $resources);
break;
Expand Down Expand Up @@ -162,6 +171,14 @@ abstract protected function exportGroupStorage(int $batchSize, array $resources)
*/
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;

/**
* Export Sites Group
*
Expand Down
Loading
Loading