refactor(migrations): separate root and child resources

This commit is contained in:
Jake Barnby committed 2026-07-20 22:11:36 +12:00
1 parent 6212284150
commit 3cfe4dca6a
18 files changed
+1104 -575

No files matched your search

+79
View File
@@ -2267,6 +2267,17 @@ return [
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('resourceInternalId'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('resourceType'),
'type' => Database::VAR_STRING,
@@ -2278,6 +2289,39 @@ return [
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('parentResourceId'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('parentResourceInternalId'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('parentResourceType'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
],
'indexes' => [
[
@@ -2308,6 +2352,41 @@ return [
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_DESC],
],
[
'$id' => '_key_resourceType',
'type' => Database::INDEX_KEY,
'attributes' => ['resourceType'],
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_ASC],
],
[
'$id' => '_key_resourceInternalId',
'type' => Database::INDEX_KEY,
'attributes' => ['resourceInternalId'],
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_ASC],
],
[
'$id' => '_key_parentResourceId',
'type' => Database::INDEX_KEY,
'attributes' => ['parentResourceId'],
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_ASC],
],
[
'$id' => '_key_parentResourceType',
'type' => Database::INDEX_KEY,
'attributes' => ['parentResourceType'],
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_ASC],
],
[
'$id' => '_key_parentResourceInternalId',
'type' => Database::INDEX_KEY,
'attributes' => ['parentResourceInternalId'],
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_ASC],
],
[
'$id' => ID::custom('_fulltext_search'),
'type' => Database::INDEX_FULLTEXT,
+8
View File
@@ -34,6 +34,7 @@ use Appwrite\Utopia\Request\Filters\V23 as RequestV23;
use Appwrite\Utopia\Request\Filters\V24 as RequestV24;
use Appwrite\Utopia\Request\Filters\V25 as RequestV25;
use Appwrite\Utopia\Request\Filters\V26 as RequestV26;
use Appwrite\Utopia\Request\Filters\V27 as RequestV27;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Filters\V16 as ResponseV16;
use Appwrite\Utopia\Response\Filters\V17 as ResponseV17;
@@ -46,6 +47,7 @@ use Appwrite\Utopia\Response\Filters\V23 as ResponseV23;
use Appwrite\Utopia\Response\Filters\V24 as ResponseV24;
use Appwrite\Utopia\Response\Filters\V25 as ResponseV25;
use Appwrite\Utopia\Response\Filters\V26 as ResponseV26;
use Appwrite\Utopia\Response\Filters\V27 as ResponseV27;
use Appwrite\Utopia\View;
use Executor\Exception\Timeout as ExecutorTimeout;
use Executor\Executor;
@@ -917,6 +919,9 @@ Http::init()
if (version_compare($requestFormat, '1.9.5', '<')) {
$request->addFilter(new RequestV26());
}
if (version_compare($requestFormat, '1.9.6', '<')) {
$request->addFilter(new RequestV27());
}
}
$localeParam = (string) $request->getParam('locale', $request->getHeaderLine('x-appwrite-locale', ''));
@@ -941,6 +946,9 @@ Http::init()
*/
$responseFormat = $request->getHeaderLine('x-appwrite-response-format', System::getEnv('_APP_SYSTEM_RESPONSE_FORMAT', ''));
if ($responseFormat) {
if (version_compare($responseFormat, '1.9.6', '<')) {
$response->addFilter(new ResponseV27());
}
if (version_compare($responseFormat, '1.9.5', '<')) {
$response->addFilter(new ResponseV26());
}
+12 -14
View File
@@ -40,11 +40,11 @@
"repositories": [
{
"type": "vcs",
"url": "https://github.com/utopia-php/auth"
"url": "https://github.com/utopia-php/migration"
}
],
"require": {
"php": ">=8.5.0",
"php": ">=8.3.0",
"ext-curl": "*",
"ext-imagick": "*",
"ext-mbstring": "*",
@@ -62,14 +62,14 @@
"utopia-php/abuse": "1.4.*",
"utopia-php/agents": "2.*",
"utopia-php/analytics": "0.15.*",
"utopia-php/audit": "2.9.*",
"utopia-php/auth": "^0.10",
"utopia-php/cache": "^3.0.0",
"utopia-php/audit": "2.6.*",
"utopia-php/auth": "^0.7",
"utopia-php/cache": "^3.0",
"utopia-php/cli": "^0.24",
"utopia-php/compression": "0.1.*",
"utopia-php/config": "1.*",
"utopia-php/console": "0.1.*",
"utopia-php/database": "^6.0.0",
"utopia-php/database": "6.0.*",
"utopia-php/detector": "0.2.*",
"utopia-php/domains": "^2.2",
"utopia-php/emails": "0.7.*",
@@ -77,24 +77,24 @@
"utopia-php/dsn": "0.2.1",
"utopia-php/http": "^2.0@RC",
"utopia-php/fetch": "^1.1",
"utopia-php/validators": "0.3.*",
"utopia-php/validators": "0.2.*",
"utopia-php/image": "0.8.*",
"utopia-php/locale": "0.8.*",
"utopia-php/lock": "0.2.*",
"utopia-php/logger": "0.8.*",
"utopia-php/messaging": "^2.0",
"utopia-php/migration": "1.17.*",
"utopia-php/messaging": "^1.2",
"utopia-php/migration": "dev-feat/drop-compound-resource-id#a7e94c86a96fc7cdfff3eae9845270d91f83e7f8 as 1.17.3",
"utopia-php/platform": "^1.0@RC",
"utopia-php/pools": "1.*",
"utopia-php/span": "3.0.*",
"utopia-php/preloader": "0.2.*",
"utopia-php/queue": "^0.22",
"utopia-php/queue": "^0.21",
"utopia-php/servers": "0.4.*",
"utopia-php/registry": "0.5.*",
"utopia-php/storage": "2.*",
"utopia-php/system": "0.10.*",
"utopia-php/telemetry": "^0.4",
"utopia-php/vcs": "^4.4.2",
"utopia-php/vcs": "4.*",
"utopia-php/websocket": "1.0.*",
"matomo/device-detector": "6.4.*",
"dragonmantank/cron-expression": "3.4.*",
@@ -104,9 +104,7 @@
"spomky-labs/otphp": "11.*",
"webonyx/graphql-php": "15.32.*",
"league/csv": "9.14.*",
"enshrined/svg-sanitize": "0.22.*",
"utopia-php/client": "^0.2.2",
"open-runtimes/sdk-for-php": "^0.4.0"
"enshrined/svg-sanitize": "0.22.*"
},
"require-dev": {
"ext-fileinfo": "*",
Generated
+355 -399
View File
File diff suppressed because it is too large. Load diff
+117 -76
View File
@@ -3,12 +3,11 @@
namespace Appwrite\Migration\Version;
use Appwrite\Migration\Migration;
use Exception;
use Throwable;
use Utopia\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Migration\Resource;
class V25 extends Migration
{
@@ -19,92 +18,134 @@ class V25 extends Migration
{
Console::info('Migrating collections');
$this->migrateCollections();
Console::info('Migrating documents');
$this->forEachDocument($this->migrateDocument(...));
}
/**
* Migrate Collections.
*
* @return void
* @throws Exception|Throwable
* @throws Throwable
*/
private function migrateCollections(): void
{
$projectInternalId = $this->project->getSequence();
if (empty($projectInternalId)) {
throw new Exception('Project ID is null');
if ($this->project->getSequence() === 'console') {
return;
}
$collectionType = match ($projectInternalId) {
'console' => 'console',
default => 'projects',
};
$id = 'migrations';
$collections = $this->collections[$collectionType];
$this->dbForProject->purgeCachedCollection($id);
$this->dbForProject->purgeCachedDocument(Database::METADATA, $id);
foreach ($collections as $collection) {
$id = $collection['$id'];
$attributes = [
'resourceInternalId',
'parentResourceId',
'parentResourceInternalId',
'parentResourceType',
];
try {
$this->createAttributesFromCollection($this->dbForProject, $id, $attributes);
} catch (Throwable $th) {
Console::warning('Failed to create attributes "' . \implode(', ', $attributes) . "\" in collection {$id}: {$th->getMessage()}");
}
if (empty($id)) {
continue;
}
Console::log("Migrating collection \"{$id}\"");
$this->dbForProject->purgeCachedCollection($id);
$this->dbForProject->purgeCachedDocument(Database::METADATA, $id);
switch ($id) {
case 'projects':
if ($collectionType === 'console') {
try {
$this->createIndexFromCollection($this->dbForProject, $id, '_key_accessedAt');
} catch (Throwable $th) {
Console::warning("Failed to create index \"_key_accessedAt\" from {$id}: {$th->getMessage()}");
}
}
$this->dbForProject->purgeCachedCollection($id);
break;
case 'installations':
if ($collectionType === 'console') {
foreach (['personalAccessToken', 'personalRefreshToken'] as $attribute) {
try {
$this->dbForProject->updateAttribute($id, $attribute, size: 2048);
} catch (Throwable $th) {
Console::warning("Failed to resize attribute \"{$attribute}\" in collection {$id}: {$th->getMessage()}");
}
}
}
$this->dbForProject->purgeCachedCollection($id);
break;
case 'databases':
if ($collectionType === 'projects') {
try {
$this->createAttributeFromCollection($this->dbForProject, $id, 'status');
} catch (Throwable $th) {
Console::warning("Failed to create attribute \"status\" in collection {$id}: {$th->getMessage()}");
}
$this->dbForProject->purgeCachedCollection($id);
// Backfill existing databases so the stored value matches the intended default.
// Materialize the matched documents before updating any of them: updating
// "status" removes rows from the isNull() set, and the offset-based iterator
// would otherwise skip un-processed rows as the filtered set shrinks mid-scan.
$databases = \iterator_to_array($this->documentsIterator($id, [Query::isNull('status')]));
foreach ($databases as $database) {
try {
$this->dbForProject->updateDocument($id, $database->getId(), new Document([
'status' => 'ready',
]));
} catch (Throwable $th) {
Console::warning("Failed to backfill \"status\" for database {$database->getId()}: {$th->getMessage()}");
}
}
}
break;
$indexes = [
'_key_resourceType',
'_key_resourceInternalId',
'_key_parentResourceId',
'_key_parentResourceType',
'_key_parentResourceInternalId',
];
foreach ($indexes as $index) {
try {
$this->createIndexFromCollection($this->dbForProject, $id, $index);
} catch (Throwable $th) {
Console::warning("Failed to create index \"{$index}\" from {$id}: {$th->getMessage()}");
}
}
$this->dbForProject->purgeCachedCollection($id);
}
protected function migrateDocument(Document $document): Document
{
if ($document->getCollection() !== 'migrations') {
return $document;
}
$resourceId = (string) $document->getAttribute('resourceId', '');
$parentResourceId = (string) $document->getAttribute('parentResourceId', '');
if ($parentResourceId === '') {
if (!\str_contains($resourceId, ':')) {
return $document;
}
[$parentResourceId, $resourceId] = \explode(':', $resourceId, 2);
if ($parentResourceId === '' || $resourceId === '') {
return $document;
}
$parentResourceType = (string) $document->getAttribute('resourceType', '');
$document
->setAttribute('resourceId', $resourceId)
->setAttribute('resourceType', Resource::TYPE_COLLECTION)
->setAttribute('parentResourceId', $parentResourceId);
if ($parentResourceType !== '') {
$document->setAttribute('parentResourceType', $parentResourceType);
}
}
if ($resourceId === '') {
return $document;
}
$internalIds = $this->resolveInternalIds($parentResourceId, $resourceId, $document->getId());
if (
$document->getAttribute('parentResourceInternalId', '') === ''
&& isset($internalIds['parentResourceInternalId'])
) {
$document->setAttribute('parentResourceInternalId', $internalIds['parentResourceInternalId']);
}
if (
$document->getAttribute('resourceInternalId', '') === ''
&& isset($internalIds['resourceInternalId'])
) {
$document->setAttribute('resourceInternalId', $internalIds['resourceInternalId']);
}
return $document;
}
/**
* @return array{parentResourceInternalId?: string, resourceInternalId?: string}
*/
protected function resolveInternalIds(string $parentResourceId, string $resourceId, string $migrationId): array
{
try {
$database = $this->dbForProject->getDocument('databases', $parentResourceId);
if ($database->isEmpty() || $database->getSequence() === '') {
return [];
}
} catch (Throwable $th) {
Console::warning("Failed to resolve parent internal ID for migration {$migrationId}: {$th->getMessage()}");
return [];
}
$internalIds = [
'parentResourceInternalId' => (string) $database->getSequence(),
];
try {
$resource = $this->dbForProject->getDocument('database_' . $database->getSequence(), $resourceId);
if (!$resource->isEmpty() && $resource->getSequence() !== '') {
$internalIds['resourceInternalId'] = (string) $resource->getSequence();
}
} catch (Throwable $th) {
Console::warning("Failed to resolve resource internal ID for migration {$migrationId}: {$th->getMessage()}");
}
return $internalIds;
}
}
@@ -9,7 +9,6 @@ use Appwrite\Extend\Exception;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Database\Validator\CompoundUID;
use Appwrite\Utopia\Response;
use Utopia\Database\Database;
use Utopia\Database\Document;
@@ -18,6 +17,7 @@ use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\Queries\Documents;
use Utopia\Database\Validator\UID;
use Utopia\Migration\Resource;
use Utopia\Migration\Sources\Appwrite as AppwriteSource;
use Utopia\Migration\Sources\CSV;
@@ -60,7 +60,8 @@ class Create extends Action
)
]
))
->param('resourceId', null, new CompoundUID(), 'Composite ID in the format {databaseId:collectionId}, identifying a collection within a database to export.')
->param('databaseId', '', new UID(), 'Database ID containing the source collection.')
->param('collectionId', '', new UID(), 'Collection ID to export documents from.')
->param('filename', '', new Text(255), 'The name of the file to be created for the export, excluding the .csv extension.')
->param('columns', [], new ArrayList(new Text(Database::LENGTH_KEY)), 'List of attributes to export. If empty, all attributes will be exported. You can use the `*` wildcard to export all attributes from the collection.', true)
->param('queries', [], new ArrayList(new Text(0)), 'Array of query strings generated using the Query class provided by the SDK to filter documents to export. [Learn more about queries](https://appwrite.io/docs/databases#querying-documents). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long.', true)
@@ -82,7 +83,8 @@ class Create extends Action
}
public function action(
string $resourceId,
string $databaseId,
string $collectionId,
string $filename,
array $columns,
array $queries,
@@ -112,14 +114,6 @@ class Create extends Action
throw new Exception(Exception::STORAGE_BUCKET_NOT_FOUND);
}
[$databaseId, $collectionId] = \explode(':', $resourceId, 2);
if (empty($databaseId)) {
throw new Exception(Exception::DATABASE_NOT_FOUND);
}
if (empty($collectionId)) {
throw new Exception(Exception::COLLECTION_NOT_FOUND);
}
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
throw new Exception(Exception::DATABASE_NOT_FOUND);
@@ -150,7 +144,7 @@ class Create extends Action
}
$resources = Transfer::extractServices([self::transferGroupForDatabaseType($databaseType)]);
$resourceType = self::resourceTypeForDatabaseType($databaseType);
$parentResourceType = self::resourceTypeForDatabaseType($databaseType);
$migration = $dbForProject->createDocument('migrations', new Document([
'$id' => ID::unique(),
@@ -159,8 +153,12 @@ class Create extends Action
'source' => AppwriteSource::getName(),
'destination' => CSV::getName(),
'resources' => $resources,
'resourceId' => $resourceId,
'resourceType' => $resourceType,
'resourceId' => $collection->getId(),
'resourceInternalId' => $collection->getSequence(),
'resourceType' => Resource::TYPE_COLLECTION,
'parentResourceId' => $database->getId(),
'parentResourceInternalId' => $database->getSequence(),
'parentResourceType' => $parentResourceType,
'statusCounters' => '{}',
'resourceData' => '{}',
'errors' => [],
@@ -10,7 +10,6 @@ use Appwrite\OpenSSL\OpenSSL;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Database\Validator\CompoundUID;
use Appwrite\Utopia\Response;
use Utopia\Compression\Algorithms\GZIP;
use Utopia\Compression\Algorithms\Zstd;
@@ -68,7 +67,8 @@ class Create extends Action
))
->param('bucketId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).', false, ['dbForProject'])
->param('fileId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'File ID.', false, ['dbForProject'])
->param('resourceId', null, new CompoundUID(), 'Composite ID in the format {databaseId:collectionId}, identifying a collection within a database.')
->param('databaseId', '', new UID(), 'Database ID containing the target collection.')
->param('collectionId', '', new UID(), 'Collection ID to import documents into.')
->param('internalFile', false, new Boolean(), 'Is the file stored in an internal bucket?', true)
->param('onDuplicate', OnDuplicate::Fail->value, new WhiteList(OnDuplicate::values()), 'Behavior when a row with an existing $id is encountered. "fail" (default): abort on first conflict. "skip": silently ignore. "overwrite": replace existing row.', true, enum: new Enum(name: 'OnDuplicate'))
->inject('response')
@@ -87,7 +87,8 @@ class Create extends Action
public function action(
string $bucketId,
string $fileId,
string $resourceId,
string $databaseId,
string $collectionId,
bool $internalFile,
string $onDuplicate,
Response $response,
@@ -163,15 +164,24 @@ class Create extends Action
throw new \Exception('Unable to copy file');
}
[$databaseId] = \explode(':', $resourceId, 2);
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
throw new Exception(Exception::DATABASE_NOT_FOUND);
}
$databaseType = $database->getAttribute('type');
if (!\in_array($databaseType, CSV_ALLOWED_DATABASE_TYPES)) {
throw new Exception(Exception::MIGRATION_DATABASE_TYPE_UNSUPPORTED, 'Database type not supported for csv');
}
$collection = $authorization->skip(fn () => $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId));
if ($collection->isEmpty()) {
throw new Exception(Exception::COLLECTION_NOT_FOUND);
}
$fileSize = $deviceForMigrations->getFileSize($newPath);
$resources = Transfer::extractServices([self::transferGroupForDatabaseType($databaseType)]);
$resourceType = self::resourceTypeForDatabaseType($databaseType);
$parentResourceType = self::resourceTypeForDatabaseType($databaseType);
$migration = $dbForProject->createDocument('migrations', new Document([
'$id' => $migrationId,
@@ -180,8 +190,12 @@ class Create extends Action
'source' => CSV::getName(),
'destination' => AppwriteSource::getName(),
'resources' => $resources,
'resourceId' => $resourceId,
'resourceType' => $resourceType,
'resourceId' => $collection->getId(),
'resourceInternalId' => $collection->getSequence(),
'resourceType' => Resource::TYPE_COLLECTION,
'parentResourceId' => $database->getId(),
'parentResourceInternalId' => $database->getSequence(),
'parentResourceType' => $parentResourceType,
'statusCounters' => '{}',
'resourceData' => '{}',
'errors' => [],
@@ -9,7 +9,6 @@ use Appwrite\Extend\Exception;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Database\Validator\CompoundUID;
use Appwrite\Utopia\Response;
use Utopia\Database\Database;
use Utopia\Database\Document;
@@ -18,6 +17,7 @@ use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\Queries\Documents;
use Utopia\Database\Validator\UID;
use Utopia\Migration\Resource;
use Utopia\Migration\Sources\Appwrite as AppwriteSource;
use Utopia\Migration\Sources\JSON as JSONSource;
@@ -60,7 +60,8 @@ class Create extends Action
)
]
))
->param('resourceId', null, new CompoundUID(), 'Composite ID in the format {databaseId:collectionId}, identifying a collection within a database to export.')
->param('databaseId', '', new UID(), 'Database ID containing the source collection.')
->param('collectionId', '', new UID(), 'Collection ID to export documents from.')
->param('filename', '', new Text(255), 'The name of the file to be created for the export, excluding the .json extension.')
->param('columns', [], new ArrayList(new Text(Database::LENGTH_KEY)), 'List of attributes to export. If empty, all attributes will be exported. You can use the `*` wildcard to export all attributes from the collection.', true)
->param('queries', [], new ArrayList(new Text(0)), 'Array of query strings generated using the Query class provided by the SDK to filter documents to export. [Learn more about queries](https://appwrite.io/docs/databases#querying-documents). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long.', true)
@@ -78,7 +79,8 @@ class Create extends Action
}
public function action(
string $resourceId,
string $databaseId,
string $collectionId,
string $filename,
array $columns,
array $queries,
@@ -104,14 +106,6 @@ class Create extends Action
throw new Exception(Exception::STORAGE_BUCKET_NOT_FOUND);
}
[$databaseId, $collectionId] = \explode(':', $resourceId, 2);
if (empty($databaseId)) {
throw new Exception(Exception::DATABASE_NOT_FOUND);
}
if (empty($collectionId)) {
throw new Exception(Exception::COLLECTION_NOT_FOUND);
}
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
throw new Exception(Exception::DATABASE_NOT_FOUND);
@@ -139,7 +133,7 @@ class Create extends Action
}
$resources = Transfer::extractServices([self::transferGroupForDatabaseType($databaseType)]);
$resourceType = self::resourceTypeForDatabaseType($databaseType);
$parentResourceType = self::resourceTypeForDatabaseType($databaseType);
$migration = $dbForProject->createDocument('migrations', new Document([
'$id' => ID::unique(),
@@ -148,8 +142,12 @@ class Create extends Action
'source' => AppwriteSource::getName(),
'destination' => JSONSource::getName(),
'resources' => $resources,
'resourceId' => $resourceId,
'resourceType' => $resourceType,
'resourceId' => $collection->getId(),
'resourceInternalId' => $collection->getSequence(),
'resourceType' => Resource::TYPE_COLLECTION,
'parentResourceId' => $database->getId(),
'parentResourceInternalId' => $database->getSequence(),
'parentResourceType' => $parentResourceType,
'statusCounters' => '{}',
'resourceData' => '{}',
'errors' => [],
@@ -10,7 +10,6 @@ use Appwrite\OpenSSL\OpenSSL;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Database\Validator\CompoundUID;
use Appwrite\Utopia\Response;
use Utopia\Compression\Algorithms\GZIP;
use Utopia\Compression\Algorithms\Zstd;
@@ -67,7 +66,8 @@ class Create extends Action
))
->param('bucketId', '', new UID(), 'Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).')
->param('fileId', '', new UID(), 'File ID.')
->param('resourceId', null, new CompoundUID(), 'Composite ID in the format {databaseId:collectionId}, identifying a collection within a database.')
->param('databaseId', '', new UID(), 'Database ID containing the target collection.')
->param('collectionId', '', new UID(), 'Collection ID to import documents into.')
->param('internalFile', false, new Boolean(), 'Is the file stored in an internal bucket?', true)
->param('onDuplicate', OnDuplicate::Fail->value, new WhiteList(OnDuplicate::values()), 'Behavior when a row with an existing $id is encountered. "fail" (default): abort on first conflict. "skip": silently ignore. "overwrite": replace existing row.', true, enum: new Enum(name: 'OnDuplicate'))
->inject('response')
@@ -86,7 +86,8 @@ class Create extends Action
public function action(
string $bucketId,
string $fileId,
string $resourceId,
string $databaseId,
string $collectionId,
bool $internalFile,
string $onDuplicate,
Response $response,
@@ -164,14 +165,19 @@ class Create extends Action
$fileSize = $deviceForMigrations->getFileSize($newPath);
[$databaseId] = \explode(':', $resourceId, 2);
$database = $authorization->skip(fn () => $dbForProject->getDocument('databases', $databaseId));
if ($database->isEmpty()) {
throw new Exception(Exception::DATABASE_NOT_FOUND);
}
$collection = $authorization->skip(fn () => $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId));
if ($collection->isEmpty()) {
throw new Exception(Exception::COLLECTION_NOT_FOUND);
}
$databaseType = $database->getAttribute('type');
$resources = Transfer::extractServices([self::transferGroupForDatabaseType($databaseType)]);
$resourceType = self::resourceTypeForDatabaseType($databaseType);
$parentResourceType = self::resourceTypeForDatabaseType($databaseType);
$migration = $dbForProject->createDocument('migrations', new Document([
'$id' => $migrationId,
@@ -180,8 +186,12 @@ class Create extends Action
'source' => JSONSource::getName(),
'destination' => AppwriteSource::getName(),
'resources' => $resources,
'resourceId' => $resourceId,
'resourceType' => $resourceType,
'resourceId' => $collection->getId(),
'resourceInternalId' => $collection->getSequence(),
'resourceType' => Resource::TYPE_COLLECTION,
'parentResourceId' => $database->getId(),
'parentResourceInternalId' => $database->getSequence(),
'parentResourceType' => $parentResourceType,
'statusCounters' => '{}',
'resourceData' => '{}',
'errors' => [],
+63 -12
View File
@@ -192,7 +192,7 @@ class Migrations extends Action
{
$source = $migration->getAttribute('source');
$destination = $migration->getAttribute('destination');
$resourceId = $migration->getAttribute('resourceId');
[$databaseId, $tableId] = $this->resolveResourceIds($migration);
$credentials = $migration->getAttribute('credentials');
$migrationOptions = $migration->getAttribute('options');
/** @var Database|null $projectDB */
@@ -291,14 +291,16 @@ class Migrations extends Action
$queries
),
CSV::getName() => new CSV(
$resourceId,
$databaseId,
$tableId,
$migrationOptions['path'],
$this->deviceForMigrations,
$this->dbForProject,
$getDatabasesDB
),
JSON::getName() => new JSON(
$resourceId,
$databaseId,
$tableId,
$migrationOptions['path'],
$this->deviceForMigrations,
$this->dbForProject,
@@ -320,6 +322,7 @@ class Migrations extends Action
$destination = $migration->getAttribute('destination');
$options = $migration->getAttribute('options', []);
$credentials = $migration->getAttribute('credentials');
[$databaseId, $tableId] = $this->resolveResourceIds($migration);
return match ($destination) {
DestinationAppwrite::getName() => new DestinationAppwrite(
@@ -336,7 +339,8 @@ class Migrations extends Action
),
DestinationCSV::getName() => new DestinationCSV(
$this->deviceForFiles,
$migration->getAttribute('resourceId'),
$databaseId,
$tableId,
$options['bucketId'],
$migration->getId(),
$options['columns'],
@@ -347,7 +351,8 @@ class Migrations extends Action
),
DestinationJSON::getName() => new DestinationJSON(
$this->deviceForFiles,
$migration->getAttribute('resourceId'),
$databaseId,
$tableId,
$options['bucketId'] ?? 'default',
$migration->getId(),
$options['columns'] ?? [],
@@ -573,8 +578,9 @@ class Migrations extends Action
}
$this->updateMigrationDocument($migration, $project, $queueForRealtime);
},
$migration->getAttribute('resourceId'),
$migration->getAttribute('resourceType')
$this->resolveResourceIds($migration)[0],
$migration->getAttribute('parentResourceType') ?: $migration->getAttribute('resourceType'),
$this->resolveResourceIds($migration)[1],
);
$destination->shutdown();
@@ -692,7 +698,7 @@ class Migrations extends Action
$publisherForUsage,
$migration->getAttribute('source'),
$authorization,
$migration->getAttribute('resourceId')
...$this->resolveResourceIds($migration),
);
}
$destination?->success();
@@ -718,6 +724,40 @@ class Migrations extends Action
return ($this->getDatabasesDB)($database);
}
/**
* Resolve [rootResourceId, rootResourceChildId] from a migration document.
*
* Returns the root resource ID (e.g. databaseId for CSV/JSON or backup
* migrations) and an optional child filter under that root (the
* collection/table ID for CSV/JSON migrations). Three input shapes:
*
* - 1.9.4 CSV/JSON migration: parentResourceId is set to the root,
* resourceId is the leaf. Returns [parent, leaf].
* - Pre-1.9.4 CSV/JSON migration (V25 not yet run, queue drain after
* upgrade, restored backup): resourceId is the composite
* "{databaseId}:{tableId}", parentResourceId is empty. Split it.
* - Single-root migration (Backup, full Appwrite/Supabase/Firebase/
* NHost imports): resourceId is the root resource ID with no leaf.
* Returns [root, ''].
*
* @return array{0: string, 1: string}
*/
protected function resolveResourceIds(Document $migration): array
{
$parent = (string) $migration->getAttribute('parentResourceId', '');
$resource = (string) $migration->getAttribute('resourceId', '');
if ($parent !== '') {
return [$parent, $resource];
}
if (\str_contains($resource, ':')) {
return \explode(':', $resource, 2);
}
return [$resource, ''];
}
/**
* Handle actions to be performed when a CSV export migration is successfully completed
*
@@ -1074,7 +1114,7 @@ class Migrations extends Action
return $errors;
}
private function processMigrationResourceStats(array $resources, Context $usage, Document $projectDocument, UsagePublisher $publisherForUsage, string $source, Authorization $authorization, ?string $resourceId)
private function processMigrationResourceStats(array $resources, Context $usage, Document $projectDocument, UsagePublisher $publisherForUsage, string $source, Authorization $authorization, ?string $parentResourceId, ?string $resourceId)
{
$resourceName = $resources['name'];
$count = $resources['count'];
@@ -1082,9 +1122,20 @@ class Migrations extends Action
$tableInternalId = $resources['tableId'];
if ($source === CSV::getName()) {
[$databaseId, $tableId] = explode(':', $resourceId);
$database = $authorization->skip(fn () => $this->dbForProject->getDocument('databases', $databaseId));
$table = $authorization->skip(fn () => $this->dbForProject->getDocument('database_' . $database->getSequence(), $tableId));
if (empty($parentResourceId) || empty($resourceId)) {
Console::warning("Skipping CSV migration usage stats: missing parent/leaf resource ID (parent: '{$parentResourceId}', leaf: '{$resourceId}')");
return;
}
$database = $authorization->skip(fn () => $this->dbForProject->getDocument('databases', $parentResourceId));
if ($database->isEmpty()) {
Console::warning("Skipping CSV migration usage stats: database '{$parentResourceId}' not found");
return;
}
$table = $authorization->skip(fn () => $this->dbForProject->getDocument('database_' . $database->getSequence(), $resourceId));
if ($table->isEmpty()) {
Console::warning("Skipping CSV migration usage stats: collection '{$resourceId}' not found in database '{$parentResourceId}'");
return;
}
$databaseInternalId = (int) $database->getSequence();
$tableInternalId = (int) $table->getSequence();
}
@@ -12,9 +12,11 @@ class Migrations extends Base
'resources',
'resourceId',
'resourceType',
'parentResourceId',
'parentResourceType',
'statusCounters',
'resourceData',
'errors'
'errors',
];
/**
@@ -0,0 +1,38 @@
<?php
namespace Appwrite\Utopia\Request\Filters;
use Appwrite\Utopia\Request\Filter;
class V27 extends Filter
{
// Convert 1.9.5 params to 1.9.6
public function parse(array $content, string $model): array
{
return match ($model) {
'migrations.createCSVImport',
'migrations.createCSVExport',
'migrations.createJSONImport',
'migrations.createJSONExport' => $this->parseMigrationResource($content),
default => $content,
};
}
protected function parseMigrationResource(array $content): array
{
if (!isset($content['resourceId']) || !\is_string($content['resourceId'])) {
return $content;
}
if (!\str_contains($content['resourceId'], ':')) {
return $content;
}
[$databaseId, $collectionId] = \explode(':', $content['resourceId'], 2);
$content['databaseId'] = $content['databaseId'] ?? $databaseId;
$content['collectionId'] = $content['collectionId'] ?? $collectionId;
unset($content['resourceId']);
return $content;
}
}
@@ -0,0 +1,41 @@
<?php
namespace Appwrite\Utopia\Response\Filters;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Filter;
// Convert 1.9.6 data format to 1.9.5 format
class V27 extends Filter
{
public function parse(array $content, string $model): array
{
return match ($model) {
Response::MODEL_MIGRATION => $this->parseMigration($content),
Response::MODEL_MIGRATION_LIST => $this->handleList($content, 'migrations', fn ($item) => $this->parseMigration($item)),
default => $content,
};
}
protected function parseMigration(array $content): array
{
$parentResourceId = $content['parentResourceId'] ?? '';
$resourceId = $content['resourceId'] ?? '';
if ($parentResourceId !== '' && $resourceId !== '') {
$content['resourceId'] = $parentResourceId . ':' . $resourceId;
}
$parentResourceType = $content['parentResourceType'] ?? '';
if ($parentResourceType !== '') {
$content['resourceType'] = $parentResourceType;
}
unset($content['resourceInternalId']);
unset($content['parentResourceId']);
unset($content['parentResourceInternalId']);
unset($content['parentResourceType']);
return $content;
}
}
@@ -62,9 +62,44 @@ class Migration extends Model
])
->addRule('resourceId', [
'type' => self::TYPE_STRING,
'description' => 'Id of the resource to migrate.',
'description' => 'ID of the resource being migrated.',
'default' => '',
'example' => 'databaseId:collectionId',
'example' => 'collectionId',
'array' => false
])
->addRule('resourceInternalId', [
'type' => self::TYPE_STRING,
'description' => 'Internal ID of the resource being migrated.',
'default' => '',
'example' => '1',
'array' => false
])
->addRule('resourceType', [
'type' => self::TYPE_STRING,
'description' => 'Type of the resource being migrated.',
'default' => '',
'example' => 'collection',
'array' => false
])
->addRule('parentResourceId', [
'type' => self::TYPE_STRING,
'description' => 'ID of the parent resource that contains the migrated resource.',
'default' => '',
'example' => 'databaseId',
'array' => false
])
->addRule('parentResourceInternalId', [
'type' => self::TYPE_STRING,
'description' => 'Internal ID of the parent resource that contains the migrated resource.',
'default' => '',
'example' => '1',
'array' => false
])
->addRule('parentResourceType', [
'type' => self::TYPE_STRING,
'description' => 'Type of the parent resource that contains the migrated resource.',
'default' => '',
'example' => 'database',
'array' => false
])
->addRule('statusCounters', [
@@ -3641,7 +3641,8 @@ trait MigrationsBase
[
'fileId' => $fileIds['missing-column'],
'bucketId' => $bucketIds['missing-column'],
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]
);
@@ -3673,7 +3674,8 @@ trait MigrationsBase
[
'fileId' => $fileIds['missing-row'],
'bucketId' => $bucketIds['missing-row'],
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]
);
@@ -3705,7 +3707,8 @@ trait MigrationsBase
[
'fileId' => $fileIds['irrelevant-column'],
'bucketId' => $bucketIds['irrelevant-column'],
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]
);
@@ -3732,7 +3735,8 @@ trait MigrationsBase
'endpoint' => $this->webEndpoint,
'fileId' => $fileIds['default'],
'bucketId' => $bucketIds['default'],
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]
);
@@ -3774,7 +3778,8 @@ trait MigrationsBase
'endpoint' => $this->webEndpoint,
'fileId' => $fileIds['documents-internals'],
'bucketId' => $bucketIds['documents-internals'],
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]
);
@@ -3888,7 +3893,8 @@ trait MigrationsBase
$first = $this->performCsvMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]);
$this->assertEventually(function () use ($first) {
$migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([
@@ -3914,7 +3920,8 @@ trait MigrationsBase
$second = $this->performCsvMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
'onDuplicate' => 'skip',
]);
$this->assertEventually(function () use ($second) {
@@ -3955,7 +3962,8 @@ trait MigrationsBase
$first = $this->performCsvMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]);
$this->assertEventually(function () use ($first) {
$migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([
@@ -3981,7 +3989,8 @@ trait MigrationsBase
$second = $this->performCsvMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
'onDuplicate' => 'overwrite',
]);
$this->assertEventually(function () use ($second) {
@@ -4023,7 +4032,8 @@ trait MigrationsBase
$first = $this->performCsvMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]);
$this->assertEventually(function () use ($first) {
$migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([
@@ -4037,7 +4047,8 @@ trait MigrationsBase
$second = $this->performCsvMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]);
$this->assertEventually(function () use ($second) {
$migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([
@@ -4148,7 +4159,8 @@ trait MigrationsBase
$first = $this->performJsonMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]);
$this->assertEventually(function () use ($first) {
$migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([
@@ -4173,7 +4185,8 @@ trait MigrationsBase
$second = $this->performJsonMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
'onDuplicate' => 'skip',
]);
$this->assertEventually(function () use ($second) {
@@ -4211,7 +4224,8 @@ trait MigrationsBase
$first = $this->performJsonMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]);
$this->assertEventually(function () use ($first) {
$migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([
@@ -4235,7 +4249,8 @@ trait MigrationsBase
$second = $this->performJsonMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
'onDuplicate' => 'overwrite',
]);
$this->assertEventually(function () use ($second) {
@@ -4273,7 +4288,8 @@ trait MigrationsBase
$first = $this->performJsonMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]);
$this->assertEventually(function () use ($first) {
$migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([
@@ -4286,7 +4302,8 @@ trait MigrationsBase
$second = $this->performJsonMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]);
$this->assertEventually(function () use ($second) {
$migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([
@@ -4465,7 +4482,8 @@ trait MigrationsBase
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()), [
'resourceId' => $databaseId . ':' . $collectionId,
'databaseId' => $databaseId,
'collectionId' => $collectionId,
'filename' => 'test-export',
'columns' => [],
'delimiter' => ',',
@@ -5445,7 +5463,8 @@ trait MigrationsBase
$migration = $this->performCsvMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $collectionId,
'databaseId' => $databaseId,
'collectionId' => $collectionId,
]);
$this->assertEquals(202, $migration['headers']['status-code']);
@@ -5573,7 +5592,8 @@ trait MigrationsBase
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'resourceId' => $databaseId . ':' . $collectionId,
'databaseId' => $databaseId,
'collectionId' => $collectionId,
'filename' => $filename,
'columns' => [],
'queries' => [],
@@ -6787,7 +6807,8 @@ trait MigrationsBase
[
'fileId' => $fileIds['missing-column'],
'bucketId' => $bucketIds['missing-column'],
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]
);
@@ -6824,7 +6845,8 @@ trait MigrationsBase
[
'fileId' => $fileIds['irrelevant-column'],
'bucketId' => $bucketIds['irrelevant-column'],
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]
);
@@ -6851,7 +6873,8 @@ trait MigrationsBase
'endpoint' => $this->endpoint,
'fileId' => $fileIds['default'],
'bucketId' => $bucketIds['default'],
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]
);
@@ -6893,7 +6916,8 @@ trait MigrationsBase
'endpoint' => $this->endpoint,
'fileId' => $fileIds['documents-internals'],
'bucketId' => $bucketIds['documents-internals'],
'resourceId' => $databaseId . ':' . $tableId,
'databaseId' => $databaseId,
'collectionId' => $tableId,
]
);
@@ -7016,7 +7040,8 @@ trait MigrationsBase
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()), [
'resourceId' => $databaseId . ':' . $collectionId,
'databaseId' => $databaseId,
'collectionId' => $collectionId,
'filename' => 'test-json-export',
'columns' => [],
'queries' => [],
@@ -7127,7 +7152,8 @@ trait MigrationsBase
// Trigger JSON export
$migration = $this->client->call(Client::METHOD_POST, '/migrations/json/exports', $headers, [
'resourceId' => $databaseId . ':' . $collectionId,
'databaseId' => $databaseId,
'collectionId' => $collectionId,
'filename' => 'vectorsdb-export-test',
'columns' => [],
'queries' => [],
@@ -7200,7 +7226,8 @@ trait MigrationsBase
$migration = $this->performJsonMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $collectionId,
'databaseId' => $databaseId,
'collectionId' => $collectionId,
]);
$this->assertEquals(202, $migration['headers']['status-code']);
@@ -7271,7 +7298,8 @@ trait MigrationsBase
// Trigger JSON export
$migration = $this->client->call(Client::METHOD_POST, '/migrations/json/exports', $headers, [
'resourceId' => $databaseId . ':' . $collectionId,
'databaseId' => $databaseId,
'collectionId' => $collectionId,
'filename' => 'documentsdb-export-test',
'columns' => [],
'queries' => [],
@@ -7343,7 +7371,8 @@ trait MigrationsBase
$migration = $this->performJsonMigration([
'fileId' => $fileId,
'bucketId' => $bucketId,
'resourceId' => $databaseId . ':' . $collectionId,
'databaseId' => $databaseId,
'collectionId' => $collectionId,
]);
$this->assertEquals(202, $migration['headers']['status-code']);
+103
View File
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Migration\Version;
use Appwrite\Migration\Version\V25;
use PHPUnit\Framework\TestCase;
use Utopia\Database\Document;
use Utopia\Migration\Resource;
final class V25Test extends TestCase
{
public function testSplitsDeletedResourceWithoutWritingEmptyInternalIds(): void
{
$migration = new class () extends V25 {
public function __construct()
{
}
public function migrate(Document $document): Document
{
return $this->migrateDocument($document);
}
#[\Override]
protected function resolveInternalIds(string $parentResourceId, string $resourceId, string $migrationId): array
{
return [];
}
};
$document = new Document([
'$id' => 'migration',
'$collection' => 'migrations',
'resourceId' => 'database:collection',
'resourceType' => Resource::TYPE_DATABASE,
]);
$result = $migration->migrate($document);
$this->assertSame('collection', $result->getAttribute('resourceId'));
$this->assertSame(Resource::TYPE_COLLECTION, $result->getAttribute('resourceType'));
$this->assertSame('database', $result->getAttribute('parentResourceId'));
$this->assertSame(Resource::TYPE_DATABASE, $result->getAttribute('parentResourceType'));
$this->assertNull($result->getAttribute('resourceInternalId'));
$this->assertNull($result->getAttribute('parentResourceInternalId'));
}
public function testRerunResolvesMissingInternalIdsIdempotently(): void
{
$migration = new class () extends V25 {
/**
* @var array{parentResourceInternalId?: string, resourceInternalId?: string}
*/
private array $internalIds = [];
public function __construct()
{
}
public function migrate(Document $document): Document
{
return $this->migrateDocument($document);
}
/**
* @param array{parentResourceInternalId?: string, resourceInternalId?: string} $internalIds
*/
public function setInternalIds(array $internalIds): void
{
$this->internalIds = $internalIds;
}
/**
* @return array{parentResourceInternalId?: string, resourceInternalId?: string}
*/
#[\Override]
protected function resolveInternalIds(string $parentResourceId, string $resourceId, string $migrationId): array
{
return $this->internalIds;
}
};
$document = new Document([
'$id' => 'migration',
'$collection' => 'migrations',
'resourceId' => 'database:collection',
'resourceType' => Resource::TYPE_DATABASE,
]);
$migration->migrate($document);
$migration->setInternalIds([
'parentResourceInternalId' => '10',
'resourceInternalId' => '20',
]);
$migration->migrate($document);
$afterResolution = $document->getArrayCopy();
$migration->migrate($document);
$this->assertSame('10', $document->getAttribute('parentResourceInternalId'));
$this->assertSame('20', $document->getAttribute('resourceInternalId'));
$this->assertSame($afterResolution, $document->getArrayCopy());
}
}
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Utopia\Request\Filters;
use Appwrite\Utopia\Request\Filters\V27;
use PHPUnit\Framework\TestCase;
final class V27Test extends TestCase
{
public function testSplitsLegacyMigrationResource(): void
{
$filter = new V27();
$result = $filter->parse([
'resourceId' => 'database:collection',
], 'migrations.createCSVImport');
$this->assertSame([
'databaseId' => 'database',
'collectionId' => 'collection',
], $result);
}
public function testExplicitResourceIdsTakePrecedence(): void
{
$filter = new V27();
$result = $filter->parse([
'resourceId' => 'legacyDatabase:legacyCollection',
'databaseId' => 'database',
'collectionId' => 'collection',
], 'migrations.createJSONExport');
$this->assertSame([
'databaseId' => 'database',
'collectionId' => 'collection',
], $result);
}
public function testLeavesMalformedResourceForValidation(): void
{
$filter = new V27();
$content = ['resourceId' => 'database'];
$this->assertSame($content, $filter->parse($content, 'migrations.createCSVExport'));
}
}
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Utopia\Response\Filters;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Filters\V27;
use PHPUnit\Framework\TestCase;
final class V27Test extends TestCase
{
public function testRestoresLegacyMigrationResourceShape(): void
{
$filter = new V27();
$result = $filter->parse([
'$id' => 'migration',
'resourceId' => 'collection',
'resourceInternalId' => '20',
'resourceType' => 'collection',
'parentResourceId' => 'database',
'parentResourceInternalId' => '10',
'parentResourceType' => 'database',
], Response::MODEL_MIGRATION);
$this->assertSame([
'$id' => 'migration',
'resourceId' => 'database:collection',
'resourceType' => 'database',
], $result);
}
public function testPreservesSingleRootMigration(): void
{
$filter = new V27();
$result = $filter->parse([
'$id' => 'migration',
'resourceId' => 'database',
'resourceInternalId' => '10',
'resourceType' => 'database',
'parentResourceId' => '',
'parentResourceInternalId' => '',
'parentResourceType' => '',
], Response::MODEL_MIGRATION);
$this->assertSame([
'$id' => 'migration',
'resourceId' => 'database',
'resourceType' => 'database',
], $result);
}
public function testTransformsMigrationList(): void
{
$filter = new V27();
$result = $filter->parse([
'total' => 1,
'migrations' => [[
'$id' => 'migration',
'resourceId' => 'collection',
'resourceType' => 'collection',
'parentResourceId' => 'database',
'parentResourceType' => 'database',
]],
], Response::MODEL_MIGRATION_LIST);
$this->assertSame([
'total' => 1,
'migrations' => [[
'$id' => 'migration',
'resourceId' => 'database:collection',
'resourceType' => 'database',
]],
], $result);
}
}