Skip to content
Merged
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 thread
premtsd-code marked this conversation as resolved.

/**
* @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(),
Comment thread
premtsd-code marked this conversation as resolved.
Outdated
'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
Comment thread
abnegate marked this conversation as resolved.
{
/**
* @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