Merge pull request #154 from utopia-php/feat-platform-db-access

Add platform migration support
This commit is contained in:
Jake Barnby authored and GitHub committed 2026-05-14 19:30:09 +12:00
commit 3ee6e12af2
13 files changed
+351 -6

No files matched your search

+71
View File
@@ -50,6 +50,7 @@ use Utopia\Migration\Resources\Database\Table;
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;
@@ -161,6 +162,8 @@ class Appwrite extends Destination
protected UtopiaDatabase $dbForProject,
callable $getDatabasesDB,
protected array $collectionStructure,
protected UtopiaDatabase $dbForPlatform,
protected string $projectInternalId,
protected OnDuplicate $onDuplicate = OnDuplicate::Fail,
?callable $getDatabaseDSN = null,
) {
@@ -270,6 +273,9 @@ class Appwrite extends Destination
Resource::TYPE_SITE_DEPLOYMENT,
Resource::TYPE_SITE_VARIABLE,
// Integrations
Resource::TYPE_PLATFORM,
// Backups
Resource::TYPE_BACKUP_POLICY,
];
@@ -419,6 +425,7 @@ class Appwrite extends Destination
try {
$this->dbForProject->setPreserveDates(true);
$this->dbForPlatform->setPreserveDates(true);
$responseResource = match ($resource->getGroup()) {
Transfer::GROUP_DATABASES => $this->importDatabaseResource($resource, $isLast),
@@ -427,6 +434,7 @@ class Appwrite extends Destination
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),
};
@@ -445,6 +453,7 @@ class Appwrite extends Destination
$responseResource = $resource;
} finally {
$this->dbForProject->setPreserveDates(false);
$this->dbForPlatform->setPreserveDates(false);
}
$this->cache->update($responseResource);
@@ -3055,6 +3064,68 @@ class Appwrite extends Destination
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->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;
}
private function validateFieldsForIndexes(Index $resource, UtopiaDocument $table, array &$lengths)
{
/**
+3
View File
@@ -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';
@@ -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,
@@ -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
View File
@@ -46,6 +46,11 @@ abstract class Source extends Target
return static::$defaultBatchSize;
}
public function getIntegrationsBatchSize(): int
{
return static::$defaultBatchSize;
}
public function getBackupsBatchSize(): int
{
return static::$defaultBatchSize;
@@ -114,6 +119,7 @@ abstract class Source extends Target
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,
];
@@ -149,6 +155,9 @@ abstract class Source extends Target
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;
@@ -204,6 +213,14 @@ abstract class Source extends Target
*/
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
*
+106 -4
View File
@@ -7,6 +7,7 @@ use Appwrite\Client;
use Appwrite\Query;
use Appwrite\Services\Functions;
use Appwrite\Services\Messaging;
use Appwrite\Services\Project;
use Appwrite\Services\Sites;
use Appwrite\Services\Storage;
use Appwrite\Services\TablesDB;
@@ -54,6 +55,7 @@ use Utopia\Migration\Resources\Database\VectorsDB;
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;
@@ -92,6 +94,8 @@ class Appwrite extends Source
private Sites $sites;
private Project $project;
/**
* @var callable(UtopiaDocument $database|null): UtopiaDatabase
*/
@@ -101,7 +105,7 @@ class Appwrite extends Source
* @throws \Exception
*/
public function __construct(
protected string $project,
protected string $projectId,
protected string $endpoint,
protected string $key,
callable $getDatabasesDB,
@@ -111,7 +115,7 @@ class Appwrite extends Source
) {
$this->client = (new Client())
->setEndpoint($endpoint)
->setProject($project)
->setProject($projectId)
->setKey($key);
$this->users = new Users($this->client);
@@ -120,8 +124,9 @@ class Appwrite extends Source
$this->functions = new Functions($this->client);
$this->messaging = new Messaging($this->client);
$this->sites = new Sites($this->client);
$this->project = new Project($this->client);
$this->headers['x-appwrite-project'] = $this->project;
$this->headers['x-appwrite-project'] = $this->projectId;
$this->headers['x-appwrite-key'] = $this->key;
$this->getDatabasesDB = $getDatabasesDB;
@@ -138,7 +143,7 @@ class Appwrite extends Source
$this->reader = new DatabaseReader(
$this->dbForProject,
$this->getDatabasesDB,
$this->project
$this->projectId
);
break;
@@ -200,6 +205,9 @@ class Appwrite extends Source
Resource::TYPE_SITE_DEPLOYMENT,
Resource::TYPE_SITE_VARIABLE,
// Integrations
Resource::TYPE_PLATFORM,
// Backups
Resource::TYPE_BACKUP_POLICY,
@@ -242,6 +250,7 @@ class Appwrite extends Source
$this->reportFunctions($resources, $report, $resourceIds);
$this->reportMessaging($resources, $report, $resourceIds);
$this->reportSites($resources, $report, $resourceIds);
$this->reportIntegrations($resources, $report, $resourceIds);
$this->reportBackups($resources, $report, $resourceIds);
$report['version'] = $this->call(
@@ -2211,6 +2220,27 @@ class Appwrite extends Source
}
}
/**
* @param array<string> $resources
* @param array<string, int> $report
* @param array<string, array<string>> $resourceIds
*/
private function reportIntegrations(array $resources, array &$report, array $resourceIds = []): void
{
if (\in_array(Resource::TYPE_PLATFORM, $resources)) {
$platformQueries = $this->buildQueries(
resourceType: Resource::TYPE_PLATFORM,
resourceIds: $resourceIds,
limit: 1
);
try {
$report[Resource::TYPE_PLATFORM] = $this->project->listPlatforms($platformQueries)->total;
} catch (\Throwable) {
$report[Resource::TYPE_PLATFORM] = 0;
}
}
}
/**
* @param string $databaseType
* @param array $database {
@@ -2235,6 +2265,27 @@ class Appwrite extends Source
}
}
/**
* @param int $batchSize
* @param array<string> $resources
*/
protected function exportGroupIntegrations(int $batchSize, array $resources): void
{
if (\in_array(Resource::TYPE_PLATFORM, $resources)) {
try {
$this->exportPlatforms($batchSize);
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_PLATFORM,
Transfer::GROUP_INTEGRATIONS,
message: $e->getMessage(),
code: $e->getCode(),
previous: $e
));
}
}
}
/**
* eg., tables,collections
* @param string $databaseType
@@ -2265,6 +2316,57 @@ class Appwrite extends Source
}
}
/**
* @throws AppwriteException
*/
private function exportPlatforms(int $batchSize): void
{
$lastId = null;
while (true) {
$queries = [Query::limit($batchSize)];
if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_PLATFORM) {
$queries[] = Query::equal('$id', $this->rootResourceId);
$queries[] = Query::limit(1);
}
if ($lastId !== null) {
$queries[] = Query::cursorAfter($lastId);
}
$response = $this->project->listPlatforms($queries);
if ($response->total === 0) {
break;
}
$platforms = [];
// SDK returns the raw platforms array (mixed PlatformWeb/Apple/Android/etc payloads),
// so we read the unified REST fields directly rather than via typed accessors.
foreach ($response->platforms as $platform) {
$platforms[] = new Platform(
$platform['$id'] ?? '',
$platform['type'] ?? '',
$platform['name'] ?? '',
$platform['key'] ?? '',
$platform['store'] ?? '',
$platform['hostname'] ?? '',
createdAt: $platform['$createdAt'] ?? '',
updatedAt: $platform['$updatedAt'] ?? '',
);
$lastId = $platform['$id'] ?? null;
}
$this->callback($platforms);
if (\count($response->platforms) < $batchSize) {
break;
}
}
}
/**
* eg.,documents/attributes
* @param string $databaseType
+5
View File
@@ -430,6 +430,11 @@ class CSV extends Source
throw new \Exception('Not Implemented');
}
protected function exportGroupIntegrations(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
+5
View File
@@ -818,6 +818,11 @@ class Firebase extends Source
throw new \Exception('Not implemented');
}
protected function exportGroupIntegrations(int $batchSize, array $resources): void
{
throw new \Exception('Not implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not implemented');
+5
View File
@@ -222,6 +222,11 @@ class JSON extends Source
throw new \Exception('Not Implemented');
}
protected function exportGroupIntegrations(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}
/**
* @param callable(Items): void $callback
* @throws \Exception|JsonMachineException
+5
View File
@@ -953,6 +953,11 @@ class NHost extends Source
throw new \Exception('Not Implemented');
}
protected function exportGroupIntegrations(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
+9 -2
View File
@@ -22,7 +22,7 @@ class Transfer
public const GROUP_DATABASES_VECTOR_DB = 'vectorsdb';
public const GROUP_SETTINGS = 'settings';
public const GROUP_INTEGRATIONS = 'integrations';
public const GROUP_MESSAGING = 'messaging';
@@ -60,6 +60,9 @@ class Transfer
Resource::TYPE_ROW,
];
public const GROUP_INTEGRATIONS_RESOURCES = [
Resource::TYPE_PLATFORM,
];
public const GROUP_DOCUMENTSDB_RESOURCES = [
Resource::TYPE_DATABASE_DOCUMENTSDB,
Resource::TYPE_COLLECTION,
@@ -124,6 +127,9 @@ class Transfer
Resource::TYPE_MESSAGE,
Resource::TYPE_BACKUP_POLICY,
// Integrations
Resource::TYPE_PLATFORM,
// legacy
Resource::TYPE_DOCUMENT,
Resource::TYPE_ATTRIBUTE,
@@ -139,6 +145,7 @@ class Transfer
Resource::TYPE_SITE,
Resource::TYPE_USER,
Resource::TYPE_TEAM,
Resource::TYPE_PLATFORM,
Resource::TYPE_PROVIDER,
Resource::TYPE_TOPIC,
Resource::TYPE_MESSAGE,
@@ -401,10 +408,10 @@ class Transfer
self::GROUP_GENERAL => array_merge($resources, []),
self::GROUP_AUTH => array_merge($resources, self::GROUP_AUTH_RESOURCES),
self::GROUP_DATABASES => array_merge($resources, self::GROUP_DATABASES_RESOURCES),
self::GROUP_INTEGRATIONS => array_merge($resources, self::GROUP_INTEGRATIONS_RESOURCES),
self::GROUP_DATABASES_TABLES_DB => array_merge($resources, self::GROUP_TABLESDB_RESOURCES),
self::GROUP_DATABASES_DOCUMENTS_DB => array_merge($resources, self::GROUP_DOCUMENTSDB_RESOURCES),
self::GROUP_DATABASES_VECTOR_DB => array_merge($resources, self::GROUP_VECTORSDB_RESOURCES),
self::GROUP_SETTINGS => array_merge($resources, self::GROUP_SETTINGS_RESOURCES),
self::GROUP_MESSAGING => array_merge($resources, self::GROUP_MESSAGING_RESOURCES),
self::GROUP_BACKUPS => array_merge($resources, self::GROUP_BACKUPS_RESOURCES),
default => throw new \Exception('No service group found'),
@@ -51,6 +51,7 @@ class MockDestination extends Destination
Resource::TYPE_ENVIRONMENT_VARIABLE,
Resource::TYPE_TEAM,
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_PLATFORM,
Resource::TYPE_PROVIDER,
Resource::TYPE_TOPIC,
Resource::TYPE_SUBSCRIBER,
@@ -80,6 +80,7 @@ class MockSource extends Source
Resource::TYPE_ENVIRONMENT_VARIABLE,
Resource::TYPE_TEAM,
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_PLATFORM,
Resource::TYPE_PROVIDER,
Resource::TYPE_TOPIC,
Resource::TYPE_SUBSCRIBER,
@@ -200,6 +201,23 @@ class MockSource extends Source
}
}
/**
* Export Integrations Group
*
* @param int $batchSize Max 100
* @param string[] $resources Resources to export
*/
protected function exportGroupIntegrations(int $batchSize, array $resources): void
{
foreach (Transfer::GROUP_INTEGRATIONS_RESOURCES as $resource) {
if (!\in_array($resource, $resources)) {
continue;
}
$this->handleResourceTransfer(Transfer::GROUP_INTEGRATIONS, $resource);
}
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
foreach (Transfer::GROUP_BACKUPS_RESOURCES as $resource) {
@@ -71,6 +71,8 @@ class AppwriteDestinationDsnTest extends TestCase
dbForProject: $this->createStub(UtopiaDatabase::class),
getDatabasesDB: fn (UtopiaDocument $database): UtopiaDatabase => $this->createStub(UtopiaDatabase::class),
collectionStructure: ['attributes' => [], 'indexes' => []],
dbForPlatform: $this->createStub(UtopiaDatabase::class),
projectInternalId: '1',
onDuplicate: OnDuplicate::Fail,
getDatabaseDSN: $getDatabaseDSN,
);