-
Notifications
You must be signed in to change notification settings - Fork 4
Add platform migration support #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
06ea808
666f015
4939c67
12a4d8e
8684335
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ | |
| use Utopia\Migration\Resources\Functions\Deployment; | ||
| use Utopia\Migration\Resources\Functions\EnvVar; | ||
| use Utopia\Migration\Resources\Functions\Func; | ||
| use Utopia\Migration\Resources\Integrations\Platform; | ||
| use Utopia\Migration\Resources\Messaging\Message; | ||
| use Utopia\Migration\Resources\Messaging\Provider; | ||
| use Utopia\Migration\Resources\Messaging\Subscriber; | ||
|
|
@@ -99,7 +100,8 @@ public function __construct( | |
| string $key, | ||
| protected UtopiaDatabase $dbForProject, | ||
| callable $getDatabasesDB, | ||
| protected array $collectionStructure | ||
| protected array $collectionStructure, | ||
| protected UtopiaDatabase $dbForPlatform, | ||
| ) { | ||
| $this->project = $project; | ||
| $this->endpoint = $endpoint; | ||
|
|
@@ -169,6 +171,9 @@ public static function getSupportedResources(): array | |
| Resource::TYPE_SITE, | ||
| Resource::TYPE_SITE_DEPLOYMENT, | ||
| Resource::TYPE_SITE_VARIABLE, | ||
|
|
||
| // Integrations | ||
| Resource::TYPE_PLATFORM, | ||
| ]; | ||
| } | ||
|
|
||
|
|
@@ -281,7 +286,6 @@ public function report(array $resources = [], array $resourceIds = []): array | |
| $scope = 'sites.write'; | ||
| $this->sites->create('', '', Framework::OTHER(), BuildRuntime::STATIC1()); | ||
| } | ||
|
|
||
| } catch (AppwriteException $e) { | ||
| if ($e->getCode() === 403) { | ||
| throw new \Exception( | ||
|
|
@@ -317,6 +321,7 @@ protected function import(array $resources, callable $callback): void | |
|
|
||
| try { | ||
| $this->dbForProject->setPreserveDates(true); | ||
| $this->dbForPlatform?->setPreserveDates(true); | ||
|
|
||
| $responseResource = match ($resource->getGroup()) { | ||
| Transfer::GROUP_DATABASES => $this->importDatabaseResource($resource, $isLast), | ||
|
|
@@ -325,6 +330,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_INTEGRATIONS => $this->importIntegrationsResource($resource), | ||
| default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION), | ||
| }; | ||
| } catch (\Throwable $e) { | ||
|
|
@@ -342,6 +348,7 @@ protected function import(array $resources, callable $callback): void | |
| $responseResource = $resource; | ||
| } finally { | ||
| $this->dbForProject->setPreserveDates(false); | ||
| $this->dbForPlatform?->setPreserveDates(false); | ||
| } | ||
|
|
||
| $this->cache->update($responseResource); | ||
|
|
@@ -1071,7 +1078,6 @@ protected function createRecord(Row $resource, bool $isLast): bool | |
| 'database_' . $databaseInternalId . '_collection_' . $tableInternalId, | ||
| $this->rowBuffer | ||
| )); | ||
|
|
||
| } finally { | ||
| $this->rowBuffer = []; | ||
| } | ||
|
|
@@ -2189,6 +2195,66 @@ private function importSiteDeployment(SiteDeployment $deployment): Resource | |
| return $deployment; | ||
| } | ||
|
|
||
| /** | ||
| * @throws \Exception | ||
| */ | ||
| public function importIntegrationsResource(Resource $resource): Resource | ||
| { | ||
| switch ($resource->getName()) { | ||
| case Resource::TYPE_PLATFORM: | ||
| /** @var Platform $resource */ | ||
| $this->createPlatform($resource); | ||
| break; | ||
| } | ||
|
|
||
| if ($resource->getStatus() !== Resource::STATUS_SKIPPED) { | ||
| $resource->setStatus(Resource::STATUS_SUCCESS); | ||
| } | ||
|
|
||
| return $resource; | ||
| } | ||
|
|
||
| /** | ||
| * @throws \Throwable | ||
| */ | ||
| protected function createPlatform(Platform $resource): bool | ||
| { | ||
| $existing = $this->dbForPlatform->findOne('platforms', [ | ||
| Query::equal('projectId', [$this->project]), | ||
| Query::equal('type', [$resource->getType()]), | ||
| Query::equal('name', [$resource->getPlatformName()]), | ||
| ]); | ||
|
|
||
| if ($existing !== false && !$existing->isEmpty()) { | ||
| $resource->setStatus(Resource::STATUS_SKIPPED, 'Platform already exists'); | ||
| return false; | ||
| } | ||
|
|
||
| $createdAt = $this->normalizeDateTime($resource->getCreatedAt()); | ||
| $updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt); | ||
|
|
||
| try { | ||
| $this->dbForPlatform->createDocument('platforms', new UtopiaDocument([ | ||
| '$id' => ID::unique(), | ||
| '$permissions' => $resource->getPermissions(), | ||
| 'projectInternalId' => $this->dbForPlatform->getDocument('projects', $this->project)->getInternalId(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify that getInternalId() is used correctly for project documents
ast-grep --pattern $'$_->getDocument($_, $_)->getInternalId()'
rg -n 'getInternalId\(\)' --type=php -C2Repository: utopia-php/migration Length of output: 768 🏁 Script executed: # Check for Document class definition and getInternalId method
find . -type f -name "*.php" | xargs grep -l "class Document"Repository: utopia-php/migration Length of output: 162 🏁 Script executed: # Search for getInternalId method definition
rg -n "function getInternalId|public.*getInternalId" --type=phpRepository: utopia-php/migration Length of output: 46 🏁 Script executed: # Check for getSequence usage to understand when it's used
rg -n "getSequence\(\)" --type=php -C2 | head -50Repository: utopia-php/migration Length of output: 2909 🏁 Script executed: # Look at the dbForPlatform->getDocument usage in the same file
rg -n "getDocument\(" src/Migration/Destinations/Appwrite.php | head -20Repository: utopia-php/migration Length of output: 1112 🏁 Script executed: # Read the Document class to verify available methods
cat -n src/Migration/Resources/Database/Document.php | head -100Repository: utopia-php/migration Length of output: 1712 🏁 Script executed: # Check for all public methods in Document class
rg -n "public.*function" src/Migration/Resources/Database/Document.phpRepository: utopia-php/migration Length of output: 170 🏁 Script executed: # Read the Row class to check for getInternalId and other methods
cat -n src/Migration/Resources/Database/Row.phpRepository: utopia-php/migration Length of output: 2991 🏁 Script executed: # Read the Resource class to check for getInternalId and getSequence
cat -n src/Migration/Resource.phpRepository: utopia-php/migration Length of output: 9627 🏁 Script executed: # Check composer.json for dependencies
cat composer.json | grep -A 20 '"require"'Repository: utopia-php/migration Length of output: 599 🏁 Script executed: # Search for dbForPlatform initialization and type hints to understand what it returns
rg -n "dbForPlatform" src/Migration/Destinations/Appwrite.php -B2 -A2 | head -40Repository: utopia-php/migration Length of output: 1552 🏁 Script executed: # Search for other references to 'projects' document to understand its structure
rg -n "getDocument\('projects'" --type=php -A2 -B2Repository: utopia-php/migration Length of output: 604 🏁 Script executed: # Search for getInternalId in the entire repo to understand its usage pattern
rg -n "getInternalId" --type=php -B3 -A3Repository: utopia-php/migration Length of output: 1505 🏁 Script executed: # Check if there's a Project resource class that might have getInternalId
find . -type f -name "*.php" | xargs grep -l "class Project"Repository: utopia-php/migration Length of output: 48 🏁 Script executed: # Verify the line context around 2240 to understand what method should be used
sed -n '2230,2250p' src/Migration/Destinations/Appwrite.phpRepository: utopia-php/migration Length of output: 1026 🏁 Script executed: # Check if utopia-php/database Document class has getInternalId method by examining any documentation or type hints
rg -n "import.*Document|use.*Document" src/Migration/Destinations/Appwrite.phpRepository: utopia-php/migration Length of output: 292 🏁 Script executed: # Check if there are any other patterns where we retrieve and use project document properties
rg -n "projects.*project\)" src/Migration/Destinations/Appwrite.php -A3 -B1 | head -50Repository: utopia-php/migration Length of output: 654 🏁 Script executed: # Search for any documentation or comments about projectInternalId
rg -n "projectInternalId" --type=php -B3 -A3Repository: utopia-php/migration Length of output: 833 🏁 Script executed: # Check the git history or comments around line 2240 to understand intent
git log -p --follow -S "projectInternalId" -- src/Migration/Destinations/Appwrite.php 2>/dev/null | head -100Repository: utopia-php/migration Length of output: 46 🏁 Script executed: # Look at the broader context of the platforms document structure
rg -n "platforms.*createDocument" src/Migration/Destinations/Appwrite.php -B5 -A15Repository: utopia-php/migration Length of output: 46 🏁 Script executed: # Check the imports to understand what's available from utopia-php/database
grep -r "use Utopia" src/Migration/Destinations/Appwrite.php | head -20Repository: utopia-php/migration Length of output: 944 🏁 Script executed: # Look for any cached documents or other getDocument calls to see what methods are used on them
rg -n "getDocument\(" src/Migration/Destinations/Appwrite.php -A1 | grep -E "(getId|getSequence|getInternalId|get[A-Z])" | head -20Repository: utopia-php/migration Length of output: 1349 Replace The code calls Line 2240🤖 Prompt for AI Agents |
||
| 'projectId' => $this->project, | ||
| 'type' => $resource->getType(), | ||
| 'name' => $resource->getPlatformName(), | ||
| 'key' => $resource->getKey(), | ||
| 'store' => $resource->getStore(), | ||
| 'hostname' => $resource->getHostname(), | ||
| '$createdAt' => $createdAt, | ||
| '$updatedAt' => $updatedAt, | ||
| ])); | ||
| } catch (DuplicateException) { | ||
| $resource->setStatus(Resource::STATUS_SKIPPED, 'Platform already exists'); | ||
| return false; | ||
| } | ||
|
|
||
| $this->dbForPlatform->purgeCachedDocument('projects', $this->project); | ||
|
|
||
| return true; | ||
| private function validateFieldsForIndexes(Index $resource, UtopiaDocument $table, array &$lengths) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| { | ||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Migration\Resources\Integrations; | ||
|
|
||
| use Utopia\Migration\Resource; | ||
| use Utopia\Migration\Transfer; | ||
|
|
||
| class Platform extends Resource | ||
| { | ||
| /** | ||
| * @param string $id | ||
| * @param string $type | ||
| * @param string $name | ||
| * @param string $key | ||
| * @param string $store | ||
| * @param string $hostname | ||
| * @param string $createdAt | ||
| * @param string $updatedAt | ||
| */ | ||
| public function __construct( | ||
| string $id, | ||
| private readonly string $type, | ||
| private readonly string $name, | ||
| private readonly string $key = '', | ||
| private readonly string $store = '', | ||
| private readonly string $hostname = '', | ||
| string $createdAt = '', | ||
| string $updatedAt = '', | ||
| ) { | ||
| $this->id = $id; | ||
| $this->createdAt = $createdAt; | ||
| $this->updatedAt = $updatedAt; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $array | ||
| * @return self | ||
| */ | ||
| public static function fromArray(array $array): self | ||
| { | ||
| return new self( | ||
| $array['id'], | ||
| $array['type'], | ||
| $array['name'], | ||
| $array['key'] ?? '', | ||
| $array['store'] ?? '', | ||
| $array['hostname'] ?? '', | ||
| createdAt: $array['createdAt'] ?? '', | ||
| updatedAt: $array['updatedAt'] ?? '', | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function jsonSerialize(): array | ||
| { | ||
| return [ | ||
| 'id' => $this->id, | ||
| 'type' => $this->type, | ||
| 'name' => $this->name, | ||
| 'key' => $this->key, | ||
| 'store' => $this->store, | ||
| 'hostname' => $this->hostname, | ||
| 'createdAt' => $this->createdAt, | ||
| 'updatedAt' => $this->updatedAt, | ||
| ]; | ||
| } | ||
|
|
||
| public static function getName(): string | ||
| { | ||
| return Resource::TYPE_PLATFORM; | ||
| } | ||
|
|
||
| public function getGroup(): string | ||
| { | ||
| return Transfer::GROUP_INTEGRATIONS; | ||
| } | ||
|
|
||
| public function getType(): string | ||
| { | ||
| return $this->type; | ||
| } | ||
|
|
||
| public function getPlatformName(): string | ||
| { | ||
| return $this->name; | ||
| } | ||
|
|
||
| public function getKey(): string | ||
| { | ||
| return $this->key; | ||
| } | ||
|
|
||
| public function getStore(): string | ||
| { | ||
| return $this->store; | ||
| } | ||
|
|
||
| public function getHostname(): string | ||
| { | ||
| return $this->hostname; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fail fast on unsupported integration resource types.
importIntegrationsResource()has nodefaultbranch, so unknown integration types can be markedSTATUS_SUCCESSwithout any import action.💡 Proposed fix
public function importIntegrationsResource(Resource $resource): Resource { switch ($resource->getName()) { case Resource::TYPE_PLATFORM: /** `@var` Platform $resource */ $this->createPlatform($resource); break; + default: + throw new \Exception('Unknown integrations resource type: ' . $resource->getName(), Exception::CODE_VALIDATION); } if ($resource->getStatus() !== Resource::STATUS_SKIPPED) { $resource->setStatus(Resource::STATUS_SUCCESS); } return $resource; }🤖 Prompt for AI Agents