From 22800b14d91714d5d8f3a2a91a36e30f1b34f809 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 20 Jul 2026 22:38:30 +1200 Subject: [PATCH] (feat): expose migration destination generations --- app/config/collections/projects.php | 18 +++++++++ src/Appwrite/Migration/Version/V25.php | 38 ++++++++++++++++--- .../Http/Migrations/CSV/Imports/Create.php | 1 + .../Http/Migrations/JSON/Imports/Create.php | 1 + .../Database/Validator/Queries/Migrations.php | 3 ++ src/Appwrite/Utopia/Response/Filters/V27.php | 1 + .../Utopia/Response/Model/Migration.php | 7 ++++ .../Services/Migrations/MigrationsBase.php | 4 +- tests/unit/Migration/Version/V25Test.php | 29 +++++++++++++- .../Validator/Queries/MigrationsTest.php | 33 ++++++++++++++++ .../unit/Utopia/Response/Filters/V27Test.php | 3 ++ 11 files changed, 129 insertions(+), 9 deletions(-) create mode 100644 tests/unit/Utopia/Database/Validator/Queries/MigrationsTest.php diff --git a/app/config/collections/projects.php b/app/config/collections/projects.php index 052103c9ac..9c806696cb 100644 --- a/app/config/collections/projects.php +++ b/app/config/collections/projects.php @@ -2333,6 +2333,17 @@ return [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('destinationResourceInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('destinationResourceType'), 'type' => Database::VAR_STRING, @@ -2416,6 +2427,13 @@ return [ 'lengths' => [Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC], ], + [ + '$id' => '_key_destinationResourceInternalId', + 'type' => Database::INDEX_KEY, + 'attributes' => ['destinationResourceInternalId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], [ '$id' => '_key_destinationResourceType', 'type' => Database::INDEX_KEY, diff --git a/src/Appwrite/Migration/Version/V25.php b/src/Appwrite/Migration/Version/V25.php index 62d734af9e..759a3d0794 100644 --- a/src/Appwrite/Migration/Version/V25.php +++ b/src/Appwrite/Migration/Version/V25.php @@ -117,6 +117,7 @@ class V25 extends Migration 'parentResourceInternalId', 'parentResourceType', 'destinationResourceId', + 'destinationResourceInternalId', 'destinationResourceType', ]; try { @@ -132,6 +133,7 @@ class V25 extends Migration '_key_parentResourceType', '_key_parentResourceInternalId', '_key_destinationResourceId', + '_key_destinationResourceInternalId', '_key_destinationResourceType', ]; foreach ($indexes as $index) { @@ -178,7 +180,7 @@ class V25 extends Migration return $document; } - $internalIds = $this->resolveInternalIds($parentResourceId, $resourceId, $document->getId()); + $internalIds = $this->resolveInternalIds($parentResourceId, $resourceId, $document); if ( $split && (!isset($internalIds['parentResourceInternalId']) || !isset($internalIds['resourceInternalId'])) @@ -216,15 +218,19 @@ class V25 extends Migration /** * @return array{parentResourceInternalId?: string, resourceInternalId?: string} */ - protected function resolveInternalIds(string $parentResourceId, string $resourceId, string $migrationId): array + protected function resolveInternalIds(string $parentResourceId, string $resourceId, Document $migration): array { try { $database = $this->dbForProject->getDocument('databases', $parentResourceId); - if ($database->isEmpty() || $database->getSequence() === '') { + if ( + $database->isEmpty() + || $database->getSequence() === '' + || !$this->predatesMigration($database, $migration) + ) { return []; } } catch (Throwable $th) { - Console::warning("Failed to resolve parent internal ID for migration {$migrationId}: {$th->getMessage()}"); + Console::warning("Failed to resolve parent internal ID for migration {$migration->getId()}: {$th->getMessage()}"); return []; } @@ -234,13 +240,33 @@ class V25 extends Migration try { $resource = $this->dbForProject->getDocument('database_' . $database->getSequence(), $resourceId); - if (!$resource->isEmpty() && $resource->getSequence() !== '') { + if ( + !$resource->isEmpty() + && $resource->getSequence() !== '' + && $this->predatesMigration($resource, $migration) + ) { $internalIds['resourceInternalId'] = (string) $resource->getSequence(); } } catch (Throwable $th) { - Console::warning("Failed to resolve resource internal ID for migration {$migrationId}: {$th->getMessage()}"); + Console::warning("Failed to resolve resource internal ID for migration {$migration->getId()}: {$th->getMessage()}"); } return $internalIds; } + + /** + * A resource created after the migration is a reused public ID, not the + * generation the historical migration operated on. + */ + protected function predatesMigration(Document $resource, Document $migration): bool + { + $resourceCreatedAt = \strtotime($resource->getCreatedAt()); + $migrationCreatedAt = \strtotime($migration->getCreatedAt()); + + if ($resourceCreatedAt === false || $migrationCreatedAt === false) { + return false; + } + + return $resourceCreatedAt <= $migrationCreatedAt; + } } diff --git a/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/CSV/Imports/Create.php b/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/CSV/Imports/Create.php index 7dddb7b575..61b1298fd9 100644 --- a/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/CSV/Imports/Create.php +++ b/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/CSV/Imports/Create.php @@ -197,6 +197,7 @@ class Create extends Action 'parentResourceInternalId' => $database->getSequence(), 'parentResourceType' => $parentResourceType, 'destinationResourceId' => $database->getId(), + 'destinationResourceInternalId' => $database->getSequence(), 'destinationResourceType' => $parentResourceType, 'statusCounters' => '{}', 'resourceData' => '{}', diff --git a/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/JSON/Imports/Create.php b/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/JSON/Imports/Create.php index 06101e8565..4bcba3edb3 100644 --- a/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/JSON/Imports/Create.php +++ b/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/JSON/Imports/Create.php @@ -193,6 +193,7 @@ class Create extends Action 'parentResourceInternalId' => $database->getSequence(), 'parentResourceType' => $parentResourceType, 'destinationResourceId' => $database->getId(), + 'destinationResourceInternalId' => $database->getSequence(), 'destinationResourceType' => $parentResourceType, 'statusCounters' => '{}', 'resourceData' => '{}', diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Migrations.php b/src/Appwrite/Utopia/Database/Validator/Queries/Migrations.php index a146c39d10..0d5e2f28ca 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Migrations.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Migrations.php @@ -11,10 +11,13 @@ class Migrations extends Base 'destination', 'resources', 'resourceId', + 'resourceInternalId', 'resourceType', 'parentResourceId', + 'parentResourceInternalId', 'parentResourceType', 'destinationResourceId', + 'destinationResourceInternalId', 'destinationResourceType', 'statusCounters', 'resourceData', diff --git a/src/Appwrite/Utopia/Response/Filters/V27.php b/src/Appwrite/Utopia/Response/Filters/V27.php index 258dd56ffc..e0cf53ea3f 100644 --- a/src/Appwrite/Utopia/Response/Filters/V27.php +++ b/src/Appwrite/Utopia/Response/Filters/V27.php @@ -36,6 +36,7 @@ class V27 extends Filter unset($content['parentResourceInternalId']); unset($content['parentResourceType']); unset($content['destinationResourceId']); + unset($content['destinationResourceInternalId']); unset($content['destinationResourceType']); return $content; diff --git a/src/Appwrite/Utopia/Response/Model/Migration.php b/src/Appwrite/Utopia/Response/Model/Migration.php index 49463b0c9a..e6a15cff37 100644 --- a/src/Appwrite/Utopia/Response/Model/Migration.php +++ b/src/Appwrite/Utopia/Response/Model/Migration.php @@ -109,6 +109,13 @@ class Migration extends Model 'example' => 'databaseId', 'array' => false ]) + ->addRule('destinationResourceInternalId', [ + 'type' => self::TYPE_STRING, + 'description' => 'Internal ID of the destination resource created or overwritten by the migration.', + 'default' => '', + 'example' => '1', + 'array' => false + ]) ->addRule('destinationResourceType', [ 'type' => self::TYPE_STRING, 'description' => 'Type of the destination resource created or overwritten by the migration.', diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index c440fa32ce..65a96694f0 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -3855,8 +3855,10 @@ trait MigrationsBase $this->assertNotNull($migration); $this->assertSame($data['tableId'], $migration['resourceId']); $this->assertSame($databaseId, $migration['parentResourceId']); + $this->assertNotSame('', $migration['parentResourceInternalId']); $this->assertSame(Resource::TYPE_DATABASE, $migration['parentResourceType']); $this->assertSame($databaseId, $migration['destinationResourceId']); + $this->assertSame($migration['parentResourceInternalId'], $migration['destinationResourceInternalId']); $this->assertSame(Resource::TYPE_DATABASE, $migration['destinationResourceType']); $response = $this->client->call(Client::METHOD_GET, '/migrations', array_merge([ @@ -3864,7 +3866,7 @@ trait MigrationsBase 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => [ - Query::equal('destinationResourceId', [$databaseId])->toString(), + Query::equal('destinationResourceInternalId', [$migration['destinationResourceInternalId']])->toString(), Query::equal('destinationResourceType', [Resource::TYPE_DATABASE])->toString(), ], ]); diff --git a/tests/unit/Migration/Version/V25Test.php b/tests/unit/Migration/Version/V25Test.php index f09736e87f..d95805a13c 100644 --- a/tests/unit/Migration/Version/V25Test.php +++ b/tests/unit/Migration/Version/V25Test.php @@ -24,7 +24,7 @@ final class V25Test extends TestCase } #[\Override] - protected function resolveInternalIds(string $parentResourceId, string $resourceId, string $migrationId): array + protected function resolveInternalIds(string $parentResourceId, string $resourceId, Document $migration): array { return []; } @@ -75,7 +75,7 @@ final class V25Test extends TestCase * @return array{parentResourceInternalId?: string, resourceInternalId?: string} */ #[\Override] - protected function resolveInternalIds(string $parentResourceId, string $resourceId, string $migrationId): array + protected function resolveInternalIds(string $parentResourceId, string $resourceId, Document $migration): array { return $this->internalIds; } @@ -116,4 +116,29 @@ final class V25Test extends TestCase $this->assertSame(Resource::TYPE_DATABASE, $document->getAttribute('parentResourceType')); $this->assertSame($afterResolution, $document->getArrayCopy()); } + + public function testRejectsResourcesCreatedAfterMigration(): void + { + $migration = new class () extends V25 { + public function __construct() + { + } + + public function isCandidate(Document $resource, Document $migration): bool + { + return $this->predatesMigration($resource, $migration); + } + }; + $document = new Document([ + '$createdAt' => '2026-01-02T00:00:00.000+00:00', + ]); + + $this->assertTrue($migration->isCandidate(new Document([ + '$createdAt' => '2026-01-01T00:00:00.000+00:00', + ]), $document)); + $this->assertFalse($migration->isCandidate(new Document([ + '$createdAt' => '2026-01-03T00:00:00.000+00:00', + ]), $document)); + $this->assertFalse($migration->isCandidate(new Document(), $document)); + } } diff --git a/tests/unit/Utopia/Database/Validator/Queries/MigrationsTest.php b/tests/unit/Utopia/Database/Validator/Queries/MigrationsTest.php new file mode 100644 index 0000000000..80c8c41f1e --- /dev/null +++ b/tests/unit/Utopia/Database/Validator/Queries/MigrationsTest.php @@ -0,0 +1,33 @@ +assertTrue($validator->isValid([$query]), $validator->getDescription()); + } + } +} diff --git a/tests/unit/Utopia/Response/Filters/V27Test.php b/tests/unit/Utopia/Response/Filters/V27Test.php index e2420ae332..fe03dac7d9 100644 --- a/tests/unit/Utopia/Response/Filters/V27Test.php +++ b/tests/unit/Utopia/Response/Filters/V27Test.php @@ -23,6 +23,7 @@ final class V27Test extends TestCase 'parentResourceInternalId' => '10', 'parentResourceType' => 'database', 'destinationResourceId' => 'destination', + 'destinationResourceInternalId' => '30', 'destinationResourceType' => 'database', ], Response::MODEL_MIGRATION); @@ -46,6 +47,7 @@ final class V27Test extends TestCase 'parentResourceInternalId' => '', 'parentResourceType' => '', 'destinationResourceId' => '', + 'destinationResourceInternalId' => '', 'destinationResourceType' => '', ], Response::MODEL_MIGRATION); @@ -69,6 +71,7 @@ final class V27Test extends TestCase 'parentResourceId' => 'database', 'parentResourceType' => 'database', 'destinationResourceId' => 'destination', + 'destinationResourceInternalId' => '30', 'destinationResourceType' => 'database', ]], ], Response::MODEL_MIGRATION_LIST);