From 9e54ff0c20f0f765d89bbda1cfb94dab8a9a6fcb Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 14 May 2026 09:25:38 +0100 Subject: [PATCH] Drop console-key fetch from Appwrite source/destination Source uses Project SDK (listKeys with cursor pagination); destination writes keys directly via dbForPlatform, matching the platform pattern. --- src/Migration/Destinations/Appwrite.php | 92 +++++++----------- src/Migration/Sources/Appwrite.php | 124 ++++++++++++------------ 2 files changed, 98 insertions(+), 118 deletions(-) diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 3a29e8eac9..7c5ffe7638 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -105,11 +105,6 @@ class Appwrite extends Destination */ protected $getDatabasesDB; - private bool $consoleKeyFetched = false; - - private ?string $consoleKey = null; - - /** * Resolves the DSN written into the destination's `_databases.database` * for a migrated database. When the source and destination projects don't @@ -232,33 +227,6 @@ class Appwrite extends Destination $this->processedTwoWayPairs = []; } - /** - * @return array|null - */ - protected function getConsoleHeaders(): ?array - { - if (!$this->consoleKeyFetched) { - $this->consoleKeyFetched = true; - - try { - $response = $this->call('POST', '/migrations/appwrite/console-key'); - $this->consoleKey = $response['key'] ?? null; - } catch (\Throwable) { - $this->consoleKey = null; - } - } - - if ($this->consoleKey === null) { - return null; - } - - return [ - 'Content-Type' => 'application/json', - 'x-appwrite-project' => 'console', - 'x-appwrite-key' => $this->consoleKey, - ]; - } - public static function getName(): string { return 'Appwrite'; @@ -3169,39 +3137,49 @@ class Appwrite extends Destination protected function createApiKey(ApiKey $resource): bool { - $consoleHeaders = $this->getConsoleHeaders(); + $existing = $this->dbForPlatform->findOne('keys', [ + Query::equal('projectInternalId', [$this->projectInternalId]), + Query::equal('name', [$resource->getApiKeyName()]), + ]); - if ($consoleHeaders === null) { - throw new \Exception('Failed to get console headers'); + if ($existing !== false && !$existing->isEmpty()) { + $resource->setStatus(Resource::STATUS_SKIPPED, 'API key already exists'); + return false; } + $createdAt = $this->normalizeDateTime($resource->getCreatedAt()); + $updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt); + $expire = $resource->getExpire(); + try { - $params = [ - 'keyId' => ID::unique(), + $this->dbForPlatform->createDocument('keys', new UtopiaDocument([ + '$id' => ID::unique(), + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + 'projectInternalId' => $this->projectInternalId, + 'projectId' => $this->project, + 'resourceInternalId' => $this->projectInternalId, + 'resourceId' => $this->project, + 'resourceType' => 'projects', 'name' => $resource->getApiKeyName(), 'scopes' => $resource->getScopes(), - ]; - - $expire = $resource->getExpire(); - if (!empty($expire)) { - $params['expire'] = $expire; - } - - $this->call( - 'POST', - '/projects/' . $this->project . '/keys', - $consoleHeaders, - $params - ); - } catch (\Throwable $e) { - if ($e->getCode() === 409) { - $resource->setStatus(Resource::STATUS_SKIPPED, 'API key already exists'); - return false; - } - - throw $e; + 'expire' => empty($expire) ? null : $expire, + 'sdks' => $resource->getSdks(), + 'accessedAt' => null, + 'secret' => 'standard_' . \bin2hex(\random_bytes(128)), + '$createdAt' => $createdAt, + '$updatedAt' => $updatedAt, + ])); + } catch (DuplicateException) { + $resource->setStatus(Resource::STATUS_SKIPPED, 'API key already exists'); + return false; } + $this->dbForPlatform->purgeCachedDocument('projects', $this->project); + return true; } diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index b3054221a0..a219652045 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -2243,16 +2243,15 @@ class Appwrite extends Source } if (\in_array(Resource::TYPE_API_KEY, $resources)) { - $consoleHeaders = $this->getConsoleHeaders(); - if ($consoleHeaders === null) { + $keyQueries = $this->buildQueries( + resourceType: Resource::TYPE_API_KEY, + resourceIds: $resourceIds, + limit: 1 + ); + try { + $report[Resource::TYPE_API_KEY] = $this->project->listKeys($keyQueries)->total; + } catch (\Throwable) { $report[Resource::TYPE_API_KEY] = 0; - } else { - try { - $response = $this->call('GET', '/projects/' . $this->project . '/keys', $consoleHeaders); - $report[Resource::TYPE_API_KEY] = $response['total'] ?? 0; - } catch (\Throwable) { - $report[Resource::TYPE_API_KEY] = 0; - } } } } @@ -2302,37 +2301,17 @@ class Appwrite extends Source } if (\in_array(Resource::TYPE_API_KEY, $resources)) { - $this->exportWithConsoleHeaders( - Resource::TYPE_API_KEY, - Transfer::GROUP_INTEGRATIONS, - $this->exportApiKeys(...) - ); - } - } - - protected function exportWithConsoleHeaders(string $resourceType, string $group, callable $callback): void - { - $consoleHeaders = $this->getConsoleHeaders(); - - if ($consoleHeaders === null) { - $this->addError(new Exception( - $resourceType, - $group, - message: 'Console key unavailable for source instance', - )); - return; - } - - try { - $callback($consoleHeaders); - } catch (\Throwable $e) { - $this->addError(new Exception( - $resourceType, - $group, - message: $e->getMessage(), - code: $e->getCode(), - previous: $e - )); + try { + $this->exportApiKeys($batchSize); + } catch (\Throwable $e) { + $this->addError(new Exception( + Resource::TYPE_API_KEY, + Transfer::GROUP_INTEGRATIONS, + message: $e->getMessage(), + code: $e->getCode(), + previous: $e + )); + } } } @@ -2417,30 +2396,53 @@ class Appwrite extends Source } } - private function exportApiKeys(array $consoleHeaders): void + /** + * @throws AppwriteException + */ + private function exportApiKeys(int $batchSize): void { - $response = $this->call('GET', '/projects/' . $this->project . '/keys', $consoleHeaders); + $lastId = null; - if (empty($response['keys'])) { - return; + while (true) { + $queries = [Query::limit($batchSize)]; + + if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_API_KEY) { + $queries[] = Query::equal('$id', $this->rootResourceId); + $queries[] = Query::limit(1); + } + + if ($lastId !== null) { + $queries[] = Query::cursorAfter($lastId); + } + + $response = $this->project->listKeys($queries); + if ($response->total === 0) { + break; + } + + $apiKeys = []; + + foreach ($response->keys as $key) { + $apiKeys[] = new ApiKey( + $key->id, + $key->name, + $key->scopes, + $key->expire, + $key->accessedAt, + $key->sdks, + createdAt: $key->createdAt, + updatedAt: $key->updatedAt, + ); + + $lastId = $key->id; + } + + $this->callback($apiKeys); + + if (\count($response->keys) < $batchSize) { + break; + } } - - $apiKeys = []; - - foreach ($response['keys'] as $key) { - $apiKeys[] = new ApiKey( - $key['$id'] ?? '', - $key['name'] ?? '', - $key['scopes'] ?? [], - $key['expire'] ?? '', - $key['accessedAt'] ?? '', - $key['sdks'] ?? [], - createdAt: $key['$createdAt'] ?? '', - updatedAt: $key['$updatedAt'] ?? '', - ); - } - - $this->callback($apiKeys); } /**