Merge pull request #163 from utopia-php/backup-migration-multitype

Add backup policy migration support
This commit is contained in:
Jake Barnby authored and GitHub committed 2026-04-30 20:11:07 +12:00
commit b164631404
11 files changed
+203

No files matched your search

+11
View File
@@ -171,6 +171,9 @@ class Appwrite extends Destination
Resource::TYPE_SITE,
Resource::TYPE_SITE_DEPLOYMENT,
Resource::TYPE_SITE_VARIABLE,
// Backups
Resource::TYPE_BACKUP_POLICY,
];
}
@@ -326,6 +329,7 @@ class Appwrite extends Destination
Transfer::GROUP_FUNCTIONS => $this->importFunctionResource($resource),
Transfer::GROUP_MESSAGING => $this->importMessagingResource($resource),
Transfer::GROUP_SITES => $this->importSiteResource($resource),
Transfer::GROUP_BACKUPS => $this->importBackupResource($resource),
default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION),
};
} catch (\Throwable $e) {
@@ -1501,6 +1505,13 @@ class Appwrite extends Destination
return $resource;
}
public function importBackupResource(Resource $resource): Resource
{
$resource->setStatus(Resource::STATUS_SKIPPED);
return $resource;
}
/**
* @throws AppwriteException
* @throws \Exception
+4
View File
@@ -75,6 +75,9 @@ abstract class Resource implements \JsonSerializable
public const TYPE_MESSAGE = 'message';
// Backups
public const TYPE_BACKUP_POLICY = 'backup-policy';
// legacy terminologies
public const TYPE_DOCUMENT = 'document';
public const TYPE_ATTRIBUTE = 'attribute';
@@ -110,6 +113,7 @@ abstract class Resource implements \JsonSerializable
self::TYPE_TOPIC,
self::TYPE_SUBSCRIBER,
self::TYPE_MESSAGE,
self::TYPE_BACKUP_POLICY,
// legacy
self::TYPE_DOCUMENT,
+115
View File
@@ -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
View File
@@ -46,6 +46,11 @@ abstract class Source extends Target
return static::$defaultBatchSize;
}
public function getBackupsBatchSize(): int
{
return static::$defaultBatchSize;
}
/**
* @param array<Resource> $resources
* @return void
@@ -109,6 +114,7 @@ abstract class Source extends Target
Transfer::GROUP_FUNCTIONS => Transfer::GROUP_FUNCTIONS_RESOURCES,
Transfer::GROUP_MESSAGING => Transfer::GROUP_MESSAGING_RESOURCES,
Transfer::GROUP_SITES => Transfer::GROUP_SITES_RESOURCES,
Transfer::GROUP_BACKUPS => Transfer::GROUP_BACKUPS_RESOURCES,
];
foreach ($mapping as $group => $resources) {
@@ -143,6 +149,9 @@ abstract class Source extends Target
case Transfer::GROUP_SITES:
$this->exportGroupSites($this->getSitesBatchSize(), $resources);
break;
case Transfer::GROUP_BACKUPS:
$this->exportGroupBackups($this->getBackupsBatchSize(), $resources);
break;
}
}
}
@@ -194,4 +203,12 @@ abstract class Source extends Target
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupSites(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;
}
+16
View File
@@ -199,6 +199,9 @@ class Appwrite extends Source
Resource::TYPE_SITE_DEPLOYMENT,
Resource::TYPE_SITE_VARIABLE,
// Backups
Resource::TYPE_BACKUP_POLICY,
// Settings
];
}
@@ -238,6 +241,7 @@ class Appwrite extends Source
$this->reportFunctions($resources, $report, $resourceIds);
$this->reportMessaging($resources, $report, $resourceIds);
$this->reportSites($resources, $report, $resourceIds);
$this->reportBackups($resources, $report, $resourceIds);
$report['version'] = $this->call(
'GET',
@@ -1421,6 +1425,18 @@ class Appwrite extends Source
}
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
// No-op: backup policies are Cloud-only
}
protected function reportBackups(array $resources, array &$report, array $resourceIds = []): void
{
if (\in_array(Resource::TYPE_BACKUP_POLICY, $resources)) {
$report[Resource::TYPE_BACKUP_POLICY] = 0;
}
}
/**
* @throws AppwriteException
*/
+5
View File
@@ -429,6 +429,11 @@ class CSV extends Source
throw new \Exception('Not Implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}
/**
* @param callable(resource $stream, string $delimiter): void $callback
* @return void
+5
View File
@@ -817,4 +817,9 @@ class Firebase extends Source
{
throw new \Exception('Not implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not implemented');
}
}
+5
View File
@@ -209,6 +209,11 @@ class JSON extends Source
throw new \Exception('Not Implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}
/**
* @throws \Exception
*/
+5
View File
@@ -951,4 +951,9 @@ class NHost extends Source
{
throw new \Exception('Not Implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}
}
+8
View File
@@ -26,6 +26,8 @@ class Transfer
public const GROUP_MESSAGING = 'messaging';
public const GROUP_BACKUPS = 'backups';
public const GROUP_AUTH_RESOURCES = [
Resource::TYPE_USER,
Resource::TYPE_TEAM,
@@ -88,6 +90,10 @@ class Transfer
public const GROUP_SETTINGS_RESOURCES = [];
public const GROUP_BACKUPS_RESOURCES = [
Resource::TYPE_BACKUP_POLICY,
];
public const GROUP_MESSAGING_RESOURCES = [
Resource::TYPE_PROVIDER,
Resource::TYPE_TOPIC,
@@ -116,6 +122,7 @@ class Transfer
Resource::TYPE_TOPIC,
Resource::TYPE_SUBSCRIBER,
Resource::TYPE_MESSAGE,
Resource::TYPE_BACKUP_POLICY,
// legacy
Resource::TYPE_DOCUMENT,
@@ -399,6 +406,7 @@ class Transfer
self::GROUP_DATABASES_VECTOR_DB => array_merge($resources, self::GROUP_VECTORSDB_RESOURCES),
self::GROUP_SETTINGS => array_merge($resources, self::GROUP_SETTINGS_RESOURCES),
self::GROUP_MESSAGING => array_merge($resources, self::GROUP_MESSAGING_RESOURCES),
self::GROUP_BACKUPS => array_merge($resources, self::GROUP_BACKUPS_RESOURCES),
default => throw new \Exception('No service group found'),
};
}
@@ -84,6 +84,7 @@ class MockSource extends Source
Resource::TYPE_TOPIC,
Resource::TYPE_SUBSCRIBER,
Resource::TYPE_MESSAGE,
Resource::TYPE_BACKUP_POLICY,
// legacy
Resource::TYPE_DOCUMENT,
@@ -198,4 +199,15 @@ class MockSource extends Source
$this->handleResourceTransfer(Transfer::GROUP_SITES, $resource);
}
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
foreach (Transfer::GROUP_BACKUPS_RESOURCES as $resource) {
if (!\in_array($resource, $resources)) {
continue;
}
$this->handleResourceTransfer(Transfer::GROUP_BACKUPS, $resource);
}
}
}