From aa9be97ca41aab7199b9c4fa08d582f327b204d9 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 20 Jul 2026 21:47:56 +1200 Subject: [PATCH] (refactor): pass canonical migration resource context --- src/Appwrite/Platform/Workers/Migrations.php | 74 ++++++++++--------- .../unit/Platform/Workers/MigrationsTest.php | 42 +++++++++++ 2 files changed, 81 insertions(+), 35 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 3be60e67b7..40d0843bd4 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -342,7 +342,7 @@ class Migrations extends Action databaseId: $databaseId, tableId: $tableId, directory: $options['bucketId'], - filename: $options['filename'], + filename: $migration->getId(), allowedColumns: $options['columns'], delimiter: $options['delimiter'], enclosure: $options['enclosure'], @@ -354,7 +354,7 @@ class Migrations extends Action databaseId: $databaseId, tableId: $tableId, directory: $options['bucketId'] ?? 'default', - filename: $options['filename'], + filename: $migration->getId(), allowedColumns: $options['columns'] ?? [], ), default => throw new Exception(Exception::MIGRATION_DESTINATION_TYPE_INVALID), @@ -540,11 +540,7 @@ class Migrations extends Action $migration->setAttribute('stage', 'migrating'); $this->updateMigrationDocument($migration, $project, $queueForRealtime); - [$rootResourceId, $rootResourceChildId] = $this->resolveResourceIds($migration); - $rootResourceType = (string) ( - $migration->getAttribute('parentResourceType') - ?: $migration->getAttribute('resourceType') - ); + $context = $this->resolveResourceContext($migration); $transfer->runWithResourceSelector( $migration->getAttribute('resources'), function ($resources) use ($migration, $transfer, $project, $queueForRealtime, &$aggregatedResources) { @@ -583,9 +579,12 @@ class Migrations extends Action } $this->updateMigrationDocument($migration, $project, $queueForRealtime); }, - $rootResourceId, - $rootResourceType, - $rootResourceChildId, + resourceId: $context['resourceId'], + resourceInternalId: $context['resourceInternalId'], + resourceType: $context['resourceType'], + parentResourceId: $context['parentResourceId'], + parentResourceInternalId: $context['parentResourceInternalId'], + parentResourceType: $context['parentResourceType'], ); $destination->shutdown(); @@ -730,38 +729,43 @@ 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} - */ + /** @return array{0: string, 1: string} */ protected function resolveResourceIds(Document $migration): array { - $parent = (string) $migration->getAttribute('parentResourceId', ''); - $resource = (string) $migration->getAttribute('resourceId', ''); + $context = $this->resolveResourceContext($migration); - if ($parent !== '') { - return [$parent, $resource]; + if ($context['parentResourceId'] !== '') { + return [$context['parentResourceId'], $context['resourceId']]; } - if (\str_contains($resource, ':')) { - return \explode(':', $resource, 2); + return [$context['resourceId'], '']; + } + + /** + * @return array{resourceId: string, resourceInternalId: string, resourceType: string, parentResourceId: string, parentResourceInternalId: string, parentResourceType: string} + */ + protected function resolveResourceContext(Document $migration): array + { + $context = [ + 'resourceId' => (string) $migration->getAttribute('resourceId', ''), + 'resourceInternalId' => (string) $migration->getAttribute('resourceInternalId', ''), + 'resourceType' => (string) $migration->getAttribute('resourceType', ''), + 'parentResourceId' => (string) $migration->getAttribute('parentResourceId', ''), + 'parentResourceInternalId' => (string) $migration->getAttribute('parentResourceInternalId', ''), + 'parentResourceType' => (string) $migration->getAttribute('parentResourceType', ''), + ]; + + if ( + $context['parentResourceId'] === '' + && \array_key_exists($context['resourceType'], Resource::DATABASE_TYPE_RESOURCE_MAP) + && \str_contains($context['resourceId'], ':') + ) { + [$context['parentResourceId'], $context['resourceId']] = \explode(':', $context['resourceId'], 2); + $context['parentResourceType'] = $context['resourceType']; + $context['resourceType'] = Resource::TYPE_COLLECTION; } - return [$resource, '']; + return $context; } /** diff --git a/tests/unit/Platform/Workers/MigrationsTest.php b/tests/unit/Platform/Workers/MigrationsTest.php index af75e2922b..b4431a49a8 100644 --- a/tests/unit/Platform/Workers/MigrationsTest.php +++ b/tests/unit/Platform/Workers/MigrationsTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\Migration\Destination; +use Utopia\Migration\Resource; use Utopia\Migration\Source; use Utopia\Queue\Publisher; use Utopia\Queue\Queue; @@ -144,6 +145,47 @@ final class MigrationsTest extends TestCase ], $events); } + public function testResourceContextUsesCanonicalRelationFields(): void + { + $worker = new class () extends Migrations { + /** + * @return array{resourceId: string, resourceInternalId: string, resourceType: string, parentResourceId: string, parentResourceInternalId: string, parentResourceType: string} + */ + public function context(Document $migration): array + { + return $this->resolveResourceContext($migration); + } + }; + + $this->assertSame([ + 'resourceId' => 'table-a', + 'resourceInternalId' => '201', + 'resourceType' => Resource::TYPE_COLLECTION, + 'parentResourceId' => 'database-a', + 'parentResourceInternalId' => '101', + 'parentResourceType' => Resource::TYPE_DATABASE, + ], $worker->context(new Document([ + 'resourceId' => 'table-a', + 'resourceInternalId' => '201', + 'resourceType' => Resource::TYPE_COLLECTION, + 'parentResourceId' => 'database-a', + 'parentResourceInternalId' => '101', + 'parentResourceType' => Resource::TYPE_DATABASE, + ]))); + + $this->assertSame([ + 'resourceId' => 'table-a', + 'resourceInternalId' => '', + 'resourceType' => Resource::TYPE_COLLECTION, + 'parentResourceId' => 'database-a', + 'parentResourceInternalId' => '', + 'parentResourceType' => Resource::TYPE_DATABASE, + ], $worker->context(new Document([ + 'resourceId' => 'database-a:table-a', + 'resourceType' => Resource::TYPE_DATABASE, + ]))); + } + private function createSourceMock(): Source&MockObject { $target = $this->createMock(Source::class);