Skip to content

Commit d3084fb

Browse files
committed
Add backup policy migration support
1 parent 95dcbb9 commit d3084fb

10 files changed

Lines changed: 191 additions & 0 deletions

File tree

src/Migration/Destinations/Appwrite.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ public static function getSupportedResources(): array
169169
Resource::TYPE_SITE,
170170
Resource::TYPE_SITE_DEPLOYMENT,
171171
Resource::TYPE_SITE_VARIABLE,
172+
173+
// Backups
174+
Resource::TYPE_BACKUP_POLICY,
172175
];
173176
}
174177

@@ -325,6 +328,7 @@ protected function import(array $resources, callable $callback): void
325328
Transfer::GROUP_FUNCTIONS => $this->importFunctionResource($resource),
326329
Transfer::GROUP_MESSAGING => $this->importMessagingResource($resource),
327330
Transfer::GROUP_SITES => $this->importSiteResource($resource),
331+
Transfer::GROUP_BACKUPS => $this->importBackupResource($resource),
328332
default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION),
329333
};
330334
} catch (\Throwable $e) {
@@ -1483,6 +1487,13 @@ public function importFunctionResource(Resource $resource): Resource
14831487
return $resource;
14841488
}
14851489

1490+
public function importBackupResource(Resource $resource): Resource
1491+
{
1492+
$resource->setStatus(Resource::STATUS_SUCCESS);
1493+
1494+
return $resource;
1495+
}
1496+
14861497
/**
14871498
* @throws AppwriteException
14881499
* @throws \Exception

src/Migration/Resource.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ abstract class Resource implements \JsonSerializable
7575

7676
public const TYPE_MESSAGE = 'message';
7777

78+
// Backups
79+
public const TYPE_BACKUP_POLICY = 'backup-policy';
80+
7881
// legacy terminologies
7982
public const TYPE_DOCUMENT = 'document';
8083
public const TYPE_ATTRIBUTE = 'attribute';
@@ -110,6 +113,7 @@ abstract class Resource implements \JsonSerializable
110113
self::TYPE_TOPIC,
111114
self::TYPE_SUBSCRIBER,
112115
self::TYPE_MESSAGE,
116+
self::TYPE_BACKUP_POLICY,
113117

114118
// legacy
115119
self::TYPE_DOCUMENT,
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Utopia\Migration\Resources\Backups;
4+
5+
use Utopia\Migration\Resource;
6+
use Utopia\Migration\Transfer;
7+
8+
class Policy extends Resource
9+
{
10+
/**
11+
* @param string $id
12+
* @param string $name
13+
* @param array<string> $services
14+
* @param int $retention
15+
* @param string $schedule
16+
* @param bool $enabled
17+
* @param string $resourceId
18+
* @param string $resourceType
19+
*/
20+
public function __construct(
21+
string $id = '',
22+
private readonly string $name = '',
23+
private readonly array $services = [],
24+
private readonly int $retention = 0,
25+
private readonly string $schedule = '',
26+
private readonly bool $enabled = true,
27+
private readonly string $resourceId = '',
28+
private readonly string $resourceType = '',
29+
) {
30+
$this->id = $id;
31+
}
32+
33+
/**
34+
* @param array<string, mixed> $array
35+
* @return self
36+
*/
37+
public static function fromArray(array $array): self
38+
{
39+
return new self(
40+
$array['id'],
41+
$array['name'] ?? '',
42+
$array['services'] ?? [],
43+
$array['retention'] ?? 0,
44+
$array['schedule'] ?? '',
45+
$array['enabled'] ?? true,
46+
$array['resourceId'] ?? '',
47+
$array['resourceType'] ?? '',
48+
);
49+
}
50+
51+
/**
52+
* @return array<string, mixed>
53+
*/
54+
public function jsonSerialize(): array
55+
{
56+
return [
57+
'id' => $this->id,
58+
'name' => $this->name,
59+
'services' => $this->services,
60+
'retention' => $this->retention,
61+
'schedule' => $this->schedule,
62+
'enabled' => $this->enabled,
63+
'resourceId' => $this->resourceId,
64+
'resourceType' => $this->resourceType,
65+
];
66+
}
67+
68+
public static function getName(): string
69+
{
70+
return Resource::TYPE_BACKUP_POLICY;
71+
}
72+
73+
public function getGroup(): string
74+
{
75+
return Transfer::GROUP_BACKUPS;
76+
}
77+
78+
public function getPolicyName(): string
79+
{
80+
return $this->name;
81+
}
82+
83+
/**
84+
* @return array<string>
85+
*/
86+
public function getServices(): array
87+
{
88+
return $this->services;
89+
}
90+
91+
public function getRetention(): int
92+
{
93+
return $this->retention;
94+
}
95+
96+
public function getSchedule(): string
97+
{
98+
return $this->schedule;
99+
}
100+
101+
public function getEnabled(): bool
102+
{
103+
return $this->enabled;
104+
}
105+
106+
public function getResourceId(): string
107+
{
108+
return $this->resourceId;
109+
}
110+
111+
public function getResourceType(): string
112+
{
113+
return $this->resourceType;
114+
}
115+
}

