Merge remote-tracking branch 'origin/feat-platform-db-access' into add-api-key-migration

# Conflicts:
#	src/Migration/Destinations/Appwrite.php
#	src/Migration/Sources/Appwrite.php
This commit is contained in:
Prem Palanisamy committed 2026-05-14 07:13:18 +01:00
commit 77ce2847b7
30 files changed
+2347 -733

No files matched your search

+4 -6
View File
@@ -22,12 +22,13 @@
"check": "./vendor/bin/phpstan analyse --level 3 src tests --memory-limit 2G"
},
"require": {
"php": ">=8.1",
"php": ">=8.2",
"ext-curl": "*",
"ext-openssl": "*",
"appwrite/appwrite": "19.*",
"appwrite/appwrite": "23.*",
"utopia-php/database": "5.*",
"utopia-php/storage": "1.0.*",
"utopia-php/storage": "2.*",
"utopia-php/dsn": "0.2.*",
"halaxa/json-machine": "^1.2"
},
@@ -38,9 +39,6 @@
"laravel/pint": "1.*",
"phpstan/phpstan": "1.*"
},
"platform": {
"php": "8.1"
},
"config": {
"allow-plugins": {
"php-http/discovery": true,
Generated
+201 -238
View File
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
-1
View File
@@ -316,5 +316,4 @@ class JSON extends Destination
return $this->convertValueToJSON($item);
}, $value);
}
}
@@ -0,0 +1,70 @@
<?php
namespace Utopia\Migration\Destinations;
enum SchemaAction
{
case Create;
case Skip;
case Overwrite;
}
enum OnDuplicate: string
{
case Fail = 'fail';
case Skip = 'skip';
case Overwrite = 'overwrite';
/**
* @return list<string>
*/
public static function values(): array
{
return \array_values(\array_map(fn (self $case) => $case->value, self::cases()));
}
/**
* Coarse routing for re-migration. The caller follows up with a spec-match
* check that overrides Overwrite to Skip when source and dest
* already have identical spec — see DestinationAppwrite for the full flow.
*/
public function resolveSchemaAction(
bool $exists,
?string $sourceUpdatedAt = null,
?string $destUpdatedAt = null,
): SchemaAction {
if (!$exists) {
return SchemaAction::Create;
}
return match ($this) {
self::Fail => SchemaAction::Create,
self::Skip => SchemaAction::Skip,
self::Overwrite => $this->sourceIsNewer($sourceUpdatedAt, $destUpdatedAt)
? SchemaAction::Overwrite
: SchemaAction::Skip,
};
}
private function sourceIsNewer(?string $source, ?string $dest): bool
{
$src = $this->parseTimestamp($source);
$dst = $this->parseTimestamp($dest);
return $src !== null && $dst !== null && $src > $dst;
}
/**
* strtotime accepts '0000-00-00' leniently (returns a large negative epoch,
* not false), so non-positive epochs are rejected too.
*/
private function parseTimestamp(?string $value): ?int
{
if ($value === null || $value === '') {
return null;
}
$epoch = \strtotime($value);
if ($epoch === false || $epoch <= 0) {
return null;
}
return $epoch;
}
}
+4
View File
@@ -78,6 +78,9 @@ abstract class Resource implements \JsonSerializable
public const TYPE_MESSAGE = 'message';
// Backups
public const TYPE_BACKUP_POLICY = 'backup-policy';
// legacy terminologies
public const TYPE_DOCUMENT = 'document';
public const TYPE_ATTRIBUTE = 'attribute';
@@ -115,6 +118,7 @@ abstract class Resource implements \JsonSerializable
self::TYPE_TOPIC,
self::TYPE_SUBSCRIBER,
self::TYPE_MESSAGE,
self::TYPE_BACKUP_POLICY,
// legacy
self::TYPE_DOCUMENT,
+115
View File
@@ -0,0 +1,115 @@
<?php
namespace Utopia\Migration\Resources\Backups;
use Utopia\Migration\Resource;
use Utopia\Migration\Transfer;
class Policy extends Resource
{
/**
* @param string $id
* @param string $name
* @param array<string> $services
* @param int $retention
* @param string $schedule
* @param bool $enabled
* @param string $resourceId
* @param string $resourceType
*/
public function __construct(
string $id = '',
private readonly string $name = '',
private readonly array $services = [],
private readonly int $retention = 0,
private readonly string $schedule = '',
private readonly bool $enabled = true,
private readonly string $resourceId = '',
private readonly string $resourceType = '',
) {
$this->id = $id;
}
/**
* @param array<string, mixed> $array
* @return self
*/
public static function fromArray(array $array): self
{
return new self(
$array['id'],
$array['name'] ?? '',
$array['services'] ?? [],
$array['retention'] ?? 0,
$array['schedule'] ?? '',
$array['enabled'] ?? true,
$array['resourceId'] ?? '',
$array['resourceType'] ?? '',
);
}
/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'services' => $this->services,
'retention' => $this->retention,
'schedule' => $this->schedule,
'enabled' => $this->enabled,
'resourceId' => $this->resourceId,
'resourceType' => $this->resourceType,
];
}
public static function getName(): string
{
return Resource::TYPE_BACKUP_POLICY;
}
public function getGroup(): string
{
return Transfer::GROUP_BACKUPS;
}
public function getPolicyName(): string
{
return $this->name;
}
/**
* @return array<string>
*/
public function getServices(): array
{
return $this->services;
}
public function getRetention(): int
{
return $this->retention;
}
public function getSchedule(): string
{
return $this->schedule;
}
public function getEnabled(): bool
{
return $this->enabled;
}
public function getResourceId(): string
{
return $this->resourceId;
}
public function getResourceType(): string
{
return $this->resourceType;
}
}
@@ -14,6 +14,7 @@ abstract class Column extends Resource
public const TYPE_LONGTEXT = 'longtext';
public const TYPE_INTEGER = 'integer';
public const TYPE_BIG_INT = 'bigint';
public const TYPE_FLOAT = 'double';
public const TYPE_BOOLEAN = 'boolean';
public const TYPE_DATETIME = 'datetime';
@@ -0,0 +1,108 @@
<?php
namespace Utopia\Migration\Resources\Database\Columns;
use Utopia\Migration\Resources\Database\Column;
use Utopia\Migration\Resources\Database\Table;
class BigInt extends Column
{
public function __construct(
string $key,
Table $table,
bool $required = false,
?int $default = null,
bool $array = false,
?int $min = null,
?int $max = null,
bool $signed = true,
string $createdAt = '',
string $updatedAt = ''
) {
$min ??= PHP_INT_MIN;
$max ??= PHP_INT_MAX;
$size = 8;
parent::__construct(
$key,
$table,
size: $size,
required: $required,
default: $default,
array: $array,
signed: $signed,
formatOptions: [
'min' => $min,
'max' => $max,
],
createdAt: $createdAt,
updatedAt: $updatedAt
);
}
/**
* @param array{
* key: string,
* collection?: array{
* database: array{
* id: string,
* name: string,
* },
* name: string,
* id: string,
* documentSecurity: bool,
* permissions: ?array<string>
* },
* table?: array{
* database: array{
* id: string,
* name: string,
* },
* name: string,
* id: string,
* rowSecurity: bool,
* permissions: ?array<string>
* },
* required: bool,
* array: bool,
* default: ?int,
* formatOptions: array{
* min: ?int,
* max: ?int
* },
* createdAt: string,
* updatedAt: string,
* } $array
* @return self
*/
public static function fromArray(array $array): self
{
return new self(
$array['key'],
Table::fromArray($array['table'] ?? $array['collection']),
required: $array['required'],
default: $array['default'],
array: $array['array'],
min: $array['formatOptions']['min'] ?? null,
max: $array['formatOptions']['max'] ?? null,
signed: $array['signed'] ?? true,
createdAt: $array['createdAt'] ?? '',
updatedAt: $array['updatedAt'] ?? '',
);
}
public function getType(): string
{
return Column::TYPE_BIG_INT;
}
public function getMin(): ?int
{
return (int)$this->formatOptions['min'];
}
public function getMax(): ?int
{
return (int)$this->formatOptions['max'];
}
}
@@ -26,7 +26,7 @@ class Database extends Resource
protected bool $enabled = true,
protected string $originalId = '',
protected string $type = '',
protected string $database = ''
protected ?string $database = ''
) {
$this->id = $id;
}
@@ -52,7 +52,7 @@ class Database extends Resource
enabled: $array['enabled'] ?? true,
originalId: $array['originalId'] ?? '',
type: $array['type'] ?? 'legacy',
database: $array['database']
database: $array['database'] ?? null
);
}
@@ -97,7 +97,7 @@ class Database extends Resource
return $this->type;
}
public function getDatabase(): string
public function getDatabase(): ?string
{
return $this->database;
}
+1 -1
View File
@@ -53,7 +53,7 @@ class Table extends Resource
Database::fromArray($array['database']),
name: $array['name'],
id: $array['id'],
rowSecurity: $array['rowSecurity'] ?? $array['documentSecurity'],
rowSecurity: (bool) ($array['rowSecurity'] ?? $array['documentSecurity'] ?? false),
permissions: $array['permissions'] ?? [],
createdAt: $array['createdAt'] ?? '',
updatedAt: $array['updatedAt'] ?? '',
+41 -2
View File
@@ -18,6 +18,10 @@ class Func extends Resource
* @param int $timeout
* @param string $activeDeployment
* @param string $entrypoint
* @param string $commands
* @param bool $logging
* @param array<string> $scopes
* @param string $specification
*/
public function __construct(
string $id,
@@ -29,7 +33,11 @@ class Func extends Resource
private readonly string $schedule = '',
private readonly int $timeout = 0,
private readonly string $activeDeployment = '',
private readonly string $entrypoint = ''
private readonly string $entrypoint = '',
private readonly string $commands = '',
private readonly bool $logging = true,
private readonly array $scopes = [],
private readonly string $specification = ''
) {
$this->id = $id;
}
@@ -50,7 +58,11 @@ class Func extends Resource
$array['schedule'] ?? '',
$array['timeout'] ?? 0,
$array['activeDeployment'] ?? '',
$array['entrypoint'] ?? ''
$array['entrypoint'] ?? '',
$array['commands'] ?? '',
$array['logging'] ?? true,
$array['scopes'] ?? [],
$array['specification'] ?? ''
);
}
@@ -70,6 +82,10 @@ class Func extends Resource
'timeout' => $this->timeout,
'activeDeployment' => $this->activeDeployment,
'entrypoint' => $this->entrypoint,
'commands' => $this->commands,
'logging' => $this->logging,
'scopes' => $this->scopes,
'specification' => $this->specification,
];
}
@@ -133,4 +149,27 @@ class Func extends Resource
{
return $this->entrypoint;
}
public function getCommands(): string
{
return $this->commands;
}
public function getLogging(): bool
{
return $this->logging;
}
/**
* @return array<string>
*/
public function getScopes(): array
{
return $this->scopes;
}
public function getSpecification(): string
{
return $this->specification;
}
}
-1
View File
@@ -67,5 +67,4 @@ class EnvVar extends Resource
{
return $this->value;
}
}
+10 -1
View File
@@ -19,6 +19,7 @@ class Bucket extends Resource
* @param bool $encryption
* @param bool $antiVirus
* @param bool $updateLimits
* @param bool $transformations
*/
public function __construct(
string $id = '',
@@ -32,6 +33,7 @@ class Bucket extends Resource
private readonly bool $encryption = false,
private readonly bool $antiVirus = false,
private readonly bool $updateLimits = false,
private readonly bool $transformations = false,
) {
$this->id = $id;
$this->permissions = $permissions;
@@ -54,7 +56,8 @@ class Bucket extends Resource
$array['compression'] ?? 'none',
$array['encryption'] ?? false,
$array['antiVirus'] ?? false,
$array['updateLimits'] ?? false
$array['updateLimits'] ?? false,
$array['transformations'] ?? false
);
}
@@ -74,6 +77,7 @@ class Bucket extends Resource
'encryption' => $this->encryption,
'antiVirus' => $this->antiVirus,
'updateLimits' => $this->updateLimits,
'transformations' => $this->transformations,
];
}
@@ -131,4 +135,9 @@ class Bucket extends Resource
{
return $this->antiVirus;
}
public function getTransformations(): bool
{
return $this->transformations;
}
}
+17
View File
@@ -51,6 +51,11 @@ abstract class Source extends Target
return static::$defaultBatchSize;
}
public function getBackupsBatchSize(): int
{
return static::$defaultBatchSize;
}
/**
* @param array<Resource> $resources
* @return void
@@ -115,6 +120,7 @@ abstract class Source extends Target
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,
];
foreach ($mapping as $group => $resources) {
@@ -152,6 +158,9 @@ abstract class Source extends Target
case Transfer::GROUP_INTEGRATIONS:
$this->exportGroupIntegrations($this->getIntegrationsBatchSize(), $resources);
break;
case Transfer::GROUP_BACKUPS:
$this->exportGroupBackups($this->getBackupsBatchSize(), $resources);
break;
}
}
}
@@ -211,4 +220,12 @@ abstract class Source extends Target
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupIntegrations(int $batchSize, array $resources): void;
/**
* Export Backups Group
*
* @param int $batchSize
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupBackups(int $batchSize, array $resources): void;
}
File diff suppressed because it is too large. Load diff
@@ -108,5 +108,4 @@ interface Reader
*/
public function queryLimit(int $limit): mixed;
public function getSupportForAttributes(): bool;
}
+53 -53
View File
@@ -4,21 +4,23 @@ namespace Utopia\Migration\Sources\Appwrite\Reader;
use Appwrite\AppwriteException;
use Appwrite\Query;
use Appwrite\Services\Databases;
/* use Appwrite\Services\Tables; */
use Appwrite\Services\TablesDB;
use Utopia\Migration\Resource;
use Utopia\Migration\Resources\Database\Database;
use Utopia\Migration\Resources\Database\Table;
use Utopia\Migration\Sources\Appwrite\Reader;
/**
* Adapts Appwrite SDK typed models to the Reader's plain-array contract so that
* downstream callers can use array-offset syntax interchangeably with the
* Database reader (which returns utopia-php Document objects).
*
* @implements Reader<Query>
*/
class API implements Reader
{
public function __construct(
private readonly Databases $database,
/* private readonly Tables $table, */
private readonly TablesDB $database,
) {
}
@@ -53,10 +55,10 @@ class API implements Reader
}
$databasesResponse = $this->database->list($databaseQueries);
$databases = $databasesResponse['databases'];
$databases = \array_map(fn ($database) => $database->toArray(), $databasesResponse->databases);
if (in_array(Resource::TYPE_DATABASE, $resources)) {
$report[Resource::TYPE_DATABASE] = $databasesResponse['total'];
$report[Resource::TYPE_DATABASE] = $databasesResponse->total;
}
if (count(array_intersect($resources, $relevantResources)) === 1 &&
@@ -73,13 +75,13 @@ class API implements Reader
$lastTable = null;
while (true) {
/* $currentTables = $this->tables->list(...); */
$currentTables = $this->database->listCollections(
$tablesResponse = $this->database->listTables(
$databaseId,
$lastTable
? [Query::cursorAfter($lastTable)]
: [Query::limit($pageLimit)]
)['collections']; /* ['tables'] */
);
$currentTables = \array_map(fn ($table) => $table->toArray(), $tablesResponse->tables);
$tables = \array_merge($tables, $currentTables);
$lastTable = $tables[count($tables) - 1]['$id'] ?? null;
@@ -108,14 +110,13 @@ class API implements Reader
}
if (Resource::isSupported(Resource::TYPE_ROW, $resources)) {
/* $rowsResponse = $this->tables->listRows(...) */
$rowsResponse = $this->database->listDocuments(
$rowsResponse = $this->database->listRows(
$databaseId,
$tableId,
[Query::limit(1)]
);
$report[Resource::TYPE_ROW] += $rowsResponse['total'];
$report[Resource::TYPE_ROW] += $rowsResponse->total;
}
}
}
@@ -125,90 +126,89 @@ class API implements Reader
}
/**
* @return array<array<string, mixed>>
* @throws AppwriteException
*/
public function listDatabases(array $queries = []): array
{
return $this->database->list($queries)['databases'];
return \array_map(
fn ($database) => $database->toArray(),
$this->database->list($queries)->databases
);
}
/**
* @return array<array<string, mixed>>
* @throws AppwriteException
*/
public function listTables(Database $resource, array $queries = []): array
{
/* $this->tables->list(...)['tables'] */
return $this->database->listCollections(
$resource->getId(),
$queries
)['collections'];
return \array_map(
fn ($table) => $table->toArray(),
$this->database->listTables($resource->getId(), $queries)->tables
);
}
/**
* @param Table $resource
* @param array $queries
* @return array
* @return array<array<string, mixed>>
* @throws AppwriteException
*/
public function listColumns(Table $resource, array $queries = []): array
{
/* $this->tables->listColumns(...)['columns'] */
return $this->database->listAttributes(
$resource->getDatabase()->getId(),
$resource->getId(),
$queries
)['attributes'];
return \array_map(
fn ($column) => $column->toArray(),
$this->database->listColumns(
$resource->getDatabase()->getId(),
$resource->getId(),
$queries
)->columns
);
}
/**
* @param Table $resource
* @param array $queries
* @return array
* @return array<array<string, mixed>>
* @throws AppwriteException
*/
public function listIndexes(Table $resource, array $queries = []): array
{
/* $this->tables->listIndexes(...)['indexes'] */
return $this->database->listIndexes(
$resource->getDatabase()->getId(),
$resource->getId(),
$queries
)['indexes'];
return \array_map(
fn ($index) => $index->toArray(),
$this->database->listIndexes(
$resource->getDatabase()->getId(),
$resource->getId(),
$queries
)->indexes
);
}
/**
* @param Table $resource
* @param array $queries
* @return array
* @return array<array<string, mixed>>
* @throws AppwriteException
*/
public function listRows(Table $resource, array $queries = []): array
{
/* $this->tables->listRows(...)['rows'] */
return $this->database->listDocuments(
$resource->getDatabase()->getId(),
$resource->getId(),
$queries
)['documents'];
return \array_map(
fn ($row) => $row->toArray(),
$this->database->listRows(
$resource->getDatabase()->getId(),
$resource->getId(),
$queries
)->rows
);
}
/**
* @param Table $resource
* @param string $rowId
* @param array $queries
* @return array
* @return array<string, mixed>
* @throws AppwriteException
*/
public function getRow(Table $resource, string $rowId, array $queries = []): array
{
/* $this->tables->getRow(...) */
return $this->database->getDocument(
return $this->database->getRow(
$resource->getDatabase()->getId(),
$resource->getId(),
$rowId,
$queries
);
)->toArray();
}
/**
+8 -2
View File
@@ -301,7 +301,7 @@ class CSV extends Source
$parsedData[$key] = array_map(function ($item) use ($type) {
return match ($type) {
Column::TYPE_INTEGER => is_numeric($item) ? (int)$item : null,
Column::TYPE_INTEGER,Column::TYPE_BIG_INT => is_numeric($item) ? (int)$item : null,
Column::TYPE_FLOAT => is_numeric($item) ? (float)$item : null,
Column::TYPE_BOOLEAN => filter_var($item, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
default => $item,
@@ -323,6 +323,7 @@ class CSV extends Source
'null' => null, // 'null' string is converted to null
'' => match ($type) {
Column::TYPE_INTEGER,
Column::TYPE_BIG_INT,
Column::TYPE_FLOAT,
Column::TYPE_BOOLEAN,
Column::TYPE_DATETIME,
@@ -330,7 +331,7 @@ class CSV extends Source
default => '', // but empty string stays empty string for compatibility
},
default => match ($type) {
Column::TYPE_INTEGER => \is_numeric($parsedValue) ? (int)$parsedValue : null,
Column::TYPE_INTEGER, Column::TYPE_BIG_INT => \is_numeric($parsedValue) ? (int)$parsedValue : null,
Column::TYPE_FLOAT => \is_numeric($parsedValue) ? (float)$parsedValue : null,
Column::TYPE_BOOLEAN => \filter_var(
$parsedValue,
@@ -434,6 +435,11 @@ class CSV extends Source
throw new \Exception('Not Implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}
/**
* @param callable(resource $stream, string $delimiter): void $callback
* @return void
+5
View File
@@ -822,4 +822,9 @@ class Firebase extends Source
{
throw new \Exception('Not implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not implemented');
}
}
+5
View File
@@ -209,6 +209,11 @@ class JSON extends Source
throw new \Exception('Not Implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}
/**
* @throws \Exception
*/
+7 -1
View File
@@ -8,6 +8,7 @@ use Utopia\Migration\Resource;
use Utopia\Migration\Resources\Auth\Hash;
use Utopia\Migration\Resources\Auth\User;
use Utopia\Migration\Resources\Database\Column;
use Utopia\Migration\Resources\Database\Columns\BigInt;
use Utopia\Migration\Resources\Database\Columns\Boolean;
use Utopia\Migration\Resources\Database\Columns\DateTime;
use Utopia\Migration\Resources\Database\Columns\Decimal;
@@ -679,7 +680,7 @@ class NHost extends Source
$column['column_default'] = null;
}
return new Integer(
return new BigInt(
$column['column_name'],
$table,
required: $column['is_nullable'] === 'NO',
@@ -956,4 +957,9 @@ class NHost extends Source
{
throw new \Exception('Not Implemented');
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
throw new \Exception('Not Implemented');
}
}
-1
View File
@@ -150,7 +150,6 @@ abstract class Target
throw new \Exception(\curl_error($ch), Exception::CODE_INTERNAL);
}
\curl_close($ch);
if ($responseStatus >= 400) {
if (\is_array($responseBody)) {
+8
View File
@@ -26,6 +26,8 @@ class Transfer
public const GROUP_MESSAGING = 'messaging';
public const GROUP_BACKUPS = 'backups';
public const GROUP_AUTH_RESOURCES = [
Resource::TYPE_USER,
Resource::TYPE_TEAM,
@@ -92,6 +94,10 @@ class Transfer
public const GROUP_SETTINGS_RESOURCES = [];
public const GROUP_BACKUPS_RESOURCES = [
Resource::TYPE_BACKUP_POLICY,
];
public const GROUP_MESSAGING_RESOURCES = [
Resource::TYPE_PROVIDER,
Resource::TYPE_TOPIC,
@@ -120,6 +126,7 @@ class Transfer
Resource::TYPE_TOPIC,
Resource::TYPE_SUBSCRIBER,
Resource::TYPE_MESSAGE,
Resource::TYPE_BACKUP_POLICY,
// Integrations
Resource::TYPE_PLATFORM,
@@ -409,6 +416,7 @@ class Transfer
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_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'),
};
}
-1
View File
@@ -124,7 +124,6 @@ abstract class Base extends TestCase
throw new \Exception(curl_error($ch));
}
curl_close($ch);
if ($responseStatus >= 400) {
if (is_array($responseBody)) {
@@ -86,6 +86,7 @@ class MockSource extends Source
Resource::TYPE_TOPIC,
Resource::TYPE_SUBSCRIBER,
Resource::TYPE_MESSAGE,
Resource::TYPE_BACKUP_POLICY,
// legacy
Resource::TYPE_DOCUMENT,
@@ -217,4 +218,15 @@ class MockSource extends Source
$this->handleResourceTransfer(Transfer::GROUP_INTEGRATIONS, $resource);
}
}
protected function exportGroupBackups(int $batchSize, array $resources): void
{
foreach (Transfer::GROUP_BACKUPS_RESOURCES as $resource) {
if (!\in_array($resource, $resources)) {
continue;
}
$this->handleResourceTransfer(Transfer::GROUP_BACKUPS, $resource);
}
}
}
@@ -0,0 +1,96 @@
<?php
namespace Utopia\Tests\Unit\Destinations;
use Appwrite\Services\Databases;
use Appwrite\Services\TablesDB;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionMethod;
use Utopia\Migration\Destinations\Appwrite as AppwriteDestination;
/**
* Lock-in for the SDK boundary that drives ATTRIBUTE_IMMUTABLE_FIELDS.
*
* Reflects on appwrite/appwrite's TablesDB + Databases services. If the SDK
* ships a new updateXColumn / updateXAttribute endpoint that exposes a
* previously-non-SDK field, this test fails so DestinationAppwrite's
* ATTRIBUTE_IMMUTABLE_FIELDS constant can be synced. Catches drift in CI
* rather than at runtime.
*/
class AppwriteAttributeSdkBoundaryTest extends TestCase
{
/** Routing/identity params that aren't mutable column state. */
private const IGNORED_PARAMS = ['databaseId', 'tableId', 'collectionId', 'key', 'newKey'];
/**
* SDK param name → meta-doc field name. Param names that don't appear on
* the meta doc as a top-level key map to null (e.g. min/max/elements live
* inside formatOptions; onDelete lives inside the relationship's options).
*
* @var array<string, string|null>
*/
private const PARAM_TO_META_FIELD = [
'xdefault' => 'default',
'min' => null,
'max' => null,
'elements' => null,
'onDelete' => null,
];
public function testSdkExposedFieldsAreNotInNonSdkConstant(): void
{
$sdkExposed = $this->collectSdkExposedMetaFields();
$nonSdk = $this->readNonSdkConstant();
$overlap = \array_intersect($sdkExposed, $nonSdk);
$this->assertSame(
[],
\array_values($overlap),
'ATTRIBUTE_IMMUTABLE_FIELDS marks these fields as non-SDK, but the appwrite/appwrite '
. 'SDK exposes them via updateXColumn/updateXAttribute: ' . \implode(', ', $overlap)
. '. Either drop these from ATTRIBUTE_IMMUTABLE_FIELDS or update PARAM_TO_META_FIELD '
. 'in this test if the SDK param doesn\'t correspond to a top-level meta-doc field.',
);
}
/**
* @return list<string> meta-doc field names the SDK can mutate
*/
private function collectSdkExposedMetaFields(): array
{
$fields = [];
foreach ([TablesDB::class, Databases::class] as $service) {
$service = new ReflectionClass($service);
foreach ($service->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if (!\preg_match('/^update.+(Column|Attribute)$/', $method->getName())) {
continue;
}
foreach ($method->getParameters() as $param) {
$name = $param->getName();
if (\in_array($name, self::IGNORED_PARAMS, true)) {
continue;
}
$mapped = \array_key_exists($name, self::PARAM_TO_META_FIELD)
? self::PARAM_TO_META_FIELD[$name]
: $name;
if ($mapped === null) {
continue;
}
$fields[$mapped] = true;
}
}
}
return \array_keys($fields);
}
/**
* @return list<string>
*/
private function readNonSdkConstant(): array
{
/** @var list<string> $value */
$value = (new ReflectionClass(AppwriteDestination::class))->getConstant('ATTRIBUTE_IMMUTABLE_FIELDS');
return $value;
}
}
@@ -0,0 +1,98 @@
<?php
namespace Utopia\Tests\Unit\Destinations;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Utopia\Database\Database as UtopiaDatabase;
use Utopia\Database\Document as UtopiaDocument;
use Utopia\Migration\Destinations\Appwrite as AppwriteDestination;
use Utopia\Migration\Destinations\OnDuplicate;
use Utopia\Migration\Resources\Database\Database as DatabaseResource;
/**
* Regression for PR #151: the destination must never write the source's DSN
* into `_databases.database`. With no resolver, the value must be blank so
* the runtime falls back to the destination project's DSN. With a resolver,
* the resolver's value must be written and the source's value ignored.
*
* Reproduces the comuneo-pre-production incident where post-migration
* `_databases.database` rows pointed at the source's host (db11) and
* destination reads hit `Table 'appwrite._<tenant>__metadata' doesn't exist`.
*/
class AppwriteDestinationDsnTest extends TestCase
{
public function testWithoutResolverReturnsEmptyString(): void
{
$destination = $this->makeDestination(getDatabaseDSN: null);
$resource = $this->makeResource(sourceDsn: 'database_db_fra1_self_hosted_11_0');
$resolved = $this->invokeResolver($destination, $resource);
$this->assertSame('', $resolved, 'Without a resolver the destination must not propagate the source DSN.');
}
public function testWithResolverUsesItsReturnValue(): void
{
$expected = 'appwrite://database_db_fra1_self_hosted_17_0?database=appwrite&namespace=_1';
$destination = $this->makeDestination(
getDatabaseDSN: fn (DatabaseResource $r): string => $expected,
);
$resource = $this->makeResource(sourceDsn: 'database_db_fra1_self_hosted_11_0');
$resolved = $this->invokeResolver($destination, $resource);
$this->assertSame($expected, $resolved);
$this->assertNotSame($resource->getDatabase(), $resolved, 'Source DSN must not leak through the resolver path.');
}
public function testResolverReceivesTheResource(): void
{
$captured = null;
$destination = $this->makeDestination(
getDatabaseDSN: function (DatabaseResource $r) use (&$captured): string {
$captured = $r;
return 'resolved';
},
);
$resource = $this->makeResource(sourceDsn: 'src');
$this->invokeResolver($destination, $resource);
$this->assertSame($resource, $captured);
}
private function makeDestination(?callable $getDatabaseDSN): AppwriteDestination
{
return new AppwriteDestination(
project: 'destination-project',
endpoint: 'http://example.test/v1',
key: 'test-key',
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,
);
}
private function makeResource(string $sourceDsn): DatabaseResource
{
return new DatabaseResource(
id: 'src-database',
name: 'src',
type: 'legacy',
database: $sourceDsn,
);
}
private function invokeResolver(AppwriteDestination $destination, DatabaseResource $resource): string
{
$method = (new ReflectionClass(AppwriteDestination::class))->getMethod('resolveDestinationDsn');
/** @var string $value */
$value = $method->invoke($destination, $resource);
return $value;
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Migration\Unit\General;
namespace Utopia\Tests\Unit\General;
use PHPUnit\Framework\TestCase;
use Utopia\Migration\Cache;
@@ -0,0 +1,155 @@
<?php
namespace Utopia\Tests\Unit\General;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Utopia\Migration\Destinations\OnDuplicate;
use Utopia\Migration\Destinations\SchemaAction;
/**
* OnDuplicate::resolveSchemaAction is the load-bearing decision point for
* re-migration tolerance. The destination then runs a spec-match guard that
* overrides Overwrite to Skip when source and dest already have
* identical spec — see DestinationAppwrite. These tests lock the
* mode × existence × updatedAt matrix.
*/
class OnDuplicateTest extends TestCase
{
public function testNotExistsAlwaysCreates(): void
{
foreach (OnDuplicate::cases() as $mode) {
$this->assertSame(
SchemaAction::Create,
$mode->resolveSchemaAction(exists: false),
"{$mode->value} on non-existing resource must return Create",
);
}
}
public function testFailExistsReturnsCreateSoCallerDDLThrows(): void
{
// Fail is routed through Create (not Skip) so the caller's normal
// create flow runs and the library surfaces DuplicateException as
// designed. Returning Skip here would silently hide the error.
$this->assertSame(
SchemaAction::Create,
OnDuplicate::Fail->resolveSchemaAction(exists: true),
);
}
public function testSkipAlwaysSkipsExisting(): void
{
// Skip must ignore updatedAt entirely — it's the "don't touch" contract.
$this->assertSame(
SchemaAction::Skip,
OnDuplicate::Skip->resolveSchemaAction(
exists: true,
sourceUpdatedAt: '2026-01-01T00:00:00.000+00:00',
destUpdatedAt: '2020-01-01T00:00:00.000+00:00',
),
);
$this->assertSame(
SchemaAction::Skip,
OnDuplicate::Skip->resolveSchemaAction(
exists: true,
sourceUpdatedAt: '2020-01-01T00:00:00.000+00:00',
destUpdatedAt: '2026-01-01T00:00:00.000+00:00',
),
);
}
public function testOverwriteSourceNewerUpdatesInPlace(): void
{
// Source updatedAt strictly newer than dest → Overwrite. The
// caller (DestinationAppwrite) follows up with attribute/index spec
// checks and falls through to drop+recreate when the SDK can't
// express the change.
$this->assertSame(
SchemaAction::Overwrite,
OnDuplicate::Overwrite->resolveSchemaAction(
exists: true,
sourceUpdatedAt: '2026-04-23T10:00:00.000+00:00',
destUpdatedAt: '2026-04-23T09:59:59.000+00:00',
),
);
}
public function testOverwriteSourceEqualSkips(): void
{
$when = '2026-04-23T10:00:00.000+00:00';
$this->assertSame(
SchemaAction::Skip,
OnDuplicate::Overwrite->resolveSchemaAction(
exists: true,
sourceUpdatedAt: $when,
destUpdatedAt: $when,
),
);
}
public function testOverwriteSourceOlderSkips(): void
{
// Dest is ahead — don't roll back. Avoids overwriting newer
// destination edits with stale source data.
$this->assertSame(
SchemaAction::Skip,
OnDuplicate::Overwrite->resolveSchemaAction(
exists: true,
sourceUpdatedAt: '2026-04-23T09:00:00.000+00:00',
destUpdatedAt: '2026-04-23T10:00:00.000+00:00',
),
);
}
public function testOverwriteNoTimestampsSkips(): void
{
// No timestamps provided at all → no information to act on. Conservative:
// Skip rather than risk a destructive update on uncertain input.
$this->assertSame(
SchemaAction::Skip,
OnDuplicate::Overwrite->resolveSchemaAction(exists: true),
);
}
/**
* @return array<string, array{0: ?string, 1: ?string}>
*/
public static function unparseableTimestampPairs(): array
{
return [
'both empty' => ['', ''],
'both null' => [null, null],
'source empty' => ['', '2026-04-23T10:00:00.000+00:00'],
'dest empty' => ['2026-04-23T10:00:00.000+00:00', ''],
'source null' => [null, '2026-04-23T10:00:00.000+00:00'],
'dest null' => ['2026-04-23T10:00:00.000+00:00', null],
'source zero-date' => ['0000-00-00 00:00:00', '2026-04-23T10:00:00.000+00:00'],
'dest zero-date' => ['2026-04-23T10:00:00.000+00:00', '0000-00-00 00:00:00'],
'source garbage' => ['not-a-date', '2026-04-23T10:00:00.000+00:00'],
'dest garbage' => ['2026-04-23T10:00:00.000+00:00', 'not-a-date'],
];
}
#[DataProvider('unparseableTimestampPairs')]
public function testOverwriteUnparseableUpdatedAtSkips(?string $source, ?string $dest): void
{
// Conservative: unparseable updatedAt preserves existing destination
// rather than risk a destructive update on garbage input.
$this->assertSame(
SchemaAction::Skip,
OnDuplicate::Overwrite->resolveSchemaAction(
exists: true,
sourceUpdatedAt: $source,
destUpdatedAt: $dest,
),
);
}
public function testValuesListsAllCasesInDeclarationOrder(): void
{
// The values() helper is consumed by API/SDK param validators; this
// protects against an accidental case-rename or reorder.
$this->assertSame(['fail', 'skip', 'overwrite'], OnDuplicate::values());
}
}