Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
71 changes: 71 additions & 0 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 @@ -161,6 +162,8 @@ public function __construct(
protected UtopiaDatabase $dbForProject,
callable $getDatabasesDB,
protected array $collectionStructure,
protected UtopiaDatabase $dbForPlatform,
protected string $projectInternalId,
protected OnDuplicate $onDuplicate = OnDuplicate::Fail,
?callable $getDatabaseDSN = null,
) {
Expand Down Expand Up @@ -270,6 +273,9 @@ public static function getSupportedResources(): array
Resource::TYPE_SITE_DEPLOYMENT,
Resource::TYPE_SITE_VARIABLE,

// Integrations
Resource::TYPE_PLATFORM,

// Backups
Resource::TYPE_BACKUP_POLICY,
];
Expand Down Expand Up @@ -419,6 +425,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 @@ -427,6 +434,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),
Transfer::GROUP_BACKUPS => $this->importBackupResource($resource),
default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION),
};
Expand All @@ -445,6 +453,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 @@ -3055,6 +3064,68 @@ 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->projectInternalId,
'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;
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Comment thread
premtsd-code marked this conversation as resolved.

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 @@ -109,6 +111,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;
}

public function getBackupsBatchSize(): int
{
return static::$defaultBatchSize;
Expand Down Expand Up @@ -114,6 +119,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,
Transfer::GROUP_BACKUPS => Transfer::GROUP_BACKUPS_RESOURCES,
];

Expand Down Expand Up @@ -149,6 +155,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;
case Transfer::GROUP_BACKUPS:
$this->exportGroupBackups($this->getBackupsBatchSize(), $resources);
break;
Expand Down Expand Up @@ -204,6 +213,14 @@ abstract protected function exportGroupMessaging(int $batchSize, array $resource
*/
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;

/**
* Export Backups Group
*
Expand Down
Loading
Loading