diff --git a/app/config/collections/projects.php b/app/config/collections/projects.php index c4aa686b72..052103c9ac 100644 --- a/app/config/collections/projects.php +++ b/app/config/collections/projects.php @@ -2322,6 +2322,28 @@ return [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('destinationResourceId'), + '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, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], ], 'indexes' => [ [ @@ -2387,6 +2409,20 @@ return [ 'lengths' => [Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC], ], + [ + '$id' => '_key_destinationResourceId', + 'type' => Database::INDEX_KEY, + 'attributes' => ['destinationResourceId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => '_key_destinationResourceType', + 'type' => Database::INDEX_KEY, + 'attributes' => ['destinationResourceType'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], [ '$id' => ID::custom('_fulltext_search'), 'type' => Database::INDEX_FULLTEXT, diff --git a/src/Appwrite/Migration/Version/V25.php b/src/Appwrite/Migration/Version/V25.php index cf8eeb6ba9..7d57715128 100644 --- a/src/Appwrite/Migration/Version/V25.php +++ b/src/Appwrite/Migration/Version/V25.php @@ -42,6 +42,8 @@ class V25 extends Migration 'parentResourceId', 'parentResourceInternalId', 'parentResourceType', + 'destinationResourceId', + 'destinationResourceType', ]; try { $this->createAttributesFromCollection($this->dbForProject, $id, $attributes); @@ -55,6 +57,8 @@ class V25 extends Migration '_key_parentResourceId', '_key_parentResourceType', '_key_parentResourceInternalId', + '_key_destinationResourceId', + '_key_destinationResourceType', ]; foreach ($indexes as $index) { try { 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 bae41c756d..7dddb7b575 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 @@ -196,6 +196,8 @@ class Create extends Action 'parentResourceId' => $database->getId(), 'parentResourceInternalId' => $database->getSequence(), 'parentResourceType' => $parentResourceType, + 'destinationResourceId' => $database->getId(), + 'destinationResourceType' => $parentResourceType, 'statusCounters' => '{}', 'resourceData' => '{}', 'errors' => [], 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 365f8c213b..06101e8565 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 @@ -192,6 +192,8 @@ class Create extends Action 'parentResourceId' => $database->getId(), 'parentResourceInternalId' => $database->getSequence(), 'parentResourceType' => $parentResourceType, + 'destinationResourceId' => $database->getId(), + 'destinationResourceType' => $parentResourceType, 'statusCounters' => '{}', 'resourceData' => '{}', 'errors' => [], diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Migrations.php b/src/Appwrite/Utopia/Database/Validator/Queries/Migrations.php index c1c2cea344..a146c39d10 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Migrations.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Migrations.php @@ -14,6 +14,8 @@ class Migrations extends Base 'resourceType', 'parentResourceId', 'parentResourceType', + 'destinationResourceId', + 'destinationResourceType', 'statusCounters', 'resourceData', 'errors', diff --git a/src/Appwrite/Utopia/Response/Filters/V27.php b/src/Appwrite/Utopia/Response/Filters/V27.php index 1cf518f14d..57bad7171e 100644 --- a/src/Appwrite/Utopia/Response/Filters/V27.php +++ b/src/Appwrite/Utopia/Response/Filters/V27.php @@ -35,6 +35,8 @@ class V27 extends Filter unset($content['parentResourceId']); unset($content['parentResourceInternalId']); unset($content['parentResourceType']); + unset($content['destinationResourceId']); + unset($content['destinationResourceType']); return $content; } diff --git a/src/Appwrite/Utopia/Response/Model/Migration.php b/src/Appwrite/Utopia/Response/Model/Migration.php index 0fbf7362b7..49463b0c9a 100644 --- a/src/Appwrite/Utopia/Response/Model/Migration.php +++ b/src/Appwrite/Utopia/Response/Model/Migration.php @@ -102,6 +102,20 @@ class Migration extends Model 'example' => 'database', 'array' => false ]) + ->addRule('destinationResourceId', [ + 'type' => self::TYPE_STRING, + 'description' => 'ID of the destination resource created or overwritten by the migration.', + 'default' => '', + 'example' => 'databaseId', + 'array' => false + ]) + ->addRule('destinationResourceType', [ + 'type' => self::TYPE_STRING, + 'description' => 'Type of the destination resource created or overwritten by the migration.', + 'default' => '', + 'example' => 'database', + 'array' => false + ]) ->addRule('statusCounters', [ 'type' => self::TYPE_JSON, 'description' => 'A group of counters that represent the total progress of the migration.', diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 1f131cb07b..c440fa32ce 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -3496,8 +3496,10 @@ trait MigrationsBase /** * Import documents from a CSV file. + * + * @return array{databaseId: string, tableId: string, migrationId: string} */ - public function testCreateCSVImport(): void + public function testCreateCSVImport(): array { // Make a database $response = $this->client->call(Client::METHOD_POST, '/databases', [ @@ -3799,6 +3801,76 @@ trait MigrationsBase $this->assertArrayHasKey(Resource::TYPE_ROW, $migration['body']['statusCounters']); $this->assertEquals(25, $migration['body']['statusCounters'][Resource::TYPE_ROW]['success']); }, 10_000, 500); + + return [ + 'databaseId' => $databaseId, + 'tableId' => $tableId, + 'migrationId' => $migration['body']['$id'], + ]; + } + + /** + * @param array{databaseId: string, tableId: string, migrationId: string} $data + */ + #[Depends('testCreateCSVImport')] + public function testListMigrationsByDatabaseResource(array $data): void + { + $databaseId = $data['databaseId']; + $query = Query::or([ + Query::and([ + Query::equal('resourceId', [$databaseId]), + Query::equal('resourceType', [Resource::TYPE_DATABASE]), + ]), + Query::and([ + Query::equal('parentResourceId', [$databaseId]), + Query::equal('parentResourceType', [Resource::TYPE_DATABASE]), + ]), + Query::and([ + Query::equal('destinationResourceId', [$databaseId]), + Query::equal('destinationResourceType', [Resource::TYPE_DATABASE]), + ]), + ]); + + $response = $this->client->call(Client::METHOD_GET, '/migrations', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + $query->toString(), + Query::limit(100)->toString(), + ], + ]); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertContains($data['migrationId'], array_column($response['body']['migrations'], '$id')); + + $migration = null; + foreach ($response['body']['migrations'] as $candidate) { + if ($candidate['$id'] === $data['migrationId']) { + $migration = $candidate; + break; + } + } + + $this->assertNotNull($migration); + $this->assertSame($data['tableId'], $migration['resourceId']); + $this->assertSame($databaseId, $migration['parentResourceId']); + $this->assertSame(Resource::TYPE_DATABASE, $migration['parentResourceType']); + $this->assertSame($databaseId, $migration['destinationResourceId']); + $this->assertSame(Resource::TYPE_DATABASE, $migration['destinationResourceType']); + + $response = $this->client->call(Client::METHOD_GET, '/migrations', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + Query::equal('destinationResourceId', [$databaseId])->toString(), + Query::equal('destinationResourceType', [Resource::TYPE_DATABASE])->toString(), + ], + ]); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertContains($data['migrationId'], array_column($response['body']['migrations'], '$id')); } /** diff --git a/tests/unit/Utopia/Response/Filters/V27Test.php b/tests/unit/Utopia/Response/Filters/V27Test.php index ca18768a90..e2420ae332 100644 --- a/tests/unit/Utopia/Response/Filters/V27Test.php +++ b/tests/unit/Utopia/Response/Filters/V27Test.php @@ -22,6 +22,8 @@ final class V27Test extends TestCase 'parentResourceId' => 'database', 'parentResourceInternalId' => '10', 'parentResourceType' => 'database', + 'destinationResourceId' => 'destination', + 'destinationResourceType' => 'database', ], Response::MODEL_MIGRATION); $this->assertSame([ @@ -43,6 +45,8 @@ final class V27Test extends TestCase 'parentResourceId' => '', 'parentResourceInternalId' => '', 'parentResourceType' => '', + 'destinationResourceId' => '', + 'destinationResourceType' => '', ], Response::MODEL_MIGRATION); $this->assertSame([ @@ -64,6 +68,8 @@ final class V27Test extends TestCase 'resourceType' => 'collection', 'parentResourceId' => 'database', 'parentResourceType' => 'database', + 'destinationResourceId' => 'destination', + 'destinationResourceType' => 'database', ]], ], Response::MODEL_MIGRATION_LIST);