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

// Integrations
Resource::TYPE_PLATFORM,
];
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -1071,7 +1078,6 @@ protected function createRecord(Row $resource, bool $isLast): bool
'database_' . $databaseInternalId . '_collection_' . $tableInternalId,
$this->rowBuffer
));

} finally {
$this->rowBuffer = [];
}
Expand Down Expand Up @@ -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;
}
Comment on lines +2202 to +2216
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 | 🟠 Major

Fail fast on unsupported integration resource types.

importIntegrationsResource() has no default branch, so unknown integration types can be marked STATUS_SUCCESS without 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
Verify each finding against the current code and only fix it if needed.

In `@src/Migration/Destinations/Appwrite.php` around lines 2202 - 2216, The switch
in importIntegrationsResource(Resource $resource) only handles
Resource::TYPE_PLATFORM and then unconditionally sets status to STATUS_SUCCESS
if not STATUS_SKIPPED, allowing unsupported types to be marked success; add a
default branch (or explicit check after the switch) that fails fast for unknown
resource names by setting the resource to a failure status (or throwing an
exception) and/or logging an error, referencing importIntegrationsResource(),
Resource::TYPE_PLATFORM, Resource::STATUS_SKIPPED and Resource::STATUS_SUCCESS
so unsupported integration types are never silently marked successful.


/**
* @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(),
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 | 🔴 Critical

🧩 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 -C2

Repository: 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=php

Repository: 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 -50

Repository: 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 -20

Repository: 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 -100

Repository: 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.php

Repository: 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.php

Repository: utopia-php/migration

Length of output: 2991


🏁 Script executed:

# Read the Resource class to check for getInternalId and getSequence
cat -n src/Migration/Resource.php

Repository: 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 -40

Repository: 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 -B2

Repository: 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 -A3

Repository: 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.php

Repository: 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.php

Repository: 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 -50

Repository: utopia-php/migration

Length of output: 654


🏁 Script executed:

# Search for any documentation or comments about projectInternalId
rg -n "projectInternalId" --type=php -B3 -A3

Repository: 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 -100

Repository: 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 -A15

Repository: 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 -20

Repository: 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 -20

Repository: utopia-php/migration

Length of output: 1349


Replace getInternalId() with getSequence() on the project document.

The code calls getInternalId() which does not exist on Utopia\Database\Document. The correct method is getSequence(), as consistently used throughout the codebase for retrieving internal/sequential IDs from documents.

Line 2240
'projectInternalId' => $this->dbForPlatform->getDocument('projects', $this->project)->getSequence(),
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Migration/Destinations/Appwrite.php` at line 2240, The project document
access uses a non-existent method getInternalId(); replace that call with
getSequence() on the Document returned by
$this->dbForPlatform->getDocument('projects', $this->project) so the assignment
for 'projectInternalId' uses ->getSequence() instead of ->getInternalId();
update the expression where 'projectInternalId' is set (in the
Destinations\Appwrite migration code) to call getSequence().

'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)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Migration/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ abstract class Resource implements \JsonSerializable

public const TYPE_ENVIRONMENT_VARIABLE = 'environment-variable';

// Integrations
public const TYPE_PLATFORM = 'platform';
public const TYPE_SUBSCRIBER = 'subscriber';

public const TYPE_MESSAGE = 'message';
Expand Down Expand Up @@ -106,6 +108,7 @@ abstract class Resource implements \JsonSerializable
self::TYPE_ENVIRONMENT_VARIABLE,
self::TYPE_TEAM,
self::TYPE_MEMBERSHIP,
self::TYPE_PLATFORM,
self::TYPE_PROVIDER,
self::TYPE_TOPIC,
self::TYPE_SUBSCRIBER,
Expand Down
104 changes: 104 additions & 0 deletions src/Migration/Resources/Integrations/Platform.php
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;
}
}
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 getIntegrationsBatchSize(): 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_INTEGRATIONS => Transfer::GROUP_INTEGRATIONS_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_INTEGRATIONS:
$this->exportGroupIntegrations($this->getIntegrationsBatchSize(), $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 Integrations Group
*
* @param int $batchSize
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupIntegrations(int $batchSize, array $resources): void;
}
Loading
Loading