src/Migration/Source.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public function getSitesBatchSize(): int
4646
return static::$defaultBatchSize;
4747
}
4848

49+
public function getBackupsBatchSize(): int
50+
{
51+
return static::$defaultBatchSize;
52+
}
53+
4954
/**
5055
* @param array<Resource> $resources
5156
* @return void
@@ -109,6 +114,7 @@ public function exportResources(array $resources): void
109114
Transfer::GROUP_FUNCTIONS => Transfer::GROUP_FUNCTIONS_RESOURCES,
110115
Transfer::GROUP_MESSAGING => Transfer::GROUP_MESSAGING_RESOURCES,
111116
Transfer::GROUP_SITES => Transfer::GROUP_SITES_RESOURCES,
117+
Transfer::GROUP_BACKUPS => Transfer::GROUP_BACKUPS_RESOURCES,
112118
];
113119

114120
foreach ($mapping as $group => $resources) {
@@ -143,6 +149,9 @@ public function exportResources(array $resources): void
143149
case Transfer::GROUP_SITES:
144150
$this->exportGroupSites($this->getSitesBatchSize(), $resources);
145151
break;
152+
case Transfer::GROUP_BACKUPS:
153+
$this->exportGroupBackups($this->getBackupsBatchSize(), $resources);
154+
break;
146155
}
147156
}
148157
}
@@ -194,4 +203,12 @@ abstract protected function exportGroupMessaging(int $batchSize, array $resource
194203
* @param array<string> $resources Resources to export
195204
*/
196205
abstract protected function exportGroupSites(int $batchSize, array $resources): void;
206+
207+
/**
208+
* Export Backups Group
209+
*
210+
* @param int $batchSize
211+
* @param array<string> $resources Resources to export
212+
*/
213+
abstract protected function exportGroupBackups(int $batchSize, array $resources): void;
197214
}

src/Migration/Sources/Appwrite.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ public static function getSupportedResources(): array
200200
Resource::TYPE_SITE_DEPLOYMENT,
201201
Resource::TYPE_SITE_VARIABLE,
202202

203+
// Backups
204+
Resource::TYPE_BACKUP_POLICY,
205+
203206
// Settings
204207
];
205208
}
@@ -239,6 +242,7 @@ public function report(array $resources = [], array $resourceIds = []): array
239242
$this->reportFunctions($resources, $report, $resourceIds);
240243
$this->reportMessaging($resources, $report, $resourceIds);
241244
$this->reportSites($resources, $report, $resourceIds);
245+
$this->reportBackups($resources, $report, $resourceIds);
242246

243247
$report['version'] = $this->call(
244248
'GET',
@@ -1419,6 +1423,18 @@ protected function exportGroupSites(int $batchSize, array $resources): void
14191423
}
14201424
}
14211425

