Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ public static function getSupportedResources(): array
Resource::TYPE_SITE,
Resource::TYPE_SITE_DEPLOYMENT,
Resource::TYPE_SITE_VARIABLE,

// Backups
Resource::TYPE_BACKUP_POLICY,
];
}

Expand Down Expand Up @@ -325,6 +328,7 @@ protected function import(array $resources, callable $callback): void
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) {
Expand Down Expand Up @@ -1483,6 +1487,13 @@ public function importFunctionResource(Resource $resource): Resource
return $resource;
}

public function importBackupResource(Resource $resource): Resource
{
$resource->setStatus(Resource::STATUS_SKIPPED);

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 @@ -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';
Expand Down Expand Up @@ -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,
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'] ?? '',
);
Comment on lines +37 to +48
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Harden fromArray() for missing/invalid required id.

Line 40 assumes id exists and is valid. Malformed payloads will trigger a low-level notice instead of a clear validation error.

💡 Proposed fix
 public static function fromArray(array $array): self
 {
+    if (!isset($array['id']) || !\is_string($array['id']) || $array['id'] === '') {
+        throw new \InvalidArgumentException('Backup policy "id" is required.');
+    }
+
     return new self(
         $array['id'],
         $array['name'] ?? '',
         $array['services'] ?? [],
         $array['retention'] ?? 0,
         $array['schedule'] ?? '',
         $array['enabled'] ?? true,
         $array['resourceId'] ?? '',
         $array['resourceType'] ?? '',
     );
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Migration/Resources/Backups/Policy.php` around lines 37 - 48, The
fromArray() factory (Policy::fromArray) currently assumes $array['id'] exists;
add explicit validation for the required id: check isset($array['id']) and that
it's the expected type/format (e.g., non-empty string or integer) and throw a
clear InvalidArgumentException (or similar) if missing/invalid so callers get a
descriptive validation error instead of a PHP notice; then pass the
validated/casted id into new self(...) as before.

}

/**
* @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 @@ -46,6 +46,11 @@ public function getSitesBatchSize(): int
return static::$defaultBatchSize;
}

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

/**
* @param array<Resource> $resources
* @return void
Expand Down Expand Up @@ -109,6 +114,7 @@ public function exportResources(array $resources): void
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) {
Expand Down Expand Up @@ -143,6 +149,9 @@ public function exportResources(array $resources): void
case Transfer::GROUP_SITES:
$this->exportGroupSites($this->getSitesBatchSize(), $resources);
break;
case Transfer::GROUP_BACKUPS:
$this->exportGroupBackups($this->getBackupsBatchSize(), $resources);
break;
}
}
}
Expand Down Expand Up @@ -194,4 +203,12 @@ abstract protected function exportGroupMessaging(int $batchSize, array $resource
* @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 changes: 16 additions & 0 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ public static function getSupportedResources(): array
Resource::TYPE_SITE_DEPLOYMENT,
Resource::TYPE_SITE_VARIABLE,

// Backups
Resource::TYPE_BACKUP_POLICY,

// Settings
];
}
Expand Down Expand Up @@ -239,6 +242,7 @@ public function report(array $resources = [], array $resourceIds = []): array
$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',
Expand Down Expand Up @@ -1419,6 +1423,18 @@ protected function exportGroupSites(int $batchSize, array $resources): void
}
}

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
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Migration/Sources/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ protected function exportGroupSites(int $batchSize, array $resources): void
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
Expand Down
5 changes: 5 additions & 0 deletions src/Migration/Sources/Firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,4 +818,9 @@ protected function exportGroupSites(int $batchSize, array $resources): void
{
throw new \Exception('Not implemented');
}

protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not implemented');
}
}
5 changes: 5 additions & 0 deletions src/Migration/Sources/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ protected function exportGroupMessaging(int $batchSize, array $resources): void
throw new \Exception('Not Implemented');
}

protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}

/**
* @throws \Exception
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Migration/Sources/NHost.php
Original file line number Diff line number Diff line change
Expand Up @@ -951,4 +951,9 @@ protected function exportGroupSites(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}

protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}
}
8 changes: 8 additions & 0 deletions src/Migration/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -116,6 +122,7 @@ class Transfer
Resource::TYPE_TOPIC,
Resource::TYPE_SUBSCRIBER,
Resource::TYPE_MESSAGE,
Resource::TYPE_BACKUP_POLICY,

// legacy
Resource::TYPE_DOCUMENT,
Expand Down Expand Up @@ -399,6 +406,7 @@ public static function extractServices(array $services): array
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'),
};
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Migration/Unit/Adapters/MockSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public static function getSupportedResources(): array
Resource::TYPE_TOPIC,
Resource::TYPE_SUBSCRIBER,
Resource::TYPE_MESSAGE,
Resource::TYPE_BACKUP_POLICY,

// legacy
Resource::TYPE_DOCUMENT,
Expand Down Expand Up @@ -198,4 +199,15 @@ protected function exportGroupSites(int $batchSize, array $resources): void
$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);
}
}
}
Loading