diff --git a/composer.json b/composer.json index 5585f2a5..4c9cdc40 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "php": ">=8.1", "ext-curl": "*", "ext-openssl": "*", - "appwrite/appwrite": "19.*", + "appwrite/appwrite": "20.*", "utopia-php/database": "5.*", "utopia-php/storage": "1.0.*", "utopia-php/dsn": "0.2.*", diff --git a/composer.lock b/composer.lock index 17d14221..9444b087 100644 --- a/composer.lock +++ b/composer.lock @@ -4,29 +4,29 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "37980b9001fbbd4f213f3102c1332727", + "content-hash": "cce689808f9fd30a82c0b7d1819af517", "packages": [ { "name": "appwrite/appwrite", - "version": "19.1.0", + "version": "20.1.0", "source": { "type": "git", "url": "https://github.com/appwrite/sdk-for-php.git", - "reference": "8738e812062f899c85b2598eef43d6a247f08a56" + "reference": "e8c76cbe6b09f964694a3d1b9096cb91737e48e2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/sdk-for-php/zipball/8738e812062f899c85b2598eef43d6a247f08a56", - "reference": "8738e812062f899c85b2598eef43d6a247f08a56", + "url": "https://api.github.com/repos/appwrite/sdk-for-php/zipball/e8c76cbe6b09f964694a3d1b9096cb91737e48e2", + "reference": "e8c76cbe6b09f964694a3d1b9096cb91737e48e2", "shasum": "" }, "require": { "ext-curl": "*", "ext-json": "*", - "php": ">=7.1.0" + "php": ">=8.0.0" }, "require-dev": { - "mockery/mockery": "^1.6.12", + "mockery/mockery": "1.6.12", "phpunit/phpunit": "^10" }, "type": "library", @@ -39,14 +39,14 @@ "license": [ "BSD-3-Clause" ], - "description": "Appwrite is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API", + "description": "Appwrite is an open-source self-hosted backend server that abstracts and simplifies complex and repetitive development tasks behind a very simple REST API", "support": { "email": "team@appwrite.io", "issues": "https://github.com/appwrite/sdk-for-php/issues", - "source": "https://github.com/appwrite/sdk-for-php/tree/19.1.0", + "source": "https://github.com/appwrite/sdk-for-php/tree/20.1.0", "url": "https://appwrite.io/support" }, - "time": "2025-12-18T08:07:43+00:00" + "time": "2026-02-12T11:55:40+00:00" }, { "name": "brick/math", @@ -4997,7 +4997,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": {}, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 256fe00c..4bfdf2fd 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -98,8 +98,8 @@ public function __construct( string $endpoint, string $key, protected UtopiaDatabase $dbForProject, - callable $getDatabasesDB, - protected array $collectionStructure + ?callable $getDatabasesDB = null, + protected array $collectionStructure = [] ) { $this->project = $project; $this->endpoint = $endpoint; @@ -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, ]; } @@ -324,6 +327,7 @@ protected function import(array $resources, callable $callback): void Transfer::GROUP_AUTH => $this->importAuthResource($resource), Transfer::GROUP_FUNCTIONS => $this->importFunctionResource($resource), Transfer::GROUP_MESSAGING => $this->importMessagingResource($resource), + Transfer::GROUP_BACKUPS => $this->importBackupResource($resource), Transfer::GROUP_SITES => $this->importSiteResource($resource), default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION), }; @@ -1479,6 +1483,13 @@ public function importFunctionResource(Resource $resource): Resource return $resource; } + public function importBackupResource(Resource $resource): Resource + { + $resource->setStatus(Resource::STATUS_SUCCESS); + + return $resource; + } + /** * @throws AppwriteException * @throws \Exception diff --git a/src/Migration/Resource.php b/src/Migration/Resource.php index 1dc198c6..2fc102c5 100644 --- a/src/Migration/Resource.php +++ b/src/Migration/Resource.php @@ -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, diff --git a/src/Migration/Resources/Backups/Policy.php b/src/Migration/Resources/Backups/Policy.php new file mode 100644 index 00000000..d5358b42 --- /dev/null +++ b/src/Migration/Resources/Backups/Policy.php @@ -0,0 +1,115 @@ + $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 $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 + */ + 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 + */ + 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; + } +} diff --git a/src/Migration/Resources/Database/Database.php b/src/Migration/Resources/Database/Database.php index 05afab2c..23737f29 100644 --- a/src/Migration/Resources/Database/Database.php +++ b/src/Migration/Resources/Database/Database.php @@ -52,7 +52,7 @@ public static function fromArray(array $array): self enabled: $array['enabled'] ?? true, originalId: $array['originalId'] ?? '', type: $array['type'] ?? 'legacy', - database: $array['database'] + database: $array['database'] ?? '' ); } diff --git a/src/Migration/Source.php b/src/Migration/Source.php index b6eb8c42..3894597c 100644 --- a/src/Migration/Source.php +++ b/src/Migration/Source.php @@ -41,6 +41,11 @@ public function getMessagingBatchSize(): int return static::$defaultBatchSize; } + public function getBackupsBatchSize(): int + { + return static::$defaultBatchSize; + } + public function getSitesBatchSize(): int { return static::$defaultBatchSize; @@ -108,6 +113,7 @@ public function exportResources(array $resources): void Transfer::GROUP_STORAGE => Transfer::GROUP_STORAGE_RESOURCES, Transfer::GROUP_FUNCTIONS => Transfer::GROUP_FUNCTIONS_RESOURCES, Transfer::GROUP_MESSAGING => Transfer::GROUP_MESSAGING_RESOURCES, + Transfer::GROUP_BACKUPS => Transfer::GROUP_BACKUPS_RESOURCES, Transfer::GROUP_SITES => Transfer::GROUP_SITES_RESOURCES, ]; @@ -140,6 +146,9 @@ public function exportResources(array $resources): void case Transfer::GROUP_MESSAGING: $this->exportGroupMessaging($this->getMessagingBatchSize(), $resources); break; + case Transfer::GROUP_BACKUPS: + $this->exportGroupBackups($this->getBackupsBatchSize(), $resources); + break; case Transfer::GROUP_SITES: $this->exportGroupSites($this->getSitesBatchSize(), $resources); break; @@ -187,6 +196,14 @@ abstract protected function exportGroupFunctions(int $batchSize, array $resource */ abstract protected function exportGroupMessaging(int $batchSize, array $resources): void; + /** + * Export Backups Group + * + * @param int $batchSize + * @param array $resources Resources to export + */ + abstract protected function exportGroupBackups(int $batchSize, array $resources): void; + /** * Export Sites Group * diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index ca00f1be..a4058886 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -14,7 +14,6 @@ use Appwrite\Services\Users; use Utopia\Database\Database as UtopiaDatabase; use Utopia\Database\DateTime as UtopiaDateTime; -use Utopia\Database\Document as UtopiaDocument; use Utopia\Migration\Exception; use Utopia\Migration\Resource; use Utopia\Migration\Resources\Auth\Hash; @@ -91,10 +90,7 @@ class Appwrite extends Source private Sites $sites; - /** - * @var callable(UtopiaDocument $database|null): UtopiaDatabase - */ - protected mixed $getDatabasesDB; + private Reader $database; /** * @throws \Exception @@ -103,7 +99,7 @@ public function __construct( protected string $project, protected string $endpoint, protected string $key, - callable $getDatabasesDB, + ?callable $getDatabasesDB = null, protected string $source = self::SOURCE_API, protected ?UtopiaDatabase $dbForProject = null, protected array $queries = [], @@ -200,6 +196,9 @@ public static function getSupportedResources(): array Resource::TYPE_SITE_DEPLOYMENT, Resource::TYPE_SITE_VARIABLE, + // Backups + Resource::TYPE_BACKUP_POLICY, + // Settings ]; } @@ -239,6 +238,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', @@ -527,6 +527,13 @@ private function reportSites(array $resources, array &$report, array $resourceId } } + protected function reportBackups(array $resources, array &$report, array $resourceIds = []): void + { + if (\in_array(Resource::TYPE_BACKUP_POLICY, $resources)) { + $report[Resource::TYPE_BACKUP_POLICY] = 0; + } + } + /** * Export Auth Resources * @@ -1325,7 +1332,7 @@ private function exportFileData(File $file): void // Get the file size $fileSize = $file->getSize(); - if ($end >= $fileSize) { + if ($end > $fileSize) { $end = $fileSize - 1; } @@ -1349,7 +1356,7 @@ private function exportFileData(File $file): void $start += Transfer::STORAGE_MAX_CHUNK_SIZE; $end += Transfer::STORAGE_MAX_CHUNK_SIZE; - if ($end >= $fileSize) { + if ($end > $fileSize) { $end = $fileSize - 1; } } @@ -1561,8 +1568,8 @@ private function exportDeploymentData(Func $func, array $deployment): void $responseHeaders ); - // content-length header missing, file is less than max buffer size - if (!array_key_exists('content-length', $responseHeaders)) { + // Content-Length header was missing, file is less than max buffer size. + if (!array_key_exists('Content-Length', $responseHeaders)) { $file = $this->call( 'GET', "/functions/{$func->getId()}/deployments/{$deployment['$id']}/download", @@ -1571,7 +1578,7 @@ private function exportDeploymentData(Func $func, array $deployment): void $responseHeaders ); - $size = strlen($file); + $size = mb_strlen($file); if ($end > $size) { $end = $size - 1; @@ -1594,11 +1601,7 @@ private function exportDeploymentData(Func $func, array $deployment): void return; } - $fileSize = $responseHeaders['content-length']; - - if ($end >= $fileSize) { - $end = $fileSize - 1; - } + $fileSize = $responseHeaders['Content-Length']; $deployment = new Deployment( $deployment['$id'], @@ -1633,12 +1636,17 @@ private function exportDeploymentData(Func $func, array $deployment): void $start += Transfer::STORAGE_MAX_CHUNK_SIZE; $end += Transfer::STORAGE_MAX_CHUNK_SIZE; - if ($end >= $fileSize) { + if ($end > $fileSize) { $end = $fileSize - 1; } } } + protected function exportGroupBackups(int $batchSize, array $resources): void + { + // No-op: backup policies are Cloud-only + } + /** * @param array $resources * @param array $report @@ -2112,7 +2120,7 @@ private function exportSiteDeploymentData(Site $site, array $deployment): void $responseHeaders ); - if (!\array_key_exists('content-length', $responseHeaders)) { + if (!\array_key_exists('Content-Length', $responseHeaders)) { $file = $this->call( 'GET', "/sites/{$site->getId()}/deployments/{$deployment['$id']}/download", @@ -2121,7 +2129,7 @@ private function exportSiteDeploymentData(Site $site, array $deployment): void $responseHeaders ); - $size = strlen($file); + $size = mb_strlen($file); if ($end > $size) { $end = $size - 1; @@ -2143,11 +2151,7 @@ private function exportSiteDeploymentData(Site $site, array $deployment): void return; } - $fileSize = $responseHeaders['content-length']; - - if ($end >= $fileSize) { - $end = $fileSize - 1; - } + $fileSize = $responseHeaders['Content-Length']; $siteDeployment = new SiteDeployment( $deployment['$id'], @@ -2178,7 +2182,7 @@ private function exportSiteDeploymentData(Site $site, array $deployment): void $start += Transfer::STORAGE_MAX_CHUNK_SIZE; $end += Transfer::STORAGE_MAX_CHUNK_SIZE; - if ($end >= $fileSize) { + if ($end > $fileSize) { $end = $fileSize - 1; } } @@ -2508,7 +2512,7 @@ public static function getAttribute(Collection $collection, mixed $attribute): A /** * Build queries with optional filtering by resource IDs */ - private function buildQueries( + protected function buildQueries( string $resourceType, array $resourceIds, ?string $cursor = null, diff --git a/src/Migration/Sources/CSV.php b/src/Migration/Sources/CSV.php index 7d02090f..1dbdafa9 100644 --- a/src/Migration/Sources/CSV.php +++ b/src/Migration/Sources/CSV.php @@ -421,6 +421,14 @@ protected function exportGroupMessaging(int $batchSize, array $resources): void throw new \Exception('Not Implemented'); } + /** + * @throws \Exception + */ + protected function exportGroupBackups(int $batchSize, array $resources): void + { + throw new \Exception('Not Implemented'); + } + /** * @throws \Exception */ diff --git a/src/Migration/Sources/Firebase.php b/src/Migration/Sources/Firebase.php index 4495fb5b..73c9a179 100644 --- a/src/Migration/Sources/Firebase.php +++ b/src/Migration/Sources/Firebase.php @@ -814,6 +814,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'); + } + protected function exportGroupSites(int $batchSize, array $resources): void { throw new \Exception('Not implemented'); diff --git a/src/Migration/Sources/JSON.php b/src/Migration/Sources/JSON.php index 15e7b6aa..2abab762 100644 --- a/src/Migration/Sources/JSON.php +++ b/src/Migration/Sources/JSON.php @@ -201,14 +201,16 @@ protected function exportGroupFunctions(int $batchSize, array $resources): void throw new \Exception('Not Implemented'); } - /** - * @throws \Exception - */ 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 */ diff --git a/src/Migration/Sources/NHost.php b/src/Migration/Sources/NHost.php index 4aac49f8..2f9fad01 100644 --- a/src/Migration/Sources/NHost.php +++ b/src/Migration/Sources/NHost.php @@ -947,6 +947,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'); + } + protected function exportGroupSites(int $batchSize, array $resources): void { throw new \Exception('Not Implemented'); diff --git a/src/Migration/Transfer.php b/src/Migration/Transfer.php index 561ff283..4a5a5887 100644 --- a/src/Migration/Transfer.php +++ b/src/Migration/Transfer.php @@ -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, @@ -95,6 +97,10 @@ class Transfer Resource::TYPE_MESSAGE, ]; + public const GROUP_BACKUPS_RESOURCES = [ + Resource::TYPE_BACKUP_POLICY, + ]; + public const ALL_PUBLIC_RESOURCES = [ Resource::TYPE_USER, Resource::TYPE_TEAM, @@ -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 @@ 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'), }; } diff --git a/tests/Migration/Unit/Adapters/MockDestination.php b/tests/Migration/Unit/Adapters/MockDestination.php index fe1da710..3497888b 100644 --- a/tests/Migration/Unit/Adapters/MockDestination.php +++ b/tests/Migration/Unit/Adapters/MockDestination.php @@ -55,6 +55,7 @@ public static function getSupportedResources(): array Resource::TYPE_TOPIC, Resource::TYPE_SUBSCRIBER, Resource::TYPE_MESSAGE, + Resource::TYPE_BACKUP_POLICY, ]; } diff --git a/tests/Migration/Unit/Adapters/MockSource.php b/tests/Migration/Unit/Adapters/MockSource.php index 889eeb3c..3ecd137d 100644 --- a/tests/Migration/Unit/Adapters/MockSource.php +++ b/tests/Migration/Unit/Adapters/MockSource.php @@ -85,6 +85,9 @@ public static function getSupportedResources(): array Resource::TYPE_SUBSCRIBER, Resource::TYPE_MESSAGE, + // Backups + Resource::TYPE_BACKUP_POLICY, + // legacy Resource::TYPE_DOCUMENT, Resource::TYPE_ATTRIBUTE, @@ -182,6 +185,23 @@ protected function exportGroupMessaging(int $batchSize, array $resources): void } } + /** + * Export Backups Group + * + * @param int $batchSize Max 100 + * @param string[] $resources Resources to export + */ + 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); + } + } + /** * Export Sites Group *