1426+
protected function exportGroupBackups(int $batchSize, array $resources): void
1427+
{
1428+
// No-op: backup policies are Cloud-only
1429+
}
1430+
1431+
protected function reportBackups(array $resources, array &$report, array $resourceIds = []): void
1432+
{
1433+
if (\in_array(Resource::TYPE_BACKUP_POLICY, $resources)) {
1434+
$report[Resource::TYPE_BACKUP_POLICY] = 0;
1435+
}
1436+
}
1437+
14221438
/**
14231439
* @throws AppwriteException
14241440
*/

src/Migration/Sources/CSV.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ protected function exportGroupSites(int $batchSize, array $resources): void
429429
throw new \Exception('Not Implemented');
430430
}
431431

432+
protected function exportGroupBackups(int $batchSize, array $resources): void
433+
{
434+
throw new \Exception('Not Implemented');
435+
}
436+
432437
/**
433438
* @param callable(resource $stream, string $delimiter): void $callback
434439
* @return void

src/Migration/Sources/Firebase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,4 +818,9 @@ protected function exportGroupSites(int $batchSize, array $resources): void
818818
{
819819
throw new \Exception('Not implemented');
820820
}
821+
822+
protected function exportGroupBackups(int $batchSize, array $resources): void
823+
{
824+
throw new \Exception('Not implemented');
825+
}
821826
}

src/Migration/Sources/JSON.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ protected function exportGroupMessaging(int $batchSize, array $resources): void
209209
throw new \Exception('Not Implemented');
210210
}
211211

212+
protected function exportGroupBackups(int $batchSize, array $resources): void
213+
{
214+
throw new \Exception('Not Implemented');
215+
}
216+
212217
/**
213218
* @throws \Exception
214219
*/

src/Migration/Sources/NHost.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,4 +951,9 @@ protected function exportGroupSites(int $batchSize, array $resources): void
951951
{
952952
throw new \Exception('Not Implemented');
953953
}
954+
955+
protected function exportGroupBackups(int $batchSize, array $resources): void
956+
{
957+
throw new \Exception('Not Implemented');
958+
}
954959
}

src/Migration/Transfer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Transfer
2626

2727
public const GROUP_MESSAGING = 'messaging';
2828

29+
public const GROUP_BACKUPS = 'backups';
30+
2931
public const GROUP_AUTH_RESOURCES = [
3032
Resource::TYPE_USER,
3133
Resource::TYPE_TEAM,
@@ -88,6 +90,10 @@ class Transfer
8890

8991
public const GROUP_SETTINGS_RESOURCES = [];
9092

93+
public const GROUP_BACKUPS_RESOURCES = [
94+
Resource::TYPE_BACKUP_POLICY,
95+
];
96+
9197
public const GROUP_MESSAGING_RESOURCES = [
9298
Resource::TYPE_PROVIDER,
9399
Resource::TYPE_TOPIC,
@@ -116,6 +122,7 @@ class Transfer
116122
Resource::TYPE_TOPIC,
117123
Resource::TYPE_SUBSCRIBER,
118124
Resource::TYPE_MESSAGE,
125+
Resource::TYPE_BACKUP_POLICY,
119126

120127
// legacy
121128
Resource::TYPE_DOCUMENT,
@@ -399,6 +406,7 @@ public static function extractServices(array $services): array
399406
self::GROUP_DATABASES_VECTOR_DB => array_merge($resources, self::GROUP_VECTORSDB_RESOURCES),
400407
self::GROUP_SETTINGS => array_merge($resources, self::GROUP_SETTINGS_RESOURCES),
401408
self::GROUP_MESSAGING => array_merge($resources, self::GROUP_MESSAGING_RESOURCES),
409+
self::GROUP_BACKUPS => array_merge($resources, self::GROUP_BACKUPS_RESOURCES),
402410
default => throw new \Exception('No service group found'),
403411
};
404412
}

0 commit comments

Comments
 (0)