From d44820e00c956b5c671c61320951622861acd299 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 15 Apr 2026 14:15:08 +0100 Subject: [PATCH 01/40] Add overwrite and skip options to migration imports Exposes two new optional boolean params on the three migration creation endpoints so CSV / JSON / appwrite-to-appwrite imports can choose how to handle rows whose IDs already exist at the destination. Endpoints updated (app/controllers/api/migrations.php): - POST /v1/migrations/appwrite - POST /v1/migrations/csv/imports - POST /v1/migrations/json/imports Parameter semantics: - overwrite=true -> destination uses upsertDocuments instead of createDocuments; existing rows are replaced with imported values - skip=true -> destination wraps createDocuments in skipDuplicates; existing rows are preserved unchanged, duplicate-id rows silently no-op - both false -> default; fails fast on DuplicateException (original behavior, unchanged) - both true -> overwrite wins (upsert subsumes skip) Both params are stored in the migration Document's options array (matches the existing pattern for destination behavior config like path, size, delimiter, bucketId, etc.) and read back in the worker's processDestination() to instantiate DestinationAppwrite with the new constructor params. Feature-branch note: depends on utopia-php/migration#feat/skip-duplicates (DestinationAppwrite constructor params) which in turn depends on utopia-php/database#852 (skipDuplicates scope guard). composer.json is temporarily pinned to dev-feat/skip-duplicates and dev-csv-import-upsert-v2 respectively; both must be reset to proper release versions once the upstream PRs merge. --- app/controllers/api/migrations.php | 20 +++++- composer.json | 4 +- composer.lock | 72 ++++++++++++-------- src/Appwrite/Platform/Workers/Migrations.php | 2 + 4 files changed, 67 insertions(+), 31 deletions(-) diff --git a/app/controllers/api/migrations.php b/app/controllers/api/migrations.php index 4c541d2817..f19598c198 100644 --- a/app/controllers/api/migrations.php +++ b/app/controllers/api/migrations.php @@ -87,13 +87,15 @@ Http::post('/v1/migrations/appwrite') ->param('endpoint', '', new URL(), 'Source Appwrite endpoint') ->param('projectId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Source Project ID', false, ['dbForProject']) ->param('apiKey', '', new Text(512), 'Source API Key') + ->param('overwrite', false, new Boolean(), 'When true, replace existing rows by calling upsertDocuments instead of createDocuments. Rows with matching IDs will be updated with the imported values.', true) + ->param('skip', false, new Boolean(), 'When true, silently ignore rows whose IDs already exist in the destination. Existing rows are preserved unchanged.', true) ->inject('response') ->inject('dbForProject') ->inject('project') ->inject('platform') ->inject('queueForEvents') ->inject('publisherForMigrations') - ->action(function (array $resources, string $endpoint, string $projectId, string $apiKey, Response $response, Database $dbForProject, Document $project, array $platform, Event $queueForEvents, MigrationPublisher $publisherForMigrations) { + ->action(function (array $resources, string $endpoint, string $projectId, string $apiKey, bool $overwrite, bool $skip, Response $response, Database $dbForProject, Document $project, array $platform, Event $queueForEvents, MigrationPublisher $publisherForMigrations) { $migration = $dbForProject->createDocument('migrations', new Document([ '$id' => ID::unique(), 'status' => 'pending', @@ -109,6 +111,10 @@ Http::post('/v1/migrations/appwrite') 'statusCounters' => '{}', 'resourceData' => '{}', 'errors' => [], + 'options' => [ + 'overwrite' => $overwrite, + 'skip' => $skip, + ], ])); $queueForEvents->setParam('migrationId', $migration->getId()); @@ -352,6 +358,8 @@ Http::post('/v1/migrations/csv/imports') ->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('internalFile', false, new Boolean(), 'Is the file stored in an internal bucket?', true) + ->param('overwrite', false, new Boolean(), 'When true, replace existing rows by calling upsertDocuments instead of createDocuments. Rows with matching IDs will be updated with the imported values.', true) + ->param('skip', false, new Boolean(), 'When true, silently ignore rows whose IDs already exist in the destination. Existing rows are preserved unchanged.', true) ->inject('response') ->inject('dbForProject') ->inject('dbForPlatform') @@ -367,6 +375,8 @@ Http::post('/v1/migrations/csv/imports') string $fileId, string $resourceId, bool $internalFile, + bool $overwrite, + bool $skip, Response $response, Database $dbForProject, Database $dbForPlatform, @@ -467,6 +477,8 @@ Http::post('/v1/migrations/csv/imports') 'options' => [ 'path' => $newPath, 'size' => $fileSize, + 'overwrite' => $overwrite, + 'skip' => $skip, ], ])); @@ -656,6 +668,8 @@ Http::post('/v1/migrations/json/imports') ->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('internalFile', false, new Boolean(), 'Is the file stored in an internal bucket?', true) + ->param('overwrite', false, new Boolean(), 'When true, replace existing rows by calling upsertDocuments instead of createDocuments. Rows with matching IDs will be updated with the imported values.', true) + ->param('skip', false, new Boolean(), 'When true, silently ignore rows whose IDs already exist in the destination. Existing rows are preserved unchanged.', true) ->inject('response') ->inject('dbForProject') ->inject('dbForPlatform') @@ -671,6 +685,8 @@ Http::post('/v1/migrations/json/imports') string $fileId, string $resourceId, bool $internalFile, + bool $overwrite, + bool $skip, Response $response, Database $dbForProject, Database $dbForPlatform, @@ -770,6 +786,8 @@ Http::post('/v1/migrations/json/imports') 'options' => [ 'path' => $newPath, 'size' => $fileSize, + 'overwrite' => $overwrite, + 'skip' => $skip, ], ])); diff --git a/composer.json b/composer.json index 3aa6d157cf..2dbe5616e5 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,7 @@ "utopia-php/compression": "0.1.*", "utopia-php/config": "1.*", "utopia-php/console": "0.1.*", - "utopia-php/database": "5.*", + "utopia-php/database": "dev-csv-import-upsert-v2 as 5.99.0", "utopia-php/detector": "0.2.*", "utopia-php/domains": "1.*", "utopia-php/emails": "0.6.*", @@ -73,7 +73,7 @@ "utopia-php/locale": "0.8.*", "utopia-php/logger": "0.6.*", "utopia-php/messaging": "0.22.*", - "utopia-php/migration": "1.9.*", + "utopia-php/migration": "dev-feat/skip-duplicates as 1.9.99", "utopia-php/platform": "0.12.*", "utopia-php/pools": "1.*", "utopia-php/span": "1.1.*", diff --git a/composer.lock b/composer.lock index bc3d9d30bf..e93af8906a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f6a87c1012b316e614258f8f57a28e48", + "content-hash": "0cabf47b85d8fac9a1f78df82b0add1f", "packages": [ { "name": "adhocore/jwt", @@ -2887,7 +2887,7 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.34.0", + "version": "v1.35.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", @@ -2948,7 +2948,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.34.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.35.0" }, "funding": [ { @@ -2972,7 +2972,7 @@ }, { "name": "symfony/polyfill-php82", - "version": "v1.34.0", + "version": "v1.35.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php82.git", @@ -3028,7 +3028,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php82/tree/v1.34.0" + "source": "https://github.com/symfony/polyfill-php82/tree/v1.35.0" }, "funding": [ { @@ -3052,7 +3052,7 @@ }, { "name": "symfony/polyfill-php83", - "version": "v1.34.0", + "version": "v1.35.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php83.git", @@ -3108,7 +3108,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php83/tree/v1.34.0" + "source": "https://github.com/symfony/polyfill-php83/tree/v1.35.0" }, "funding": [ { @@ -3132,7 +3132,7 @@ }, { "name": "symfony/polyfill-php85", - "version": "v1.34.0", + "version": "v1.35.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php85.git", @@ -3188,7 +3188,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php85/tree/v1.34.0" + "source": "https://github.com/symfony/polyfill-php85/tree/v1.35.0" }, "funding": [ { @@ -3850,16 +3850,16 @@ }, { "name": "utopia-php/database", - "version": "5.3.21", + "version": "dev-csv-import-upsert-v2", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "ee2d7d4c87b3a3fae954089ad7494ceb454f619d" + "reference": "52b189bded7ef409bb978483a7231d779051b510" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/ee2d7d4c87b3a3fae954089ad7494ceb454f619d", - "reference": "ee2d7d4c87b3a3fae954089ad7494ceb454f619d", + "url": "https://api.github.com/repos/utopia-php/database/zipball/52b189bded7ef409bb978483a7231d779051b510", + "reference": "52b189bded7ef409bb978483a7231d779051b510", "shasum": "" }, "require": { @@ -3903,9 +3903,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/5.3.21" + "source": "https://github.com/utopia-php/database/tree/csv-import-upsert-v2" }, - "time": "2026-04-10T12:38:57+00:00" + "time": "2026-04-15T10:53:53+00:00" }, { "name": "utopia-php/detector", @@ -4526,16 +4526,16 @@ }, { "name": "utopia-php/migration", - "version": "1.9.1", + "version": "dev-feat/skip-duplicates", "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "7a86aeadf182b63a9f4ceba7e137588b31c5d2e2" + "reference": "2012cda162ad0ab79678c924d7534de6f3ec85ad" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/7a86aeadf182b63a9f4ceba7e137588b31c5d2e2", - "reference": "7a86aeadf182b63a9f4ceba7e137588b31c5d2e2", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/2012cda162ad0ab79678c924d7534de6f3ec85ad", + "reference": "2012cda162ad0ab79678c924d7534de6f3ec85ad", "shasum": "" }, "require": { @@ -4544,7 +4544,7 @@ "ext-openssl": "*", "halaxa/json-machine": "^1.2", "php": ">=8.1", - "utopia-php/database": "5.*", + "utopia-php/database": "dev-csv-import-upsert-v2 as 5.99.0", "utopia-php/dsn": "0.2.*", "utopia-php/storage": "1.0.*" }, @@ -4575,9 +4575,9 @@ ], "support": { "issues": "https://github.com/utopia-php/migration/issues", - "source": "https://github.com/utopia-php/migration/tree/1.9.1" + "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-03-25T07:05:27+00:00" + "time": "2026-04-15T12:36:51+00:00" }, { "name": "utopia-php/mongo", @@ -7778,7 +7778,7 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.34.0", + "version": "v1.35.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", @@ -7837,7 +7837,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.34.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.35.0" }, "funding": [ { @@ -8028,7 +8028,7 @@ }, { "name": "symfony/polyfill-php81", - "version": "v1.34.0", + "version": "v1.35.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php81.git", @@ -8084,7 +8084,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php81/tree/v1.34.0" + "source": "https://github.com/symfony/polyfill-php81/tree/v1.35.0" }, "funding": [ { @@ -8440,9 +8440,25 @@ "time": "2024-11-07T12:36:22+00:00" } ], - "aliases": [], + "aliases": [ + { + "package": "utopia-php/database", + "version": "dev-csv-import-upsert-v2", + "alias": "5.99.0", + "alias_normalized": "5.99.0.0" + }, + { + "package": "utopia-php/migration", + "version": "dev-feat/skip-duplicates", + "alias": "1.9.99", + "alias_normalized": "1.9.99.0" + } + ], "minimum-stability": "dev", - "stability-flags": {}, + "stability-flags": { + "utopia-php/database": 20, + "utopia-php/migration": 20 + }, "prefer-stable": true, "prefer-lowest": false, "platform": { diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 118ff7acf9..710b24c19d 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -276,6 +276,8 @@ class Migrations extends Action $this->dbForProject, $this->getDatabasesDB, Config::getParam('collections', [])['databases']['collections'], + $options['overwrite'] ?? false, + $options['skip'] ?? false, ), DestinationCSV::getName() => new DestinationCSV( $this->deviceForFiles, From c5fe71684af7582eb439701540ce50212efc1199 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 15 Apr 2026 14:23:02 +0100 Subject: [PATCH 02/40] Add E2E tests for CSV import overwrite/skip flags Three new test methods in MigrationsBase, following the existing testCreateCSVImport setup pattern: - testCreateCSVImportSkipDuplicates Seeds documents.csv, mutates one row, re-imports with skip=true. Asserts the mutated row keeps its mutated value (not overwritten by the CSV's original value) and the row count stays at 100. - testCreateCSVImportOverwrite Seeds documents.csv, mutates one row, re-imports with overwrite=true. Asserts the mutated row is restored to the CSV's original value (proving upsertDocuments actually replaced the row) and the row count stays at 100. - testCreateCSVImportDefaultFailsOnDuplicate Regression guard: re-imports documents.csv with no flags. Asserts the migration goes to status=failed with errors populated, proving the default duplicate-throws behavior is preserved. All three share a prepareCsvImportFixture() helper that sets up database + table (name, age columns) + bucket + documents.csv upload. Returns the known first-row id + original name/age so tests can mutate and assert on a predictable row. Reuses the existing documents.csv fixture (100 rows with \$id as the first column). No new fixture files needed. --- .../Services/Migrations/MigrationsBase.php | 244 ++++++++++++++++++ 1 file changed, 244 insertions(+) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 9e9ce2fbcd..37036cadc2 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1482,6 +1482,250 @@ trait MigrationsBase }, 10_000, 500); } + /** + * Set up a database + table + bucket + uploaded CSV for the skip/overwrite tests. + * Returns [$databaseId, $tableId, $bucketId, $fileId, $firstRowId, $firstRowName, $firstRowAge]. + * + * @return array{string,string,string,string,string,string,int} + */ + private function prepareCsvImportFixture(string $testLabel): array + { + $headers = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]; + + // database + $response = $this->client->call(Client::METHOD_POST, '/databases', $headers, [ + 'databaseId' => ID::unique(), + 'name' => 'Test DB ' . $testLabel, + ]); + $this->assertEquals(201, $response['headers']['status-code']); + $databaseId = $response['body']['$id']; + + // table + $response = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', $headers, [ + 'name' => 'Test table ' . $testLabel, + 'tableId' => ID::unique(), + ]); + $this->assertEquals(201, $response['headers']['status-code']); + $tableId = $response['body']['$id']; + + // columns: name, age (match documents.csv fixture) + $response = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', $headers, [ + 'key' => 'name', + 'size' => 256, + 'required' => true, + ]); + $this->assertEquals(202, $response['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/integer', $headers, [ + 'key' => 'age', + 'min' => 18, + 'max' => 65, + 'required' => true, + ]); + $this->assertEquals(202, $response['headers']['status-code']); + + // bucket + $response = $this->client->call(Client::METHOD_POST, '/storage/buckets', $headers, [ + 'bucketId' => ID::unique(), + 'name' => 'Bucket ' . $testLabel, + 'maximumFileSize' => 2000000, + 'allowedFileExtensions' => ['csv'], + ]); + $this->assertEquals(201, $response['headers']['status-code']); + $bucketId = $response['body']['$id']; + + // upload documents.csv (100 rows with $id, name, age columns) + $response = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'fileId' => ID::unique(), + 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/csv/documents.csv'), 'text/csv', 'documents.csv'), + ]); + $this->assertEquals(201, $response['headers']['status-code']); + $fileId = $response['body']['$id']; + + // first row in documents.csv: hxfcwpcas5xokpwe,Diamond Mendez,56 + return [$databaseId, $tableId, $bucketId, $fileId, 'hxfcwpcas5xokpwe', 'Diamond Mendez', 56]; + } + + /** + * skip=true on re-import: duplicates are silently no-op'd, existing rows preserved unchanged. + */ + public function testCreateCSVImportSkipDuplicates(): void + { + [$databaseId, $tableId, $bucketId, $fileId, $rowId, $originalName, $originalAge] = $this->prepareCsvImportFixture('skip'); + + // First import: 100 rows created + $first = $this->performCsvMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + ]); + $this->assertEventually(function () use ($first) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('completed', $migration['body']['status']); + $this->assertEquals(100, $migration['body']['statusCounters'][Resource::TYPE_ROW]['success']); + }, 10_000, 500); + + // Mutate one row so we can prove skip does NOT overwrite it + $mutate = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'data' => ['age' => 22], + ]); + $this->assertEquals(200, $mutate['headers']['status-code']); + $this->assertEquals(22, $mutate['body']['age']); + + // Second import with skip=true: no errors, mutated row preserved + $second = $this->performCsvMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + 'skip' => true, + ]); + $this->assertEventually(function () use ($second) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('completed', $migration['body']['status']); + }, 10_000, 500); + + // Mutated row kept its mutated value (not overwritten by CSV's original age) + $row = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals(200, $row['headers']['status-code']); + $this->assertEquals($originalName, $row['body']['name']); + $this->assertEquals(22, $row['body']['age'], 'skip=true must not overwrite mutated row'); + + // Row count still 100 (no duplicates created) + $rows = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [Query::limit(150)->toString()], + ]); + $this->assertEquals(100, $rows['body']['total']); + } + + /** + * overwrite=true on re-import: existing rows are replaced with imported values. + */ + public function testCreateCSVImportOverwrite(): void + { + [$databaseId, $tableId, $bucketId, $fileId, $rowId, $originalName, $originalAge] = $this->prepareCsvImportFixture('overwrite'); + + // First import: 100 rows created + $first = $this->performCsvMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + ]); + $this->assertEventually(function () use ($first) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('completed', $migration['body']['status']); + $this->assertEquals(100, $migration['body']['statusCounters'][Resource::TYPE_ROW]['success']); + }, 10_000, 500); + + // Mutate one row so we can prove overwrite restores it to the CSV's original value + $mutate = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'data' => ['age' => 22], + ]); + $this->assertEquals(200, $mutate['headers']['status-code']); + $this->assertEquals(22, $mutate['body']['age']); + + // Second import with overwrite=true: mutated row restored to CSV value + $second = $this->performCsvMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + 'overwrite' => true, + ]); + $this->assertEventually(function () use ($second) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('completed', $migration['body']['status']); + }, 10_000, 500); + + // Mutated row is back to CSV's original age (proving overwrite actually replaced the row) + $row = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals(200, $row['headers']['status-code']); + $this->assertEquals($originalName, $row['body']['name']); + $this->assertEquals($originalAge, $row['body']['age'], 'overwrite=true must restore row to imported value'); + + // Row count still 100 (no duplicates created) + $rows = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [Query::limit(150)->toString()], + ]); + $this->assertEquals(100, $rows['body']['total']); + } + + /** + * Default behavior (neither flag): re-import of duplicate ids fails with DuplicateException. + * Regression guard so the skip/overwrite additions don't silently change the default. + */ + public function testCreateCSVImportDefaultFailsOnDuplicate(): void + { + [$databaseId, $tableId, $bucketId, $fileId] = $this->prepareCsvImportFixture('default'); + + // First import: succeeds + $first = $this->performCsvMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + ]); + $this->assertEventually(function () use ($first) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('completed', $migration['body']['status']); + }, 10_000, 500); + + // Second import with no flags: should fail on duplicate ids + $second = $this->performCsvMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + ]); + $this->assertEventually(function () use ($second) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('finished', $migration['body']['stage']); + $this->assertEquals('failed', $migration['body']['status']); + $this->assertNotEmpty($migration['body']['errors']); + }, 60_000, 500); + } + private function performCsvMigration(array $body): array { return $this->client->call(Client::METHOD_POST, '/migrations/csv', [ From 8fa28257bedc2915ec885e92bce4c2797821a0b1 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 20 Apr 2026 10:28:05 +0100 Subject: [PATCH 03/40] Migrations: replace overwrite/skip with onDuplicate enum string, bump database to 5.3.22 --- app/controllers/api/migrations.php | 27 ++++----- composer.json | 2 +- composer.lock | 55 ++++++++----------- src/Appwrite/Platform/Workers/Migrations.php | 3 +- .../Services/Migrations/MigrationsBase.php | 20 +++---- 5 files changed, 46 insertions(+), 61 deletions(-) diff --git a/app/controllers/api/migrations.php b/app/controllers/api/migrations.php index f19598c198..5868695bd3 100644 --- a/app/controllers/api/migrations.php +++ b/app/controllers/api/migrations.php @@ -26,6 +26,7 @@ use Utopia\Database\Validator\Queries\Documents; use Utopia\Database\Validator\Query\Cursor; use Utopia\Database\Validator\UID; use Utopia\Http\Http; +use Utopia\Migration\Destinations\Appwrite as DestinationAppwrite; use Utopia\Migration\Resource; use Utopia\Migration\Sources\Appwrite; use Utopia\Migration\Sources\CSV; @@ -87,15 +88,14 @@ Http::post('/v1/migrations/appwrite') ->param('endpoint', '', new URL(), 'Source Appwrite endpoint') ->param('projectId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Source Project ID', false, ['dbForProject']) ->param('apiKey', '', new Text(512), 'Source API Key') - ->param('overwrite', false, new Boolean(), 'When true, replace existing rows by calling upsertDocuments instead of createDocuments. Rows with matching IDs will be updated with the imported values.', true) - ->param('skip', false, new Boolean(), 'When true, silently ignore rows whose IDs already exist in the destination. Existing rows are preserved unchanged.', true) + ->param('onDuplicate', DestinationAppwrite::ON_DUPLICATE_FAIL, new WhiteList(DestinationAppwrite::ON_DUPLICATES), 'Behavior when a row with an existing $id is encountered. "fail" (default): abort on first conflict. "skip": silently ignore. "upsert": replace existing row.', true) ->inject('response') ->inject('dbForProject') ->inject('project') ->inject('platform') ->inject('queueForEvents') ->inject('publisherForMigrations') - ->action(function (array $resources, string $endpoint, string $projectId, string $apiKey, bool $overwrite, bool $skip, Response $response, Database $dbForProject, Document $project, array $platform, Event $queueForEvents, MigrationPublisher $publisherForMigrations) { + ->action(function (array $resources, string $endpoint, string $projectId, string $apiKey, string $onDuplicate, Response $response, Database $dbForProject, Document $project, array $platform, Event $queueForEvents, MigrationPublisher $publisherForMigrations) { $migration = $dbForProject->createDocument('migrations', new Document([ '$id' => ID::unique(), 'status' => 'pending', @@ -112,8 +112,7 @@ Http::post('/v1/migrations/appwrite') 'resourceData' => '{}', 'errors' => [], 'options' => [ - 'overwrite' => $overwrite, - 'skip' => $skip, + 'onDuplicate' => $onDuplicate, ], ])); @@ -358,8 +357,7 @@ Http::post('/v1/migrations/csv/imports') ->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('internalFile', false, new Boolean(), 'Is the file stored in an internal bucket?', true) - ->param('overwrite', false, new Boolean(), 'When true, replace existing rows by calling upsertDocuments instead of createDocuments. Rows with matching IDs will be updated with the imported values.', true) - ->param('skip', false, new Boolean(), 'When true, silently ignore rows whose IDs already exist in the destination. Existing rows are preserved unchanged.', true) + ->param('onDuplicate', DestinationAppwrite::ON_DUPLICATE_FAIL, new WhiteList(DestinationAppwrite::ON_DUPLICATES), 'Behavior when a row with an existing $id is encountered. "fail" (default): abort on first conflict. "skip": silently ignore. "upsert": replace existing row.', true) ->inject('response') ->inject('dbForProject') ->inject('dbForPlatform') @@ -375,8 +373,7 @@ Http::post('/v1/migrations/csv/imports') string $fileId, string $resourceId, bool $internalFile, - bool $overwrite, - bool $skip, + string $onDuplicate, Response $response, Database $dbForProject, Database $dbForPlatform, @@ -477,8 +474,7 @@ Http::post('/v1/migrations/csv/imports') 'options' => [ 'path' => $newPath, 'size' => $fileSize, - 'overwrite' => $overwrite, - 'skip' => $skip, + 'onDuplicate' => $onDuplicate, ], ])); @@ -668,8 +664,7 @@ Http::post('/v1/migrations/json/imports') ->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('internalFile', false, new Boolean(), 'Is the file stored in an internal bucket?', true) - ->param('overwrite', false, new Boolean(), 'When true, replace existing rows by calling upsertDocuments instead of createDocuments. Rows with matching IDs will be updated with the imported values.', true) - ->param('skip', false, new Boolean(), 'When true, silently ignore rows whose IDs already exist in the destination. Existing rows are preserved unchanged.', true) + ->param('onDuplicate', DestinationAppwrite::ON_DUPLICATE_FAIL, new WhiteList(DestinationAppwrite::ON_DUPLICATES), 'Behavior when a row with an existing $id is encountered. "fail" (default): abort on first conflict. "skip": silently ignore. "upsert": replace existing row.', true) ->inject('response') ->inject('dbForProject') ->inject('dbForPlatform') @@ -685,8 +680,7 @@ Http::post('/v1/migrations/json/imports') string $fileId, string $resourceId, bool $internalFile, - bool $overwrite, - bool $skip, + string $onDuplicate, Response $response, Database $dbForProject, Database $dbForPlatform, @@ -786,8 +780,7 @@ Http::post('/v1/migrations/json/imports') 'options' => [ 'path' => $newPath, 'size' => $fileSize, - 'overwrite' => $overwrite, - 'skip' => $skip, + 'onDuplicate' => $onDuplicate, ], ])); diff --git a/composer.json b/composer.json index 2dbe5616e5..d8bb03d120 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,7 @@ "utopia-php/compression": "0.1.*", "utopia-php/config": "1.*", "utopia-php/console": "0.1.*", - "utopia-php/database": "dev-csv-import-upsert-v2 as 5.99.0", + "utopia-php/database": "5.3.22", "utopia-php/detector": "0.2.*", "utopia-php/domains": "1.*", "utopia-php/emails": "0.6.*", diff --git a/composer.lock b/composer.lock index e93af8906a..de21fbef5e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0cabf47b85d8fac9a1f78df82b0add1f", + "content-hash": "cce51bafc6cbb73585a51e7495b2e6cc", "packages": [ { "name": "adhocore/jwt", @@ -2887,7 +2887,7 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.35.0", + "version": "v1.36.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", @@ -2948,7 +2948,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.35.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.36.0" }, "funding": [ { @@ -2972,7 +2972,7 @@ }, { "name": "symfony/polyfill-php82", - "version": "v1.35.0", + "version": "v1.36.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php82.git", @@ -3028,7 +3028,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php82/tree/v1.35.0" + "source": "https://github.com/symfony/polyfill-php82/tree/v1.36.0" }, "funding": [ { @@ -3052,7 +3052,7 @@ }, { "name": "symfony/polyfill-php83", - "version": "v1.35.0", + "version": "v1.36.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php83.git", @@ -3108,7 +3108,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php83/tree/v1.35.0" + "source": "https://github.com/symfony/polyfill-php83/tree/v1.36.0" }, "funding": [ { @@ -3132,7 +3132,7 @@ }, { "name": "symfony/polyfill-php85", - "version": "v1.35.0", + "version": "v1.36.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php85.git", @@ -3188,7 +3188,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php85/tree/v1.35.0" + "source": "https://github.com/symfony/polyfill-php85/tree/v1.36.0" }, "funding": [ { @@ -3850,16 +3850,16 @@ }, { "name": "utopia-php/database", - "version": "dev-csv-import-upsert-v2", + "version": "5.3.22", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "52b189bded7ef409bb978483a7231d779051b510" + "reference": "d765945da6b3141852014b2f96ecf1fe7e3d6ba7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/52b189bded7ef409bb978483a7231d779051b510", - "reference": "52b189bded7ef409bb978483a7231d779051b510", + "url": "https://api.github.com/repos/utopia-php/database/zipball/d765945da6b3141852014b2f96ecf1fe7e3d6ba7", + "reference": "d765945da6b3141852014b2f96ecf1fe7e3d6ba7", "shasum": "" }, "require": { @@ -3903,9 +3903,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/csv-import-upsert-v2" + "source": "https://github.com/utopia-php/database/tree/5.3.22" }, - "time": "2026-04-15T10:53:53+00:00" + "time": "2026-04-20T07:12:46+00:00" }, { "name": "utopia-php/detector", @@ -4530,12 +4530,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "2012cda162ad0ab79678c924d7534de6f3ec85ad" + "reference": "6025318d61c15355015c0e34427480e84ad690ad" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/2012cda162ad0ab79678c924d7534de6f3ec85ad", - "reference": "2012cda162ad0ab79678c924d7534de6f3ec85ad", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/6025318d61c15355015c0e34427480e84ad690ad", + "reference": "6025318d61c15355015c0e34427480e84ad690ad", "shasum": "" }, "require": { @@ -4544,7 +4544,7 @@ "ext-openssl": "*", "halaxa/json-machine": "^1.2", "php": ">=8.1", - "utopia-php/database": "dev-csv-import-upsert-v2 as 5.99.0", + "utopia-php/database": "5.3.22", "utopia-php/dsn": "0.2.*", "utopia-php/storage": "1.0.*" }, @@ -4577,7 +4577,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-15T12:36:51+00:00" + "time": "2026-04-20T09:07:20+00:00" }, { "name": "utopia-php/mongo", @@ -7778,7 +7778,7 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.35.0", + "version": "v1.36.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", @@ -7837,7 +7837,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.35.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.36.0" }, "funding": [ { @@ -8028,7 +8028,7 @@ }, { "name": "symfony/polyfill-php81", - "version": "v1.35.0", + "version": "v1.36.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php81.git", @@ -8084,7 +8084,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php81/tree/v1.35.0" + "source": "https://github.com/symfony/polyfill-php81/tree/v1.36.0" }, "funding": [ { @@ -8441,12 +8441,6 @@ } ], "aliases": [ - { - "package": "utopia-php/database", - "version": "dev-csv-import-upsert-v2", - "alias": "5.99.0", - "alias_normalized": "5.99.0.0" - }, { "package": "utopia-php/migration", "version": "dev-feat/skip-duplicates", @@ -8456,7 +8450,6 @@ ], "minimum-stability": "dev", "stability-flags": { - "utopia-php/database": 20, "utopia-php/migration": 20 }, "prefer-stable": true, diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 710b24c19d..6a440e54c0 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -276,8 +276,7 @@ class Migrations extends Action $this->dbForProject, $this->getDatabasesDB, Config::getParam('collections', [])['databases']['collections'], - $options['overwrite'] ?? false, - $options['skip'] ?? false, + $options['onDuplicate'] ?? DestinationAppwrite::ON_DUPLICATE_FAIL, ), DestinationCSV::getName() => new DestinationCSV( $this->deviceForFiles, diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 37036cadc2..35f196ec49 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1554,7 +1554,7 @@ trait MigrationsBase } /** - * skip=true on re-import: duplicates are silently no-op'd, existing rows preserved unchanged. + * onDuplicate=skip on re-import: duplicates are silently no-op'd, existing rows preserved unchanged. */ public function testCreateCSVImportSkipDuplicates(): void { @@ -1586,12 +1586,12 @@ trait MigrationsBase $this->assertEquals(200, $mutate['headers']['status-code']); $this->assertEquals(22, $mutate['body']['age']); - // Second import with skip=true: no errors, mutated row preserved + // Second import with onDuplicate=skip: no errors, mutated row preserved $second = $this->performCsvMigration([ 'fileId' => $fileId, 'bucketId' => $bucketId, 'resourceId' => $databaseId . ':' . $tableId, - 'skip' => true, + 'onDuplicate' => 'skip', ]); $this->assertEventually(function () use ($second) { $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([ @@ -1608,7 +1608,7 @@ trait MigrationsBase ], $this->getHeaders())); $this->assertEquals(200, $row['headers']['status-code']); $this->assertEquals($originalName, $row['body']['name']); - $this->assertEquals(22, $row['body']['age'], 'skip=true must not overwrite mutated row'); + $this->assertEquals(22, $row['body']['age'], 'onDuplicate=skip must not overwrite mutated row'); // Row count still 100 (no duplicates created) $rows = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', array_merge([ @@ -1621,7 +1621,7 @@ trait MigrationsBase } /** - * overwrite=true on re-import: existing rows are replaced with imported values. + * onDuplicate=upsert on re-import: existing rows are replaced with imported values. */ public function testCreateCSVImportOverwrite(): void { @@ -1642,7 +1642,7 @@ trait MigrationsBase $this->assertEquals(100, $migration['body']['statusCounters'][Resource::TYPE_ROW]['success']); }, 10_000, 500); - // Mutate one row so we can prove overwrite restores it to the CSV's original value + // Mutate one row so we can prove upsert restores it to the CSV's original value $mutate = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, [ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -1653,12 +1653,12 @@ trait MigrationsBase $this->assertEquals(200, $mutate['headers']['status-code']); $this->assertEquals(22, $mutate['body']['age']); - // Second import with overwrite=true: mutated row restored to CSV value + // Second import with onDuplicate=upsert: mutated row restored to CSV value $second = $this->performCsvMigration([ 'fileId' => $fileId, 'bucketId' => $bucketId, 'resourceId' => $databaseId . ':' . $tableId, - 'overwrite' => true, + 'onDuplicate' => 'upsert', ]); $this->assertEventually(function () use ($second) { $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([ @@ -1668,14 +1668,14 @@ trait MigrationsBase $this->assertEquals('completed', $migration['body']['status']); }, 10_000, 500); - // Mutated row is back to CSV's original age (proving overwrite actually replaced the row) + // Mutated row is back to CSV's original age (proving upsert actually replaced the row) $row = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders())); $this->assertEquals(200, $row['headers']['status-code']); $this->assertEquals($originalName, $row['body']['name']); - $this->assertEquals($originalAge, $row['body']['age'], 'overwrite=true must restore row to imported value'); + $this->assertEquals($originalAge, $row['body']['age'], 'onDuplicate=upsert must restore row to imported value'); // Row count still 100 (no duplicates created) $rows = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', array_merge([ From 18f9dfb64e22c681320b5711038f7e8d007870be Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 20 Apr 2026 13:16:45 +0100 Subject: [PATCH 04/40] Bump utopia-php/migration to 1.9.2, use OnDuplicate enum --- app/controllers/api/migrations.php | 8 +++--- composer.json | 2 +- composer.lock | 29 +++++++------------- src/Appwrite/Platform/Workers/Migrations.php | 3 +- 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/app/controllers/api/migrations.php b/app/controllers/api/migrations.php index 5868695bd3..ef71f8f6ef 100644 --- a/app/controllers/api/migrations.php +++ b/app/controllers/api/migrations.php @@ -26,7 +26,7 @@ use Utopia\Database\Validator\Queries\Documents; use Utopia\Database\Validator\Query\Cursor; use Utopia\Database\Validator\UID; use Utopia\Http\Http; -use Utopia\Migration\Destinations\Appwrite as DestinationAppwrite; +use Utopia\Migration\Destinations\OnDuplicate; use Utopia\Migration\Resource; use Utopia\Migration\Sources\Appwrite; use Utopia\Migration\Sources\CSV; @@ -88,7 +88,7 @@ Http::post('/v1/migrations/appwrite') ->param('endpoint', '', new URL(), 'Source Appwrite endpoint') ->param('projectId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Source Project ID', false, ['dbForProject']) ->param('apiKey', '', new Text(512), 'Source API Key') - ->param('onDuplicate', DestinationAppwrite::ON_DUPLICATE_FAIL, new WhiteList(DestinationAppwrite::ON_DUPLICATES), 'Behavior when a row with an existing $id is encountered. "fail" (default): abort on first conflict. "skip": silently ignore. "upsert": replace existing row.', 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. "upsert": replace existing row.', true) ->inject('response') ->inject('dbForProject') ->inject('project') @@ -357,7 +357,7 @@ Http::post('/v1/migrations/csv/imports') ->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('internalFile', false, new Boolean(), 'Is the file stored in an internal bucket?', true) - ->param('onDuplicate', DestinationAppwrite::ON_DUPLICATE_FAIL, new WhiteList(DestinationAppwrite::ON_DUPLICATES), 'Behavior when a row with an existing $id is encountered. "fail" (default): abort on first conflict. "skip": silently ignore. "upsert": replace existing row.', 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. "upsert": replace existing row.', true) ->inject('response') ->inject('dbForProject') ->inject('dbForPlatform') @@ -664,7 +664,7 @@ Http::post('/v1/migrations/json/imports') ->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('internalFile', false, new Boolean(), 'Is the file stored in an internal bucket?', true) - ->param('onDuplicate', DestinationAppwrite::ON_DUPLICATE_FAIL, new WhiteList(DestinationAppwrite::ON_DUPLICATES), 'Behavior when a row with an existing $id is encountered. "fail" (default): abort on first conflict. "skip": silently ignore. "upsert": replace existing row.', 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. "upsert": replace existing row.', true) ->inject('response') ->inject('dbForProject') ->inject('dbForPlatform') diff --git a/composer.json b/composer.json index d8bb03d120..2f8ea37011 100644 --- a/composer.json +++ b/composer.json @@ -73,7 +73,7 @@ "utopia-php/locale": "0.8.*", "utopia-php/logger": "0.6.*", "utopia-php/messaging": "0.22.*", - "utopia-php/migration": "dev-feat/skip-duplicates as 1.9.99", + "utopia-php/migration": "1.9.2", "utopia-php/platform": "0.12.*", "utopia-php/pools": "1.*", "utopia-php/span": "1.1.*", diff --git a/composer.lock b/composer.lock index de21fbef5e..7e09c5cf00 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cce51bafc6cbb73585a51e7495b2e6cc", + "content-hash": "8fe5034e4c20ba22c778a666ef451dd9", "packages": [ { "name": "adhocore/jwt", @@ -4526,16 +4526,16 @@ }, { "name": "utopia-php/migration", - "version": "dev-feat/skip-duplicates", + "version": "1.9.2", "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "6025318d61c15355015c0e34427480e84ad690ad" + "reference": "97266905f35260137ba0b0e0c4f849f1ee422e43" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/6025318d61c15355015c0e34427480e84ad690ad", - "reference": "6025318d61c15355015c0e34427480e84ad690ad", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/97266905f35260137ba0b0e0c4f849f1ee422e43", + "reference": "97266905f35260137ba0b0e0c4f849f1ee422e43", "shasum": "" }, "require": { @@ -4544,7 +4544,7 @@ "ext-openssl": "*", "halaxa/json-machine": "^1.2", "php": ">=8.1", - "utopia-php/database": "5.3.22", + "utopia-php/database": "5.*", "utopia-php/dsn": "0.2.*", "utopia-php/storage": "1.0.*" }, @@ -4575,9 +4575,9 @@ ], "support": { "issues": "https://github.com/utopia-php/migration/issues", - "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" + "source": "https://github.com/utopia-php/migration/tree/1.9.2" }, - "time": "2026-04-20T09:07:20+00:00" + "time": "2026-04-20T11:55:43+00:00" }, { "name": "utopia-php/mongo", @@ -8440,18 +8440,9 @@ "time": "2024-11-07T12:36:22+00:00" } ], - "aliases": [ - { - "package": "utopia-php/migration", - "version": "dev-feat/skip-duplicates", - "alias": "1.9.99", - "alias_normalized": "1.9.99.0" - } - ], + "aliases": [], "minimum-stability": "dev", - "stability-flags": { - "utopia-php/migration": 20 - }, + "stability-flags": {}, "prefer-stable": true, "prefer-lowest": false, "platform": { diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 6a440e54c0..d88ab23535 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -30,6 +30,7 @@ use Utopia\Migration\Destination; use Utopia\Migration\Destinations\Appwrite as DestinationAppwrite; use Utopia\Migration\Destinations\CSV as DestinationCSV; use Utopia\Migration\Destinations\JSON as DestinationJSON; +use Utopia\Migration\Destinations\OnDuplicate; use Utopia\Migration\Exception as MigrationException; use Utopia\Migration\Resource; use Utopia\Migration\Resources\Database\Database as ResourceDatabase; @@ -276,7 +277,7 @@ class Migrations extends Action $this->dbForProject, $this->getDatabasesDB, Config::getParam('collections', [])['databases']['collections'], - $options['onDuplicate'] ?? DestinationAppwrite::ON_DUPLICATE_FAIL, + OnDuplicate::tryFrom($options['onDuplicate'] ?? '') ?? OnDuplicate::Fail, ), DestinationCSV::getName() => new DestinationCSV( $this->deviceForFiles, From f3c2502a8cec9dd2dfdcf8715303d8c321ea9bfa Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 20 Apr 2026 13:56:25 +0100 Subject: [PATCH 05/40] composer: widen database/migration pins to minor-version wildcards --- composer.json | 4 ++-- composer.lock | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 2f8ea37011..d7681742a5 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,7 @@ "utopia-php/compression": "0.1.*", "utopia-php/config": "1.*", "utopia-php/console": "0.1.*", - "utopia-php/database": "5.3.22", + "utopia-php/database": "5.3.*", "utopia-php/detector": "0.2.*", "utopia-php/domains": "1.*", "utopia-php/emails": "0.6.*", @@ -73,7 +73,7 @@ "utopia-php/locale": "0.8.*", "utopia-php/logger": "0.6.*", "utopia-php/messaging": "0.22.*", - "utopia-php/migration": "1.9.2", + "utopia-php/migration": "1.9.*", "utopia-php/platform": "0.12.*", "utopia-php/pools": "1.*", "utopia-php/span": "1.1.*", diff --git a/composer.lock b/composer.lock index 7e09c5cf00..423b5bef9c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8fe5034e4c20ba22c778a666ef451dd9", + "content-hash": "86a1fe0eb48da6028c13db50640cc4f6", "packages": [ { "name": "adhocore/jwt", From e878b0b403e73fe7feee9543fb95b16157e0da6f Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 20 Apr 2026 14:43:45 +0100 Subject: [PATCH 06/40] tests: cover onDuplicate on JSON import endpoint + column readiness wait --- .../Services/Migrations/MigrationsBase.php | 250 ++++++++++++++++++ 1 file changed, 250 insertions(+) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 35f196ec49..3adff48036 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1528,6 +1528,16 @@ trait MigrationsBase ]); $this->assertEquals(202, $response['headers']['status-code']); + // Columns are created async (202). Wait for both to be `available` + // before proceeding so the migration worker doesn't race the schema. + foreach (['name', 'age'] as $column) { + $this->assertEventually(function () use ($databaseId, $tableId, $column, $headers) { + $response = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/' . $column, $headers); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('available', $response['body']['status']); + }, 5000, 500); + } + // bucket $response = $this->client->call(Client::METHOD_POST, '/storage/buckets', $headers, [ 'bucketId' => ID::unique(), @@ -1735,6 +1745,246 @@ trait MigrationsBase ], $body); } + /** + * Set up a database + table + bucket + uploaded JSON for the skip/overwrite tests. + * Mirrors prepareCsvImportFixture but uploads documents.json instead. + * + * @return array{string,string,string,string,string,string,int} + */ + private function prepareJsonImportFixture(string $testLabel): array + { + $headers = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]; + + // database + $response = $this->client->call(Client::METHOD_POST, '/databases', $headers, [ + 'databaseId' => ID::unique(), + 'name' => 'Test JSON DB ' . $testLabel, + ]); + $this->assertEquals(201, $response['headers']['status-code']); + $databaseId = $response['body']['$id']; + + // table + $response = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', $headers, [ + 'name' => 'Test JSON table ' . $testLabel, + 'tableId' => ID::unique(), + ]); + $this->assertEquals(201, $response['headers']['status-code']); + $tableId = $response['body']['$id']; + + // columns: name, age (match documents.json fixture) + $response = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', $headers, [ + 'key' => 'name', + 'size' => 256, + 'required' => true, + ]); + $this->assertEquals(202, $response['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/integer', $headers, [ + 'key' => 'age', + 'min' => 18, + 'max' => 65, + 'required' => true, + ]); + $this->assertEquals(202, $response['headers']['status-code']); + + foreach (['name', 'age'] as $column) { + $this->assertEventually(function () use ($databaseId, $tableId, $column, $headers) { + $response = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/' . $column, $headers); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('available', $response['body']['status']); + }, 5000, 500); + } + + // bucket + $response = $this->client->call(Client::METHOD_POST, '/storage/buckets', $headers, [ + 'bucketId' => ID::unique(), + 'name' => 'JSON Bucket ' . $testLabel, + 'maximumFileSize' => 2000000, + 'allowedFileExtensions' => ['json'], + ]); + $this->assertEquals(201, $response['headers']['status-code']); + $bucketId = $response['body']['$id']; + + // upload documents.json (same row shape as documents.csv) + $response = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'fileId' => ID::unique(), + 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/json/documents.json'), 'application/json', 'documents.json'), + ]); + $this->assertEquals(201, $response['headers']['status-code']); + $fileId = $response['body']['$id']; + + // first row in documents.json: hxfcwpcas5xokpwe, Diamond Mendez, 56 + return [$databaseId, $tableId, $bucketId, $fileId, 'hxfcwpcas5xokpwe', 'Diamond Mendez', 56]; + } + + /** + * onDuplicate=skip on JSON re-import: duplicates silently no-op, existing rows preserved unchanged. + */ + public function testCreateJSONImportSkipDuplicates(): void + { + [$databaseId, $tableId, $bucketId, $fileId, $rowId, $originalName, $originalAge] = $this->prepareJsonImportFixture('skip'); + + $first = $this->performJsonMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + ]); + $this->assertEventually(function () use ($first) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('completed', $migration['body']['status']); + $this->assertEquals(100, $migration['body']['statusCounters'][Resource::TYPE_ROW]['success']); + }, 10_000, 500); + + // Mutate one row so we can prove skip does NOT overwrite it + $mutate = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'data' => ['age' => 22], + ]); + $this->assertEquals(200, $mutate['headers']['status-code']); + $this->assertEquals(22, $mutate['body']['age']); + + $second = $this->performJsonMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + 'onDuplicate' => 'skip', + ]); + $this->assertEventually(function () use ($second) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('completed', $migration['body']['status']); + }, 10_000, 500); + + $row = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals(200, $row['headers']['status-code']); + $this->assertEquals($originalName, $row['body']['name']); + $this->assertEquals(22, $row['body']['age'], 'onDuplicate=skip must not overwrite mutated row'); + + $rows = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [Query::limit(150)->toString()], + ]); + $this->assertEquals(100, $rows['body']['total']); + } + + /** + * onDuplicate=upsert on JSON re-import: existing rows replaced with imported values. + */ + public function testCreateJSONImportOverwrite(): void + { + [$databaseId, $tableId, $bucketId, $fileId, $rowId, $originalName, $originalAge] = $this->prepareJsonImportFixture('overwrite'); + + $first = $this->performJsonMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + ]); + $this->assertEventually(function () use ($first) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('completed', $migration['body']['status']); + $this->assertEquals(100, $migration['body']['statusCounters'][Resource::TYPE_ROW]['success']); + }, 10_000, 500); + + $mutate = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'data' => ['age' => 22], + ]); + $this->assertEquals(200, $mutate['headers']['status-code']); + $this->assertEquals(22, $mutate['body']['age']); + + $second = $this->performJsonMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + 'onDuplicate' => 'upsert', + ]); + $this->assertEventually(function () use ($second) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('completed', $migration['body']['status']); + }, 10_000, 500); + + $row = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals(200, $row['headers']['status-code']); + $this->assertEquals($originalName, $row['body']['name']); + $this->assertEquals($originalAge, $row['body']['age'], 'onDuplicate=upsert must restore row to imported value'); + + $rows = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [Query::limit(150)->toString()], + ]); + $this->assertEquals(100, $rows['body']['total']); + } + + /** + * Default (no onDuplicate) on JSON re-import: regression guard, must fail on duplicate ids. + */ + public function testCreateJSONImportDefaultFailsOnDuplicate(): void + { + [$databaseId, $tableId, $bucketId, $fileId] = $this->prepareJsonImportFixture('default'); + + $first = $this->performJsonMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + ]); + $this->assertEventually(function () use ($first) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $first['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('completed', $migration['body']['status']); + }, 10_000, 500); + + $second = $this->performJsonMigration([ + 'fileId' => $fileId, + 'bucketId' => $bucketId, + 'resourceId' => $databaseId . ':' . $tableId, + ]); + $this->assertEventually(function () use ($second) { + $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + $this->assertEquals('finished', $migration['body']['stage']); + $this->assertEquals('failed', $migration['body']['status']); + $this->assertNotEmpty($migration['body']['errors']); + }, 60_000, 500); + } + /** * Test CSV export with email notification */ From fc6bd7232e9105d59bdb584c156b397f9f675740 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 20 Apr 2026 14:50:57 +0100 Subject: [PATCH 07/40] tests: add Appwrite->Appwrite row migration onDuplicate test --- .../Services/Migrations/MigrationsBase.php | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 3adff48036..9b44bc4fd3 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -761,6 +761,96 @@ trait MigrationsBase self::$cachedTableData = []; } + /** + * Appwrite → Appwrite row migration honoring onDuplicate=skip and onDuplicate=upsert. + * Exercises the row-buffer dispatch path via cross-project migration rather than CSV/JSON upload. + */ + public function testAppwriteMigrationRowsOnDuplicate(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + // Source setup: database + table + column + row + $data = $this->setupMigrationTable(); + $databaseId = $data['databaseId']; + $tableId = $data['tableId']; + + $row = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', $sourceHeaders, [ + 'rowId' => ID::unique(), + 'data' => ['name' => 'Original'], + ]); + $this->assertEquals(201, $row['headers']['status-code']); + $rowId = $row['body']['$id']; + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + Resource::TYPE_ROW, + ]; + + // First migration: destination is empty, all resources copied + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + // Mutate destination row so we can prove skip preserves it + $mutate = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders, [ + 'data' => ['name' => 'Mutated'], + ]); + $this->assertEquals(200, $mutate['headers']['status-code']); + $this->assertEquals('Mutated', $mutate['body']['name']); + + // Second migration with onDuplicate=skip: destination row must keep 'Mutated' + $second = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'skip', + ]); + $this->assertEquals('completed', $second['status']); + + $rowAfterSkip = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); + $this->assertEquals(200, $rowAfterSkip['headers']['status-code']); + $this->assertEquals('Mutated', $rowAfterSkip['body']['name'], 'onDuplicate=skip must not overwrite destination row'); + + // Third migration with onDuplicate=upsert: destination row must be restored to 'Original' + $third = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'upsert', + ]); + $this->assertEquals('completed', $third['status']); + + $rowAfterUpsert = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); + $this->assertEquals(200, $rowAfterUpsert['headers']['status-code']); + $this->assertEquals('Original', $rowAfterUpsert['body']['name'], 'onDuplicate=upsert must restore source value'); + + // Cleanup on destination + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + + // Cleanup on source + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + /** * Storage */ From d0603c4d289d356b2eac8f6dea37bac140be28bb Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 20 Apr 2026 16:38:59 +0100 Subject: [PATCH 08/40] tests: add Appwrite->Appwrite row onDuplicate test with tolerant poller --- .../Services/Migrations/MigrationsBase.php | 60 ++++++++++++++----- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 9b44bc4fd3..6ba7a0604e 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -762,8 +762,12 @@ trait MigrationsBase } /** - * Appwrite → Appwrite row migration honoring onDuplicate=skip and onDuplicate=upsert. - * Exercises the row-buffer dispatch path via cross-project migration rather than CSV/JSON upload. + * Appwrite → Appwrite row re-migration honoring onDuplicate=skip and onDuplicate=upsert. + * + * onDuplicate only gates the row-write path in DestinationAppwrite. Re-running the + * migration with the full resource tree (database/table/column/row) always errors + * on schema creation because destination already has those. This test accepts those + * schema-level errors as expected noise and asserts row-level correctness directly. */ public function testAppwriteMigrationRowsOnDuplicate(): void { @@ -778,7 +782,6 @@ trait MigrationsBase 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], ]; - // Source setup: database + table + column + row $data = $this->setupMigrationTable(); $databaseId = $data['databaseId']; $tableId = $data['tableId']; @@ -797,7 +800,7 @@ trait MigrationsBase Resource::TYPE_ROW, ]; - // First migration: destination is empty, all resources copied + // First migration: destination is empty, strict completion expected. $first = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, @@ -806,51 +809,80 @@ trait MigrationsBase ]); $this->assertEquals('completed', $first['status']); - // Mutate destination row so we can prove skip preserves it + // Mutate destination row to prove onDuplicate=skip preserves it. $mutate = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders, [ 'data' => ['name' => 'Mutated'], ]); $this->assertEquals(200, $mutate['headers']['status-code']); $this->assertEquals('Mutated', $mutate['body']['name']); - // Second migration with onDuplicate=skip: destination row must keep 'Mutated' - $second = $this->performMigrationSync([ + // Re-migration with onDuplicate=skip. Overall status is expected to be 'failed' + // because schema re-create errors (database/table/column already exist) — those + // are orthogonal to onDuplicate which only affects row writes. Assert row-level + // success counter instead. + $this->runMigrationAssertingRowSuccess([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], 'onDuplicate' => 'skip', ]); - $this->assertEquals('completed', $second['status']); $rowAfterSkip = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); $this->assertEquals(200, $rowAfterSkip['headers']['status-code']); $this->assertEquals('Mutated', $rowAfterSkip['body']['name'], 'onDuplicate=skip must not overwrite destination row'); - // Third migration with onDuplicate=upsert: destination row must be restored to 'Original' - $third = $this->performMigrationSync([ + // Re-migration with onDuplicate=upsert. Same status-tolerant approach; assert + // destination row was restored to source value. + $this->runMigrationAssertingRowSuccess([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], 'onDuplicate' => 'upsert', ]); - $this->assertEquals('completed', $third['status']); $rowAfterUpsert = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); $this->assertEquals(200, $rowAfterUpsert['headers']['status-code']); $this->assertEquals('Original', $rowAfterUpsert['body']['name'], 'onDuplicate=upsert must restore source value'); - // Cleanup on destination $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); - - // Cleanup on source $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); self::$cachedDatabaseData = []; self::$cachedTableData = []; } + /** + * Helper for Appwrite→Appwrite re-migrations where onDuplicate applies only to rows. + * Accepts migration stages of 'finished' regardless of overall status, then asserts + * the row-level counter has zero errors (and at least one success). + * + * @param array $body + */ + private function runMigrationAssertingRowSuccess(array $body): void + { + $migration = $this->client->call(Client::METHOD_POST, '/migrations/appwrite', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ], $body); + $this->assertEquals(202, $migration['headers']['status-code']); + + $this->assertEventually(function () use ($migration) { + $response = $this->client->call(Client::METHOD_GET, '/migrations/' . $migration['body']['$id'], [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('finished', $response['body']['stage']); + $this->assertArrayHasKey(Resource::TYPE_ROW, $response['body']['statusCounters']); + $this->assertEquals(0, $response['body']['statusCounters'][Resource::TYPE_ROW]['error']); + $this->assertGreaterThanOrEqual(1, $response['body']['statusCounters'][Resource::TYPE_ROW]['success']); + }, 60_000, 500); + } + /** * Storage */ From 19f02a5129507e279d801f2c960ec91f5a554963 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 20 Apr 2026 16:44:36 +0100 Subject: [PATCH 09/40] composer: sync lock content-hash after merge from 1.9.x --- composer.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.lock b/composer.lock index 19d7af6719..b56e9b1444 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "86a1fe0eb48da6028c13db50640cc4f6", + "content-hash": "756f545d9723dbad7f23fd10fddc64bc", "packages": [ { "name": "adhocore/jwt", From 715bd40b4a0c649ab83a0560dce90dcd5d772772 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 22 Apr 2026 16:38:32 +0100 Subject: [PATCH 10/40] =?UTF-8?q?Tighten=20A=E2=86=92A=20re-migration=20te?= =?UTF-8?q?sts=20against=20utopia-php/migration=20schema=20tolerance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit utopia-php/migration's DestinationAppwrite now handles schema tolerance on re-migration (PR #171 on feat/skip-duplicates): it pre-checks destination `_metadata` for each database / table / column / index and tolerates in Skip/Upsert mode. Re-runs no longer produce schema-level errors, so the E2E tests can drop the status-tolerant workaround and assert strict 'completed' outcomes. Changes: - composer.json: pin utopia-php/migration to dev-feat/skip-duplicates (aliased to 1.9.99 for stability resolution). Will be replaced with a fixed 1.10.0 tag once the migration PR lands. - testAppwriteMigrationRowsOnDuplicate: replace the tolerant runMigrationAssertingRowSuccess helper with performMigrationSync on the Skip and Upsert re-runs. Asserts 'completed' status on every run, destination row content matches the expected value per mode (Mutated preserved on Skip, Original restored on Upsert). Helper method removed. - testAppwriteMigrationReRunIsIdempotent (new): seeds two rows on source, runs the migration three times back-to-back (fresh, Skip re-run, Upsert re-run) against unchanged source data, asserts strict 'completed' on every run and row content is stable across all three. Exercises the schema-tolerance path end-to-end: every database/table/column on destination already exists with a matching spec, so DestinationAppwrite's pre-check returns Tolerate for every resource. --- composer.json | 2 +- composer.lock | 27 ++-- .../Services/Migrations/MigrationsBase.php | 120 +++++++++++++----- 3 files changed, 107 insertions(+), 42 deletions(-) diff --git a/composer.json b/composer.json index bcbd59a636..8daede4537 100644 --- a/composer.json +++ b/composer.json @@ -73,7 +73,7 @@ "utopia-php/locale": "0.8.*", "utopia-php/logger": "0.6.*", "utopia-php/messaging": "0.22.*", - "utopia-php/migration": "1.9.*", + "utopia-php/migration": "dev-feat/skip-duplicates as 1.9.99", "utopia-php/platform": "0.13.*", "utopia-php/pools": "1.*", "utopia-php/span": "1.1.*", diff --git a/composer.lock b/composer.lock index b56e9b1444..9148102adf 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "756f545d9723dbad7f23fd10fddc64bc", + "content-hash": "b6c60200cc06ec4d8ef8e342f09c7c30", "packages": [ { "name": "adhocore/jwt", @@ -4528,16 +4528,16 @@ }, { "name": "utopia-php/migration", - "version": "1.9.2", + "version": "dev-feat/skip-duplicates", "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "97266905f35260137ba0b0e0c4f849f1ee422e43" + "reference": "001682168f7d87932c56635a0d0a45b927febc33" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/97266905f35260137ba0b0e0c4f849f1ee422e43", - "reference": "97266905f35260137ba0b0e0c4f849f1ee422e43", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/001682168f7d87932c56635a0d0a45b927febc33", + "reference": "001682168f7d87932c56635a0d0a45b927febc33", "shasum": "" }, "require": { @@ -4577,9 +4577,9 @@ ], "support": { "issues": "https://github.com/utopia-php/migration/issues", - "source": "https://github.com/utopia-php/migration/tree/1.9.2" + "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-20T11:55:43+00:00" + "time": "2026-04-22T13:15:22+00:00" }, { "name": "utopia-php/mongo", @@ -8441,9 +8441,18 @@ "time": "2024-11-07T12:36:22+00:00" } ], - "aliases": [], + "aliases": [ + { + "package": "utopia-php/migration", + "version": "dev-feat/skip-duplicates", + "alias": "1.9.99", + "alias_normalized": "1.9.99.0" + } + ], "minimum-stability": "dev", - "stability-flags": {}, + "stability-flags": { + "utopia-php/migration": 20 + }, "prefer-stable": true, "prefer-lowest": false, "platform": { diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 6ba7a0604e..a2199dd63d 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -764,10 +764,11 @@ trait MigrationsBase /** * Appwrite → Appwrite row re-migration honoring onDuplicate=skip and onDuplicate=upsert. * - * onDuplicate only gates the row-write path in DestinationAppwrite. Re-running the - * migration with the full resource tree (database/table/column/row) always errors - * on schema creation because destination already has those. This test accepts those - * schema-level errors as expected noise and asserts row-level correctness directly. + * With utopia-php/migration's DestinationAppwrite handling schema tolerance + * (pre-check the destination `_metadata` for each database / table / column + * / index, tolerate existing in Skip/Upsert), re-migration completes + * cleanly — no more schema-level errors to tolerate. The test asserts + * strict 'completed' status via performMigrationSync on every run. */ public function testAppwriteMigrationRowsOnDuplicate(): void { @@ -816,31 +817,31 @@ trait MigrationsBase $this->assertEquals(200, $mutate['headers']['status-code']); $this->assertEquals('Mutated', $mutate['body']['name']); - // Re-migration with onDuplicate=skip. Overall status is expected to be 'failed' - // because schema re-create errors (database/table/column already exist) — those - // are orthogonal to onDuplicate which only affects row writes. Assert row-level - // success counter instead. - $this->runMigrationAssertingRowSuccess([ + // Re-migration with onDuplicate=skip — completion is strict because + // DestinationAppwrite tolerates existing schema resources. + $skipResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], 'onDuplicate' => 'skip', ]); + $this->assertEquals('completed', $skipResult['status']); $rowAfterSkip = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); $this->assertEquals(200, $rowAfterSkip['headers']['status-code']); $this->assertEquals('Mutated', $rowAfterSkip['body']['name'], 'onDuplicate=skip must not overwrite destination row'); - // Re-migration with onDuplicate=upsert. Same status-tolerant approach; assert - // destination row was restored to source value. - $this->runMigrationAssertingRowSuccess([ + // Re-migration with onDuplicate=upsert — strict completion; destination + // row restored to source value. + $upsertResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], 'onDuplicate' => 'upsert', ]); + $this->assertEquals('completed', $upsertResult['status']); $rowAfterUpsert = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); $this->assertEquals(200, $rowAfterUpsert['headers']['status-code']); @@ -854,33 +855,88 @@ trait MigrationsBase } /** - * Helper for Appwrite→Appwrite re-migrations where onDuplicate applies only to rows. - * Accepts migration stages of 'finished' regardless of overall status, then asserts - * the row-level counter has zero errors (and at least one success). - * - * @param array $body + * Re-migrating unchanged source (Skip / Upsert) completes cleanly without + * touching destination rows. Proves the schema-tolerance path: every + * database / table / attribute on destination already exists with a + * matching spec, so DestinationAppwrite's pre-check returns Tolerate for + * every resource and no-ops row writes go through the DB-native conflict + * primitives (INSERT IGNORE / ON DUPLICATE KEY UPDATE). */ - private function runMigrationAssertingRowSuccess(array $body): void + public function testAppwriteMigrationReRunIsIdempotent(): void { - $migration = $this->client->call(Client::METHOD_POST, '/migrations/appwrite', [ + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getDestinationProject()['$id'], 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], - ], $body); - $this->assertEquals(202, $migration['headers']['status-code']); + ]; - $this->assertEventually(function () use ($migration) { - $response = $this->client->call(Client::METHOD_GET, '/migrations/' . $migration['body']['$id'], [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getDestinationProject()['$id'], - 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + $data = $this->setupMigrationTable(); + $databaseId = $data['databaseId']; + $tableId = $data['tableId']; + + // Seed two rows on source so the row-level tolerance is exercised too. + foreach (['row-a', 'row-b'] as $rowId) { + $row = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', $sourceHeaders, [ + 'rowId' => $rowId, + 'data' => ['name' => 'Seeded ' . $rowId], ]); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals('finished', $response['body']['stage']); - $this->assertArrayHasKey(Resource::TYPE_ROW, $response['body']['statusCounters']); - $this->assertEquals(0, $response['body']['statusCounters'][Resource::TYPE_ROW]['error']); - $this->assertGreaterThanOrEqual(1, $response['body']['statusCounters'][Resource::TYPE_ROW]['success']); - }, 60_000, 500); + $this->assertEquals(201, $row['headers']['status-code']); + } + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + Resource::TYPE_ROW, + ]; + + // First migration: fresh destination. + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + // Re-run under Skip: nothing on source has changed. Destination + // schema + rows are already correct — expect clean completion. + $reRunSkip = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'skip', + ]); + $this->assertEquals('completed', $reRunSkip['status']); + + // Re-run under Upsert: same unchanged source. Schema tolerance path + // fires for each resource; rows go through DB-native upsert. + $reRunUpsert = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'upsert', + ]); + $this->assertEquals('completed', $reRunUpsert['status']); + + foreach (['row-a', 'row-b'] as $rowId) { + $check = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); + $this->assertEquals(200, $check['headers']['status-code']); + $this->assertEquals('Seeded ' . $rowId, $check['body']['name']); + } + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; } /** From 2ad95e511382c6ba23762adfc4861c6cb93c1b3f Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 22 Apr 2026 16:46:33 +0100 Subject: [PATCH 11/40] ci: restrict to Migrations service for fast feedback loop (TEMP) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Branch is iterating on utopia-php/migration's re-migration tolerance. Other test matrices (unit, general, abuse, screenshots, benchmark, and every other e2e service) add ~30+ minutes to CI without exercising code this PR touches. Restrict the matrix to the Migrations service and skip the unrelated test jobs until the migration work is ready to merge. All jobs marked with 'TEMP:' comments + 'if: false' — revert to the full matrix before merging to main. Static analysis (lint, phpstan, composer audit, specs, locale, security) still runs on every PR push. --- .github/workflows/ci.yml | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b02d021f1a..ce44c19931 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -285,6 +285,8 @@ jobs: unit: name: Tests / Unit + # TEMP: Migrations-only CI run. Re-enable before merging to main. + if: false runs-on: ubuntu-latest needs: build permissions: @@ -332,6 +334,8 @@ jobs: e2e_general: name: Tests / E2E / General + # TEMP: Migrations-only CI run. Re-enable before merging to main. + if: false runs-on: ubuntu-latest needs: build permissions: @@ -400,32 +404,10 @@ jobs: matrix: database: ${{ fromJSON(needs.matrix.outputs.databases) }} mode: ${{ fromJSON(needs.matrix.outputs.modes) }} - service: [ - Account, - Avatars, - Console, - Databases, - TablesDB, - Functions, - FunctionsSchedule, - GraphQL, - Health, - Locale, - Projects, - Realtime, - Sites, - Proxy, - Storage, - Tokens, - Teams, - Users, - ProjectWebhooks, - Webhooks, - VCS, - Messaging, - Migrations, - Project - ] + # TEMP: branch is testing utopia-php/migration re-migration tolerance. + # Restricted to Migrations to get fast CI feedback on the feature work; + # revert to the full matrix before merging to main. + service: [Migrations] include: - service: Databases runner: blacksmith-4vcpu-ubuntu-2404 @@ -524,6 +506,8 @@ jobs: e2e_abuse: name: Tests / E2E / Abuse (${{ matrix.mode }}) + # TEMP: Migrations-only CI run. Re-enable before merging to main. + if: false runs-on: ubuntu-latest needs: [build, matrix] permissions: @@ -583,6 +567,8 @@ jobs: e2e_screenshots: name: Tests / E2E / Screenshots (${{ matrix.mode }}) + # TEMP: Migrations-only CI run. Re-enable before merging to main. + if: false runs-on: ubuntu-latest needs: [build, matrix] permissions: @@ -649,6 +635,8 @@ jobs: benchmark: name: Benchmark + # TEMP: Migrations-only CI run. Re-enable before merging to main. + if: false runs-on: ubuntu-latest needs: build permissions: From c1506643e9196d4395e3ab8c95e83128e3c39376 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 23 Apr 2026 00:12:09 +0100 Subject: [PATCH 12/40] composer: bump utopia-php/migration to cfba224 Picks up the PR #171 refactor + unit tests: - resolveSchemaAction decision point consolidation - deleteAttributeCompletely primitive (two-way cleanup in one place) - Hardened sourceIsNewer against MySQL zero-date sentinel - 14 unit tests locking the decision matrix --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 9148102adf..e590802949 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "001682168f7d87932c56635a0d0a45b927febc33" + "reference": "cfba224f73f00d236a748553d87381a4cf77d0f6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/001682168f7d87932c56635a0d0a45b927febc33", - "reference": "001682168f7d87932c56635a0d0a45b927febc33", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/cfba224f73f00d236a748553d87381a4cf77d0f6", + "reference": "cfba224f73f00d236a748553d87381a4cf77d0f6", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-22T13:15:22+00:00" + "time": "2026-04-22T23:09:03+00:00" }, { "name": "utopia-php/mongo", From 44b7b26adfdb86f2c23c71397b02b277e78daa12 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 23 Apr 2026 06:06:30 +0100 Subject: [PATCH 13/40] composer: bump utopia-php/migration to e08d531 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Picks up the UpdateInPlace branch — database/table metadata drift is now reconciled on Upsert-newer (renames, enable toggles, table permissions / documentSecurity) via updateDocument, without touching child rows. --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index e590802949..079cb8e145 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "cfba224f73f00d236a748553d87381a4cf77d0f6" + "reference": "e08d531d7cab9c6aeed4fa28cfe326449b0dd3b0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/cfba224f73f00d236a748553d87381a4cf77d0f6", - "reference": "cfba224f73f00d236a748553d87381a4cf77d0f6", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/e08d531d7cab9c6aeed4fa28cfe326449b0dd3b0", + "reference": "e08d531d7cab9c6aeed4fa28cfe326449b0dd3b0", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-22T23:09:03+00:00" + "time": "2026-04-23T05:05:36+00:00" }, { "name": "utopia-php/mongo", From 0134832fb6f4d0cfe3dec7d6c701e57e2a81f195 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 23 Apr 2026 06:22:26 +0100 Subject: [PATCH 14/40] tests: cover Upsert UpdateInPlace for database/table + Skip preserves dest drift MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two new E2E tests exercising the schema-tolerance UpdateInPlace path added in utopia-php/migration's DestinationAppwrite. testAppwriteMigrationUpsertUpdatesContainerMetadata (positive): - Fresh migration copies source database + table + column + row to dest. - Mutates source database name (PUT /databases/:id) and table name/permissions/rowSecurity/enabled (PUT /tablesdb/:db/tables/:id). - One-second sleep before mutation ensures source's $updatedAt is strictly greater than dest's at second granularity (strtotime comparison). - Upsert re-migration asserts: - 'completed' status. - dest database name matches source's new name. - dest table name / enabled / rowSecurity match source's new values. - child row's 'name' attribute is untouched — UpdateInPlace only rewrites container metadata, not rows. testAppwriteMigrationSkipPreservesContainerDrift (negative): - Fresh migration, then mutate BOTH dest (simulating ops tightening permissions post-migration) and source (divergence). - Skip re-migration asserts dest kept its tightened values — Skip's strict "don't touch" contract protects dev→prod cutover workflows from accidentally wiping ops-side drift on schema re-sync. Both tests use performMigrationSync for strict 'completed' assertions. Runtime ~18s combined. Existing testAppwriteMigrationRowsOnDuplicate and testAppwriteMigrationReRunIsIdempotent regression-tested locally. --- .../Services/Migrations/MigrationsBase.php | 178 ++++++++++++++++++ 1 file changed, 178 insertions(+) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index a2199dd63d..3a997a8354 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -939,6 +939,184 @@ trait MigrationsBase self::$cachedTableData = []; } + /** + * Upsert re-migration reconciles container metadata drift: when source's + * database or table was modified between runs (rename, enabled flag, + * permissions tightening), the Upsert pre-check returns UpdateInPlace + * and migration's updateDocument propagates source values to dest. + * Children (rows) are preserved. + */ + public function testAppwriteMigrationUpsertUpdatesContainerMetadata(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $data = $this->setupMigrationTable(); + $databaseId = $data['databaseId']; + $tableId = $data['tableId']; + $rowId = 'persist-me'; + + $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', $sourceHeaders, [ + 'rowId' => $rowId, + 'data' => ['name' => 'SeedRow'], + ]); + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + Resource::TYPE_ROW, + ]; + + // First migration — dest empty, strict completion. + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + // `_updatedAt` is stored at second granularity (strtotime) — ensure + // the source edits below produce a strictly-newer timestamp than + // dest's first-migration timestamp. + sleep(1); + + // Mutate source: rename database + toggle table enabled. + $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId, $sourceHeaders, [ + 'name' => 'Renamed Source DB', + ]); + $this->client->call(Client::METHOD_PUT, '/tablesdb/' . $databaseId . '/tables/' . $tableId, $sourceHeaders, [ + 'name' => 'Renamed Source Table', + 'permissions' => [Permission::read(Role::any())], + 'rowSecurity' => true, + 'enabled' => false, + ]); + + // Upsert re-migration: UpdateInPlace path fires for database + table. + $upsertResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'upsert', + ]); + $this->assertEquals('completed', $upsertResult['status']); + + // Assert dest database metadata reflects source's new values. + $destDb = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId, $destHeaders); + $this->assertEquals(200, $destDb['headers']['status-code']); + $this->assertEquals('Renamed Source DB', $destDb['body']['name']); + + // Assert dest table metadata reflects source's new values. + $destTable = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId, $destHeaders); + $this->assertEquals(200, $destTable['headers']['status-code']); + $this->assertEquals('Renamed Source Table', $destTable['body']['name']); + $this->assertFalse($destTable['body']['enabled'], 'Upsert must propagate source enabled=false'); + $this->assertTrue($destTable['body']['documentSecurity'] ?? $destTable['body']['rowSecurity'], 'Upsert must propagate source rowSecurity=true'); + + // Child row untouched — UpdateInPlace only rewrites container metadata. + $row = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); + $this->assertEquals(200, $row['headers']['status-code']); + $this->assertEquals('SeedRow', $row['body']['name'], 'Upsert must not touch child rows when updating container metadata'); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + + /** + * Skip mode is strict "don't touch" — destination container metadata + * drift (permissions tightened post-migration, rename on dest) is + * preserved on every re-run, even when source has diverged. Guards + * against a common production workflow: dev→prod migrate, ops tightens + * prod permissions, later schema-only re-sync must not wipe out the + * tightening. + */ + public function testAppwriteMigrationSkipPreservesContainerDrift(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $data = $this->setupMigrationTable(); + $databaseId = $data['databaseId']; + $tableId = $data['tableId']; + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + ]; + + // First migration: dest gets whatever source had. + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + sleep(1); + + // Mutate dest: ops tightens permissions and renames the table for + // its production-specific branding. + $this->client->call(Client::METHOD_PUT, '/tablesdb/' . $databaseId . '/tables/' . $tableId, $destHeaders, [ + 'name' => 'Dest-Managed Table', + 'permissions' => [Permission::read(Role::users())], + 'rowSecurity' => false, + 'enabled' => true, + ]); + + // Also mutate source so the second run has a real divergence. + $this->client->call(Client::METHOD_PUT, '/tablesdb/' . $databaseId . '/tables/' . $tableId, $sourceHeaders, [ + 'name' => 'Source Renamed', + 'permissions' => [Permission::read(Role::any())], + 'rowSecurity' => true, + 'enabled' => false, + ]); + + // Skip re-migration: must tolerate existing destination — no update. + $skipResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'skip', + ]); + $this->assertEquals('completed', $skipResult['status']); + + // Dest kept its tightened values. + $destTable = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId, $destHeaders); + $this->assertEquals(200, $destTable['headers']['status-code']); + $this->assertEquals('Dest-Managed Table', $destTable['body']['name'], 'Skip must not propagate source name over dest drift'); + $this->assertTrue($destTable['body']['enabled'], 'Skip must preserve dest enabled flag'); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + /** * Storage */ From 18694e0a28192bfc076779a292499de407ae6885 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 23 Apr 2026 07:10:23 +0100 Subject: [PATCH 15/40] TEMP: run Migrations across all adapters/modes on this branch --- .github/workflows/ci.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ce44c19931..cbc667c767 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -212,8 +212,11 @@ jobs: const allDatabases = ['MariaDB', 'PostgreSQL', 'MongoDB']; const allModes = ['dedicated', 'shared']; - const defaultDatabases = ['MongoDB']; - const defaultModes = ['dedicated']; + // TEMP: branch is testing utopia-php/migration re-migration tolerance + // across every adapter/mode. Revert to ['MongoDB']/['dedicated'] + // before merging to main. + const defaultDatabases = allDatabases; + const defaultModes = allModes; const pr = context.payload.pull_request; if (!pr) { From 84b6dfa9d5050b072e33ffbe2700f28d8ff4000f Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 23 Apr 2026 16:39:30 +0100 Subject: [PATCH 16/40] composer: bump utopia-php/migration to bb21912 (SKIPPED status on tolerate) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 079cb8e145..4a005bb20f 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "e08d531d7cab9c6aeed4fa28cfe326449b0dd3b0" + "reference": "bb21912e8cb501e5144b6bc6e1d0c9a6918a88d1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/e08d531d7cab9c6aeed4fa28cfe326449b0dd3b0", - "reference": "e08d531d7cab9c6aeed4fa28cfe326449b0dd3b0", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/bb21912e8cb501e5144b6bc6e1d0c9a6918a88d1", + "reference": "bb21912e8cb501e5144b6bc6e1d0c9a6918a88d1", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-23T05:05:36+00:00" + "time": "2026-04-23T15:35:27+00:00" }, { "name": "utopia-php/mongo", From e728265d21525110fa4104a3e6ba9d40472ac077 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Fri, 24 Apr 2026 05:57:32 +0100 Subject: [PATCH 17/40] composer: bump utopia-php/migration to 36fdf26 (Upsert orphan cleanup + nullable timestamps) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 4a005bb20f..8beec7f2fe 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "bb21912e8cb501e5144b6bc6e1d0c9a6918a88d1" + "reference": "36fdf269fe1c6fac2a1d80f5b913493c18989727" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/bb21912e8cb501e5144b6bc6e1d0c9a6918a88d1", - "reference": "bb21912e8cb501e5144b6bc6e1d0c9a6918a88d1", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/36fdf269fe1c6fac2a1d80f5b913493c18989727", + "reference": "36fdf269fe1c6fac2a1d80f5b913493c18989727", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-23T15:35:27+00:00" + "time": "2026-04-24T04:55:29+00:00" }, { "name": "utopia-php/mongo", From 8292f7493d85f4e3b45693e4eaea26eca33b5f6a Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Fri, 24 Apr 2026 07:25:37 +0100 Subject: [PATCH 18/40] composer: bump utopia-php/migration to c2b8715 (rename orphan tracker to 'table' vocabulary) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 8beec7f2fe..31c3610ad0 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "36fdf269fe1c6fac2a1d80f5b913493c18989727" + "reference": "c2b871519bbdaf7b3a2d947baa4e88a4b407c85d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/36fdf269fe1c6fac2a1d80f5b913493c18989727", - "reference": "36fdf269fe1c6fac2a1d80f5b913493c18989727", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/c2b871519bbdaf7b3a2d947baa4e88a4b407c85d", + "reference": "c2b871519bbdaf7b3a2d947baa4e88a4b407c85d", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-24T04:55:29+00:00" + "time": "2026-04-24T06:24:44+00:00" }, { "name": "utopia-php/mongo", From 478a2a6e86eb12cae084dbac30b1397d8afadb79 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Fri, 24 Apr 2026 07:30:29 +0100 Subject: [PATCH 19/40] tests: Upsert drops orphan columns / Skip preserves them testAppwriteMigrationUpsertDropsOrphanColumn: adds a column directly on destination (simulating post-rename orphan or dest-only drift), runs Upsert, asserts the orphan is dropped and source-declared column survives. Covers the per-table orphan cleanup fired inside createRecord before rows land. testAppwriteMigrationSkipKeepsOrphanColumn: same setup, Skip mode. Asserts the orphan survives, proving the cleanup is correctly gated to Upsert only. --- .../Services/Migrations/MigrationsBase.php | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 7a8f918b96..229c01057e 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1117,6 +1117,180 @@ trait MigrationsBase self::$cachedTableData = []; } + /** + * Upsert re-migration reconciles column-level drift: a column that + * exists only on destination (e.g. from a subsequent dest-side edit, or + * left over after a source-side rename) must be dropped so destination's + * schema matches what source declares. Source-declared columns are + * preserved. Rows land after the orphan drop so row upsert doesn't + * fail on orphan required columns. + */ + public function testAppwriteMigrationUpsertDropsOrphanColumn(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $data = $this->setupMigrationTable(); + $databaseId = $data['databaseId']; + $tableId = $data['tableId']; + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + Resource::TYPE_ROW, + ]; + + // First migration: dest mirrors source (one column 'name'). + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + // Add an orphan column directly on destination (not on source). + // Simulates the post-rename state: source dropped a column, dest + // still has it — or a dest-only column added by a separate app. + $orphanResp = $this->client->call( + Client::METHOD_POST, + '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', + $destHeaders, + [ + 'key' => 'orphan_col', + 'size' => 50, + 'required' => false, + ] + ); + $this->assertEquals(202, $orphanResp['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $tableId, $destHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/orphan_col', $destHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + }, 5000, 500); + + // Seed a row on source so per-table orphan cleanup fires inside + // createRecord (before rows land), not just at end of run. + $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', $sourceHeaders, [ + 'rowId' => ID::unique(), + 'data' => ['name' => 'seed'], + ]); + + // Upsert re-migration: orphan_col must be dropped from dest. + $upsertResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'upsert', + ]); + $this->assertEquals('completed', $upsertResult['status']); + + // Orphan column dropped. + $orphanCheck = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/orphan_col', $destHeaders); + $this->assertEquals(404, $orphanCheck['headers']['status-code'], 'Upsert must drop destination column source no longer declares'); + + // Source's column preserved. + $nameCheck = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); + $this->assertEquals(200, $nameCheck['headers']['status-code'], 'Upsert must preserve columns source declared'); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + + /** + * Skip mode never touches destination, including orphan columns. + * Pairs with testAppwriteMigrationUpsertDropsOrphanColumn to prove the + * cleanup is gated correctly: only Upsert reconciles, Skip tolerates. + */ + public function testAppwriteMigrationSkipKeepsOrphanColumn(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $data = $this->setupMigrationTable(); + $databaseId = $data['databaseId']; + $tableId = $data['tableId']; + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + Resource::TYPE_ROW, + ]; + + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + $orphanResp = $this->client->call( + Client::METHOD_POST, + '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', + $destHeaders, + [ + 'key' => 'dest_only_col', + 'size' => 50, + 'required' => false, + ] + ); + $this->assertEquals(202, $orphanResp['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $tableId, $destHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/dest_only_col', $destHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + }, 5000, 500); + + $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', $sourceHeaders, [ + 'rowId' => ID::unique(), + 'data' => ['name' => 'seed'], + ]); + + // Skip re-migration: orphan column must NOT be dropped. + $skipResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'skip', + ]); + $this->assertEquals('completed', $skipResult['status']); + + $orphanCheck = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/dest_only_col', $destHeaders); + $this->assertEquals(200, $orphanCheck['headers']['status-code'], 'Skip must preserve destination columns, including orphans'); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + /** * Storage */ From dc9f48c9485f0dedc0478307d23a69964b839874 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Fri, 24 Apr 2026 15:09:42 +0100 Subject: [PATCH 20/40] composer: bump utopia-php/migration to 6e6f825 (relationships UpdateInPlace) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 31c3610ad0..6ebcae42c5 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "c2b871519bbdaf7b3a2d947baa4e88a4b407c85d" + "reference": "6e6f8255fa52c8053fa2b9ad877c7797d93e32c5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/c2b871519bbdaf7b3a2d947baa4e88a4b407c85d", - "reference": "c2b871519bbdaf7b3a2d947baa4e88a4b407c85d", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/6e6f8255fa52c8053fa2b9ad877c7797d93e32c5", + "reference": "6e6f8255fa52c8053fa2b9ad877c7797d93e32c5", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-24T06:24:44+00:00" + "time": "2026-04-24T10:33:58+00:00" }, { "name": "utopia-php/mongo", From 5f8a32798dd6cf0c0e756c0861eb48b1ac100a16 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 27 Apr 2026 12:32:37 +0100 Subject: [PATCH 21/40] tests: SDK-aligned UpdateInPlace coverage + bump migration to a36d95f MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three new e2e tests in MigrationsBase covering the schema reconciliation paths added in utopia-php/migration: - testAppwriteMigrationUpsertUpdatesAttributeInPlace: PATCH source required/default (SDK-reachable), assert dest reflects change and the pre-existing row's column data is preserved (drop+recreate would have wiped it). - testAppwriteMigrationSkipPreservesAttributeDrift: leaf-level analog of the existing container-drift Skip test — guards Skip from ever consulting timestamps. - testAppwriteMigrationUpsertUpdatesRelationshipOnDeleteInPlace: PATCH source onDelete cascade->restrict (SDK-reachable), assert dest reflects change and structural fields (relationType, twoWay) untouched. composer.lock: utopia-php/migration 6e6f825 -> a36d95f (mechanical helpers replacement, parseTimestamp dedup, match dispatch, comment trim). --- composer.lock | 8 +- .../Services/Migrations/MigrationsBase.php | 316 ++++++++++++++++++ 2 files changed, 320 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 6ebcae42c5..084d4a5bfe 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "6e6f8255fa52c8053fa2b9ad877c7797d93e32c5" + "reference": "a36d95f86d24b4f024a419191e69c3c034a593eb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/6e6f8255fa52c8053fa2b9ad877c7797d93e32c5", - "reference": "6e6f8255fa52c8053fa2b9ad877c7797d93e32c5", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/a36d95f86d24b4f024a419191e69c3c034a593eb", + "reference": "a36d95f86d24b4f024a419191e69c3c034a593eb", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-24T10:33:58+00:00" + "time": "2026-04-27T11:29:25+00:00" }, { "name": "utopia-php/mongo", diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 229c01057e..264f6a0ee1 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1291,6 +1291,322 @@ trait MigrationsBase self::$cachedTableData = []; } + /** + * Upsert reconciles attribute-level metadata edits using the Appwrite SDK's + * per-type updateXAttribute endpoint instead of drop+recreate. Source-side + * PATCH of fields the SDK can express (`required`, `default`, `size` for + * strings) — same `$createdAt` on both sides, source `$updatedAt` newer — + * routes through `updateAttributeInPlace` on DestinationAppwrite. Existing + * row data must survive (drop+recreate would have wiped the column). + */ + public function testAppwriteMigrationUpsertUpdatesAttributeInPlace(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $data = $this->setupMigrationTable(); + $databaseId = $data['databaseId']; + $tableId = $data['tableId']; + $rowId = 'persist-on-inplace'; + + // Seed a row that proves drop+recreate didn't happen — recreate would + // have wiped this column's data on the destination. + $row = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', $sourceHeaders, [ + 'rowId' => $rowId, + 'data' => ['name' => 'SeedRow'], + ]); + $this->assertEquals(201, $row['headers']['status-code']); + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + Resource::TYPE_ROW, + ]; + + // First migration — dest gets the column as required:true. + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + $beforeUpdate = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); + $this->assertEquals(200, $beforeUpdate['headers']['status-code']); + $this->assertTrue($beforeUpdate['body']['required']); + + // _updatedAt has second granularity; ensure source's PATCH produces a + // strictly-newer timestamp than the dest's first-migration value. + sleep(1); + + // SDK-reachable change set: required true→false, default null→'unknown'. + // Both fields are supported by PATCH /columns/string/:key — must route + // through updateAttributeInPlace, not DropAndRecreate. + $patch = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string/name', $sourceHeaders, [ + 'required' => false, + 'default' => 'unknown', + ]); + $this->assertEquals(200, $patch['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $tableId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $sourceHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + $this->assertFalse($r['body']['required']); + $this->assertEquals('unknown', $r['body']['default']); + }, 5000, 500); + + $upsertResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'upsert', + ]); + $this->assertEquals('completed', $upsertResult['status']); + + $this->assertEventually(function () use ($databaseId, $tableId, $destHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + $this->assertFalse($r['body']['required'], 'updateAttributeInPlace must propagate source required=false'); + $this->assertEquals('unknown', $r['body']['default'], 'updateAttributeInPlace must propagate source default'); + }, 10000, 500); + + // Pre-existing row preserved — proof that the path was UpdateInPlace + // and not DropAndRecreate (which would have nulled this column). + $rowAfter = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); + $this->assertEquals(200, $rowAfter['headers']['status-code']); + $this->assertEquals('SeedRow', $rowAfter['body']['name'], 'updateAttributeInPlace must not touch row data'); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + + /** + * Skip mode is "don't touch" at the attribute level too: destination + * column drift (ops loosened a column post-migration) must survive a + * Skip re-run, even when source's `$updatedAt` is strictly newer. + * Pairs with testAppwriteMigrationSkipPreservesContainerDrift but + * exercises the leaf path (`canDrop = true`) instead of the container + * path. Regression guard against Skip ever consulting timestamps. + */ + public function testAppwriteMigrationSkipPreservesAttributeDrift(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $data = $this->setupMigrationTable(); + $databaseId = $data['databaseId']; + $tableId = $data['tableId']; + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + ]; + + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + sleep(1); + + // Dest divergence: ops loosens the column for a production-only need. + $destPatch = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string/name', $destHeaders, [ + 'required' => false, + 'default' => 'dest-default', + ]); + $this->assertEquals(200, $destPatch['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $tableId, $destHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + $this->assertFalse($r['body']['required']); + }, 5000, 500); + + sleep(1); + + // Source advances strictly later (and to a different value). Under + // Upsert this would propagate to dest; under Skip it must not. + $sourcePatch = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string/name', $sourceHeaders, [ + 'required' => true, + 'default' => null, + ]); + $this->assertEquals(200, $sourcePatch['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $tableId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $sourceHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + $this->assertTrue($r['body']['required']); + }, 5000, 500); + + $skipResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'skip', + ]); + $this->assertEquals('completed', $skipResult['status']); + + $destAttr = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); + $this->assertEquals(200, $destAttr['headers']['status-code']); + $this->assertFalse($destAttr['body']['required'], 'Skip must not propagate source required over dest drift'); + $this->assertEquals('dest-default', $destAttr['body']['default'], 'Skip must preserve dest default'); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + + /** + * Upsert reconciles relationship `onDelete` drift through the SDK's + * `updateRelationshipAttribute` endpoint — the only relationship fields + * the SDK exposes for in-place edit are `onDelete` and `newKey`. Any + * structural change (`relationType`, `twoWay`, `twoWayKey`, + * `relatedCollection`) is a non-SDK field and must drop+recreate via + * `deleteRelationship`. This test exercises the in-place path: change + * `onDelete` cascade→restrict on source, re-migrate Upsert, assert dest + * reflects the new value without dropping the column. + */ + public function testAppwriteMigrationUpsertUpdatesRelationshipOnDeleteInPlace(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $databaseId = ID::unique(); + $createDb = $this->client->call(Client::METHOD_POST, '/databases', $sourceHeaders, [ + 'databaseId' => $databaseId, + 'name' => 'Rel In-Place DB', + ]); + $this->assertEquals(201, $createDb['headers']['status-code']); + + foreach (['parents', 'children'] as $tbl) { + $createTable = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', $sourceHeaders, [ + 'tableId' => $tbl, + 'name' => $tbl, + ]); + $this->assertEquals(201, $createTable['headers']['status-code']); + } + + // One-way relationship parents → children. One-way is sufficient to + // exercise updateRelationshipInPlace; two-way pair-key dedup is + // covered by the existing two-way coverage in MigrationDocumentsDB. + $createRel = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/parents/columns/relationship', $sourceHeaders, [ + 'relatedTableId' => 'children', + 'type' => Database::RELATION_ONE_TO_MANY, + 'twoWay' => false, + 'key' => 'kids', + 'onDelete' => Database::RELATION_MUTATE_CASCADE, + ]); + $this->assertEquals(202, $createRel['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $sourceHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_CASCADE, $r['body']['onDelete']); + }, 10000, 500); + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + ]; + + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + $this->assertEventually(function () use ($databaseId, $destHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_CASCADE, $r['body']['onDelete']); + }, 10000, 500); + + sleep(1); + + // SDK-reachable: PATCH /columns/:key/relationship accepts onDelete. + $patch = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids/relationship', $sourceHeaders, [ + 'onDelete' => Database::RELATION_MUTATE_RESTRICT, + ]); + $this->assertEquals(200, $patch['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $sourceHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_RESTRICT, $r['body']['onDelete']); + }, 5000, 500); + + $upsertResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'upsert', + ]); + $this->assertEquals('completed', $upsertResult['status']); + + $this->assertEventually(function () use ($databaseId, $destHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_RESTRICT, $r['body']['onDelete'], 'updateRelationshipInPlace must propagate source onDelete'); + $this->assertEquals(Database::RELATION_ONE_TO_MANY, $r['body']['relationType'], 'In-place update must not change relationType'); + $this->assertFalse($r['body']['twoWay'], 'In-place update must not change twoWay'); + }, 10000, 500); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + /** * Storage */ From dfde1be03544301365f18450495c362ed4976928 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 27 Apr 2026 14:03:02 +0100 Subject: [PATCH 22/40] tests: cover two-way relationship onDelete in-place update + bump migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous version of this test created a one-way relationship, which falls through to DropAndRecreate (one-way + onDelete change is gated off in updateRelationshipInPlace because utopia's updateRelationship partner-cascade throws on one-way). It never exercised the in-place path it was named for. Converted to two-way (parents.kids ↔ children.parent), and asserted both parent- and partner-side onDelete on dest. Partner-side assertion is the regression guard for the partner-meta refresh that was missing from updateRelationshipInPlace. composer.lock: utopia-php/migration a36d95f -> c76de9a (partner-side onDelete sync fix). --- composer.lock | 8 +-- .../Services/Migrations/MigrationsBase.php | 58 ++++++++++++------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/composer.lock b/composer.lock index 084d4a5bfe..9f951576fb 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "a36d95f86d24b4f024a419191e69c3c034a593eb" + "reference": "c76de9acffa7fddac7cb1e969ebde63c053abcd3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/a36d95f86d24b4f024a419191e69c3c034a593eb", - "reference": "a36d95f86d24b4f024a419191e69c3c034a593eb", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/c76de9acffa7fddac7cb1e969ebde63c053abcd3", + "reference": "c76de9acffa7fddac7cb1e969ebde63c053abcd3", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-27T11:29:25+00:00" + "time": "2026-04-27T12:57:33+00:00" }, { "name": "utopia-php/mongo", diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 264f6a0ee1..c52cd8fb99 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1490,14 +1490,15 @@ trait MigrationsBase } /** - * Upsert reconciles relationship `onDelete` drift through the SDK's - * `updateRelationshipAttribute` endpoint — the only relationship fields - * the SDK exposes for in-place edit are `onDelete` and `newKey`. Any - * structural change (`relationType`, `twoWay`, `twoWayKey`, - * `relatedCollection`) is a non-SDK field and must drop+recreate via - * `deleteRelationship`. This test exercises the in-place path: change - * `onDelete` cascade→restrict on source, re-migrate Upsert, assert dest - * reflects the new value without dropping the column. + * Upsert reconciles two-way relationship `onDelete` drift via the SDK's + * updateRelationshipAttribute (only `onDelete`/`newKey` are SDK-reachable). + * Two-way is required to exercise updateRelationshipInPlace — one-way + + * onDelete change falls through to DropAndRecreate (utopia's + * updateRelationship partner-cascade throws on one-way). + * + * Asserts both sides of the relationship reflect the new onDelete on dest: + * utopia's updateRelationship syncs the physical constraint on both sides, + * but the Appwrite-level partner meta doc has to be refreshed explicitly. */ public function testAppwriteMigrationUpsertUpdatesRelationshipOnDeleteInPlace(): void { @@ -1527,14 +1528,13 @@ trait MigrationsBase $this->assertEquals(201, $createTable['headers']['status-code']); } - // One-way relationship parents → children. One-way is sufficient to - // exercise updateRelationshipInPlace; two-way pair-key dedup is - // covered by the existing two-way coverage in MigrationDocumentsDB. + // Two-way: parents.kids ↔ children.parent. Required to hit the in-place path. $createRel = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/parents/columns/relationship', $sourceHeaders, [ 'relatedTableId' => 'children', 'type' => Database::RELATION_ONE_TO_MANY, - 'twoWay' => false, + 'twoWay' => true, 'key' => 'kids', + 'twoWayKey' => 'parent', 'onDelete' => Database::RELATION_MUTATE_CASCADE, ]); $this->assertEquals(202, $createRel['headers']['status-code']); @@ -1560,11 +1560,17 @@ trait MigrationsBase ]); $this->assertEquals('completed', $first['status']); + // Both sides land on dest with onDelete=cascade. $this->assertEventually(function () use ($databaseId, $destHeaders) { - $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); - $this->assertEquals(200, $r['headers']['status-code']); - $this->assertEquals('available', $r['body']['status']); - $this->assertEquals(Database::RELATION_MUTATE_CASCADE, $r['body']['onDelete']); + $parent = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); + $this->assertEquals(200, $parent['headers']['status-code']); + $this->assertEquals('available', $parent['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_CASCADE, $parent['body']['onDelete']); + + $child = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/children/columns/parent', $destHeaders); + $this->assertEquals(200, $child['headers']['status-code']); + $this->assertEquals('available', $child['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_CASCADE, $child['body']['onDelete']); }, 10000, 500); sleep(1); @@ -1591,13 +1597,21 @@ trait MigrationsBase ]); $this->assertEquals('completed', $upsertResult['status']); + // Both sides on dest must reflect onDelete=restrict. Asserting the + // partner side is the regression guard for the previously-missed + // partner meta refresh in updateRelationshipInPlace. $this->assertEventually(function () use ($databaseId, $destHeaders) { - $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); - $this->assertEquals(200, $r['headers']['status-code']); - $this->assertEquals('available', $r['body']['status']); - $this->assertEquals(Database::RELATION_MUTATE_RESTRICT, $r['body']['onDelete'], 'updateRelationshipInPlace must propagate source onDelete'); - $this->assertEquals(Database::RELATION_ONE_TO_MANY, $r['body']['relationType'], 'In-place update must not change relationType'); - $this->assertFalse($r['body']['twoWay'], 'In-place update must not change twoWay'); + $parent = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); + $this->assertEquals(200, $parent['headers']['status-code']); + $this->assertEquals('available', $parent['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_RESTRICT, $parent['body']['onDelete'], 'parent-side onDelete must reflect source'); + $this->assertEquals(Database::RELATION_ONE_TO_MANY, $parent['body']['relationType'], 'In-place update must not change relationType'); + $this->assertTrue($parent['body']['twoWay'], 'In-place update must not change twoWay'); + + $child = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/children/columns/parent', $destHeaders); + $this->assertEquals(200, $child['headers']['status-code']); + $this->assertEquals('available', $child['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_RESTRICT, $child['body']['onDelete'], 'partner-side onDelete must reflect source after in-place update'); }, 10000, 500); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); From c80c747e4896af8d1f2bfc4bca5bf3ff693909b0 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 27 Apr 2026 14:45:39 +0100 Subject: [PATCH 23/40] tests: pin two-way recreate partner-side dedup + bump migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit testAppwriteMigrationUpsertTwoWayRecreateSkipsPartnerSide exercises the DropAndRecreate path on a two-way relationship that the partner- side pair-key dedup guards. Source recreates the relationship between runs, forcing parent-side createdAt diff. Test asserts the migration completes cleanly and partner-table rows survive — without dedup, the partner pass re-fires DropAndRecreate and destroys those rows. composer.lock: utopia-php/migration c76de9a -> c13e77d (partner-side pair-key dedup restored). --- composer.lock | 8 +- .../Services/Migrations/MigrationsBase.php | 154 ++++++++++++++++++ 2 files changed, 158 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 9f951576fb..2a8c2cd5be 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "c76de9acffa7fddac7cb1e969ebde63c053abcd3" + "reference": "c13e77d562e775b66ab8c03ee5c8163e53c3067e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/c76de9acffa7fddac7cb1e969ebde63c053abcd3", - "reference": "c76de9acffa7fddac7cb1e969ebde63c053abcd3", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/c13e77d562e775b66ab8c03ee5c8163e53c3067e", + "reference": "c13e77d562e775b66ab8c03ee5c8163e53c3067e", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-27T12:57:33+00:00" + "time": "2026-04-27T13:42:58+00:00" }, { "name": "utopia-php/mongo", diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index c52cd8fb99..64ff4af596 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1621,6 +1621,160 @@ trait MigrationsBase self::$cachedTableData = []; } + /** + * Two-way DropAndRecreate path: source recreates the relationship between + * runs (createdAt diff). Without partner-side pair-key dedup, the second + * createField pass for the partner can re-fire DropAndRecreate after the + * first side already reconciled both physical columns, destroying rows + * already migrated to the partner table this run. + * + * Asserts: migration completes, both sides exist on dest, child rows + * referencing the parent are preserved end-to-end. + */ + public function testAppwriteMigrationUpsertTwoWayRecreateSkipsPartnerSide(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $databaseId = ID::unique(); + $createDb = $this->client->call(Client::METHOD_POST, '/databases', $sourceHeaders, [ + 'databaseId' => $databaseId, + 'name' => 'Two-Way Recreate DB', + ]); + $this->assertEquals(201, $createDb['headers']['status-code']); + + foreach (['parents', 'children'] as $tbl) { + $createTable = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', $sourceHeaders, [ + 'tableId' => $tbl, + 'name' => $tbl, + 'permissions' => [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ]); + $this->assertEquals(201, $createTable['headers']['status-code']); + } + + $createRel = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/parents/columns/relationship', $sourceHeaders, [ + 'relatedTableId' => 'children', + 'type' => Database::RELATION_ONE_TO_MANY, + 'twoWay' => true, + 'key' => 'kids', + 'twoWayKey' => 'parent', + 'onDelete' => Database::RELATION_MUTATE_CASCADE, + ]); + $this->assertEquals(202, $createRel['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $sourceHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + }, 10000, 500); + + $parentRow = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/parents/rows', $sourceHeaders, [ + 'rowId' => 'parent-1', + 'data' => [], + ]); + $this->assertEquals(201, $parentRow['headers']['status-code']); + $childRow = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/children/rows', $sourceHeaders, [ + 'rowId' => 'child-1', + 'data' => ['parent' => 'parent-1'], + ]); + $this->assertEquals(201, $childRow['headers']['status-code']); + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + Resource::TYPE_ROW, + ]; + + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + // Recreate the relationship on source so its createdAt advances past + // dest's stored value — forces SchemaAction::DropAndRecreate on the + // parent side, which is the path the partner-side dedup guards. + sleep(1); + $deleteRel = $this->client->call(Client::METHOD_DELETE, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $sourceHeaders); + $this->assertEquals(204, $deleteRel['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $sourceHeaders); + $this->assertEquals(404, $r['headers']['status-code']); + }, 10000, 500); + + sleep(1); + $recreate = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/parents/columns/relationship', $sourceHeaders, [ + 'relatedTableId' => 'children', + 'type' => Database::RELATION_ONE_TO_MANY, + 'twoWay' => true, + 'key' => 'kids', + 'twoWayKey' => 'parent', + 'onDelete' => Database::RELATION_MUTATE_CASCADE, + ]); + $this->assertEquals(202, $recreate['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $sourceHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + }, 10000, 500); + + // Child-row's relationship was wiped by the source-side delete. Re-link. + $relink = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/children/rows/child-1', $sourceHeaders, [ + 'data' => ['parent' => 'parent-1'], + ]); + $this->assertEquals(200, $relink['headers']['status-code']); + + $upsertResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'upsert', + ]); + $this->assertEquals('completed', $upsertResult['status']); + + $this->assertEventually(function () use ($databaseId, $destHeaders) { + $parent = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); + $this->assertEquals(200, $parent['headers']['status-code']); + $this->assertEquals('available', $parent['body']['status']); + + $child = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/children/columns/parent', $destHeaders); + $this->assertEquals(200, $child['headers']['status-code']); + $this->assertEquals('available', $child['body']['status']); + }, 10000, 500); + + // Both rows survive the re-migration. If the partner-side dedup were + // missing and the partner pass re-fired DropAndRecreate, the partner + // (children) table's row would have been wiped before the row pass. + $destChild = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/children/rows/child-1', $destHeaders); + $this->assertEquals(200, $destChild['headers']['status-code'], 'partner-table row must survive two-way recreate re-migration'); + $this->assertEquals('parent-1', $destChild['body']['parent']['$id'] ?? $destChild['body']['parent'], 'partner-table row relationship must point to the migrated parent'); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + /** * Storage */ From 7b32fd0196ecfd2f116062c0068dde231febad4d Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 27 Apr 2026 17:32:14 +0100 Subject: [PATCH 24/40] composer: bump utopia-php/migration to 09c1b21 (maintainability pass) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 2a8c2cd5be..a5e5d52d35 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "c13e77d562e775b66ab8c03ee5c8163e53c3067e" + "reference": "09c1b2133d3ec42d11aee345f003d067d1de9ae5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/c13e77d562e775b66ab8c03ee5c8163e53c3067e", - "reference": "c13e77d562e775b66ab8c03ee5c8163e53c3067e", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/09c1b2133d3ec42d11aee345f003d067d1de9ae5", + "reference": "09c1b2133d3ec42d11aee345f003d067d1de9ae5", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-27T13:42:58+00:00" + "time": "2026-04-27T16:27:12+00:00" }, { "name": "utopia-php/mongo", From b03c901fa758504691691c85a64a8738ec9ba234 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 27 Apr 2026 17:40:23 +0100 Subject: [PATCH 25/40] tests: cover one-way DropAndRecreate + attribute-recreate scenarios MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two coverage gaps closed: - testAppwriteMigrationUpsertOneWayRelationshipDropAndRecreate exercises the path that updateRelationshipInPlace gates off: one-way + onDelete change → returns false → falls through to DropAndRecreate via deleteRelationship. Coverage was lost when testAppwriteMigrationUpsertUpdatesRelationshipOnDeleteInPlace was converted to two-way to actually hit the in-place path. - testAppwriteMigrationUpsertAttributeRecreateDropsAndRecreates pins the createdAt-different leaf path: source drops + recreates the attribute (createdAt advances), re-migration must DropAndRecreate on dest and re-flow the row data through the row pass. Companion to testAppwriteMigrationUpsertUpdatesAttributeInPlace which covers the same-createdAt + newer-updatedAt path. Migration package already at 09c1b21 (the maintainability commit) from the previous lock bump — no further composer.lock change needed. --- .../Services/Migrations/MigrationsBase.php | 215 ++++++++++++++++++ 1 file changed, 215 insertions(+) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 64ff4af596..cc399cc768 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1775,6 +1775,221 @@ trait MigrationsBase self::$cachedTableData = []; } + /** + * One-way + onDelete change is gated to false in updateRelationshipInPlace + * (utopia's updateRelationship partner-cascade throws on one-way), so the + * caller falls through to DropAndRecreate via deleteRelationship. Asserts + * dest's onDelete reflects source's new value end-to-end. Companion to + * testAppwriteMigrationUpsertUpdatesRelationshipOnDeleteInPlace which + * exercises the two-way in-place path. + */ + public function testAppwriteMigrationUpsertOneWayRelationshipDropAndRecreate(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $databaseId = ID::unique(); + $createDb = $this->client->call(Client::METHOD_POST, '/databases', $sourceHeaders, [ + 'databaseId' => $databaseId, + 'name' => 'One-Way DropAndRecreate DB', + ]); + $this->assertEquals(201, $createDb['headers']['status-code']); + + foreach (['parents', 'children'] as $tbl) { + $createTable = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', $sourceHeaders, [ + 'tableId' => $tbl, + 'name' => $tbl, + ]); + $this->assertEquals(201, $createTable['headers']['status-code']); + } + + $createRel = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/parents/columns/relationship', $sourceHeaders, [ + 'relatedTableId' => 'children', + 'type' => Database::RELATION_ONE_TO_MANY, + 'twoWay' => false, + 'key' => 'kids', + 'onDelete' => Database::RELATION_MUTATE_CASCADE, + ]); + $this->assertEquals(202, $createRel['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $sourceHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + }, 10000, 500); + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + ]; + + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + $this->assertEventually(function () use ($databaseId, $destHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_CASCADE, $r['body']['onDelete']); + }, 10000, 500); + + sleep(1); + + $patch = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids/relationship', $sourceHeaders, [ + 'onDelete' => Database::RELATION_MUTATE_RESTRICT, + ]); + $this->assertEquals(200, $patch['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $sourceHeaders); + $this->assertEquals('available', $r['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_RESTRICT, $r['body']['onDelete']); + }, 5000, 500); + + $upsertResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'upsert', + ]); + $this->assertEquals('completed', $upsertResult['status']); + + $this->assertEventually(function () use ($databaseId, $destHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + $this->assertEquals(Database::RELATION_MUTATE_RESTRICT, $r['body']['onDelete'], 'one-way DropAndRecreate must propagate source onDelete'); + $this->assertEquals(Database::RELATION_ONE_TO_MANY, $r['body']['relationType'], 'DropAndRecreate must preserve relationType'); + $this->assertFalse($r['body']['twoWay'], 'DropAndRecreate must preserve twoWay=false'); + }, 10000, 500); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + + /** + * Source drops + recreates a regular attribute between runs (createdAt + * differs, leaf, canDrop=true). Re-migration must DropAndRecreate the + * destination attribute and re-flow the row data — proving the + * createdAt-aware decision distinguishes "physically recreated" from + * "metadata edit". Pairs with testAppwriteMigrationUpsertUpdatesAttributeInPlace + * which exercises the same-createdAt + newer-updatedAt path. + */ + public function testAppwriteMigrationUpsertAttributeRecreateDropsAndRecreates(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $data = $this->setupMigrationTable(); + $databaseId = $data['databaseId']; + $tableId = $data['tableId']; + $rowId = 'row-after-recreate'; + + $row = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', $sourceHeaders, [ + 'rowId' => $rowId, + 'data' => ['name' => 'before-recreate'], + ]); + $this->assertEquals(201, $row['headers']['status-code']); + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + Resource::TYPE_ROW, + ]; + + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + sleep(1); + + // Drop + recreate the column on source. createdAt advances → re-migration + // must take the createdAt-diff DropAndRecreate path on dest. + $delete = $this->client->call(Client::METHOD_DELETE, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $sourceHeaders); + $this->assertEquals(204, $delete['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $tableId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $sourceHeaders); + $this->assertEquals(404, $r['headers']['status-code']); + }, 10000, 500); + + $recreate = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', $sourceHeaders, [ + 'key' => 'name', + 'size' => 100, + 'required' => false, + ]); + $this->assertEquals(202, $recreate['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $tableId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $sourceHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + }, 10000, 500); + + // Source row's data was nulled by the source-side delete. Set fresh value. + $relink = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $sourceHeaders, [ + 'data' => ['name' => 'after-recreate'], + ]); + $this->assertEquals(200, $relink['headers']['status-code']); + + $upsertResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'upsert', + ]); + $this->assertEquals('completed', $upsertResult['status']); + + $this->assertEventually(function () use ($databaseId, $tableId, $destHeaders) { + $col = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); + $this->assertEquals(200, $col['headers']['status-code']); + $this->assertEquals('available', $col['body']['status']); + $this->assertFalse($col['body']['required'], 'recreated column must reflect the new spec (required=false)'); + }, 10000, 500); + + $rowAfter = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); + $this->assertEquals(200, $rowAfter['headers']['status-code']); + $this->assertEquals('after-recreate', $rowAfter['body']['name'], 'row pass must repopulate the recreated column with source value'); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + /** * Storage */ From 8cccc7993580120c3b13d2dffd3f38c35f2d7d89 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 03:31:25 +0100 Subject: [PATCH 26/40] composer: bump utopia-php/migration to 47933c1 (inline TwoWayPartner) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index a5e5d52d35..f3aa60fb2e 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "09c1b2133d3ec42d11aee345f003d067d1de9ae5" + "reference": "47933c1dd01173d7bda38572593f28a68a740e45" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/09c1b2133d3ec42d11aee345f003d067d1de9ae5", - "reference": "09c1b2133d3ec42d11aee345f003d067d1de9ae5", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/47933c1dd01173d7bda38572593f28a68a740e45", + "reference": "47933c1dd01173d7bda38572593f28a68a740e45", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-27T16:27:12+00:00" + "time": "2026-04-28T02:30:55+00:00" }, { "name": "utopia-php/mongo", From ac46ff802a1d34951b252d66a0a1806de04da5a9 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 03:45:25 +0100 Subject: [PATCH 27/40] composer: bump utopia-php/migration to 24fd23b (SDK-boundary lock-in test) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index f3aa60fb2e..ebce2f687b 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "47933c1dd01173d7bda38572593f28a68a740e45" + "reference": "24fd23b676ee7c7163e583c8559f362471940954" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/47933c1dd01173d7bda38572593f28a68a740e45", - "reference": "47933c1dd01173d7bda38572593f28a68a740e45", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/24fd23b676ee7c7163e583c8559f362471940954", + "reference": "24fd23b676ee7c7163e583c8559f362471940954", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-28T02:30:55+00:00" + "time": "2026-04-28T02:44:46+00:00" }, { "name": "utopia-php/mongo", From 3fba7afd2e4afe2a3f98e09f7e5adcb10926da2d Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 04:16:51 +0100 Subject: [PATCH 28/40] tests: pin spec-match guard + bump migration to c8d1789 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit testAppwriteMigrationUpsertSameSpecRecreateTolerates exercises the new spec-match guard added in utopia-php/migration c8d1789. Source drops + recreates a column with the EXACT same spec as before; createdAt advances but specs match → action is forced to Tolerate. Asserts dest column's $createdAt stays at first-migration value (proving Tolerate, not DropAndRecreate). Row pass under Upsert still propagates source's new row value. Companion to testAppwriteMigrationUpsertAttributeRecreateDropsAndRecreates which exercises the spec-DIFFERS path: same precondition (drop + recreate), different outcome (DropAndRecreate vs Tolerate) gated on spec equality. composer.lock: utopia-php/migration 24fd23b -> c8d1789 (spec-match guard). --- composer.lock | 8 +- .../Services/Migrations/MigrationsBase.php | 114 ++++++++++++++++++ 2 files changed, 118 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index ebce2f687b..15a7039b6d 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "24fd23b676ee7c7163e583c8559f362471940954" + "reference": "c8d17898458de4a297ba03fd1a126f7aa13227af" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/24fd23b676ee7c7163e583c8559f362471940954", - "reference": "24fd23b676ee7c7163e583c8559f362471940954", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/c8d17898458de4a297ba03fd1a126f7aa13227af", + "reference": "c8d17898458de4a297ba03fd1a126f7aa13227af", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-28T02:44:46+00:00" + "time": "2026-04-28T03:12:58+00:00" }, { "name": "utopia-php/mongo", diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index cc399cc768..b38da99554 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1990,6 +1990,120 @@ trait MigrationsBase self::$cachedTableData = []; } + /** + * Source drops + recreates a column with the EXACT same spec. createdAt + * advances on source, but the spec-match guard short-circuits the + * DropAndRecreate to Tolerate — dest's column meta doc stays untouched + * (verified via $createdAt invariance). Row pass under Upsert still + * propagates source's new row values via upsertDocuments. + * + * Companion to testAppwriteMigrationUpsertAttributeRecreateDropsAndRecreates + * which exercises the spec-DIFFERS path: same precondition, different + * outcome based on whether spec matches. + */ + public function testAppwriteMigrationUpsertSameSpecRecreateTolerates(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + $data = $this->setupMigrationTable(); + $databaseId = $data['databaseId']; + $tableId = $data['tableId']; + $rowId = 'row-spec-match'; + + $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', $sourceHeaders, [ + 'rowId' => $rowId, + 'data' => ['name' => 'before-recreate'], + ]); + + $resources = [ + Resource::TYPE_DATABASE, + Resource::TYPE_TABLE, + Resource::TYPE_COLUMN, + Resource::TYPE_ROW, + ]; + + $first = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + $this->assertEquals('completed', $first['status']); + + $destBefore = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); + $this->assertEquals(200, $destBefore['headers']['status-code']); + $destCreatedAtBefore = $destBefore['body']['$createdAt']; + + sleep(1); + + // Drop + recreate with the EXACT same spec as setupMigrationTable + // (size=100, required=true). Source's $createdAt advances but the + // spec is identical → spec-match guard must force Tolerate. + $delete = $this->client->call(Client::METHOD_DELETE, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $sourceHeaders); + $this->assertEquals(204, $delete['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $tableId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $sourceHeaders); + $this->assertEquals(404, $r['headers']['status-code']); + }, 10000, 500); + + $recreate = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', $sourceHeaders, [ + 'key' => 'name', + 'size' => 100, + 'required' => true, + ]); + $this->assertEquals(202, $recreate['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $tableId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $sourceHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + }, 10000, 500); + + $relink = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $sourceHeaders, [ + 'data' => ['name' => 'after-recreate'], + ]); + $this->assertEquals(200, $relink['headers']['status-code']); + + $upsertResult = $this->performMigrationSync([ + 'resources' => $resources, + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + 'onDuplicate' => 'upsert', + ]); + $this->assertEquals('completed', $upsertResult['status']); + + // Spec-match guard fired → dest column's $createdAt stayed at the + // first-migration value. If DropAndRecreate had run, $createdAt + // would have been bumped to source's NEW createdAt. + $destAfter = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); + $this->assertEquals(200, $destAfter['headers']['status-code']); + $this->assertEquals($destCreatedAtBefore, $destAfter['body']['$createdAt'], 'spec-match guard must keep dest column meta untouched'); + $this->assertEquals(100, $destAfter['body']['size']); + $this->assertTrue($destAfter['body']['required']); + + // Row pass under Upsert still propagated source's new row value. + $rowAfter = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); + $this->assertEquals(200, $rowAfter['headers']['status-code']); + $this->assertEquals('after-recreate', $rowAfter['body']['name']); + + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); + $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); + + self::$cachedDatabaseData = []; + self::$cachedTableData = []; + } + /** * Storage */ From b6afef6efcf5fa07b0dcb3f2e2ad81a622ba780a Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 04:44:19 +0100 Subject: [PATCH 29/40] tests: trim verbose multi-paragraph docblocks to one-liners --- .../Services/Migrations/MigrationsBase.php | 122 ++---------------- 1 file changed, 13 insertions(+), 109 deletions(-) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index b38da99554..5d8ff48e53 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -761,15 +761,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Appwrite → Appwrite row re-migration honoring onDuplicate=skip and onDuplicate=upsert. - * - * With utopia-php/migration's DestinationAppwrite handling schema tolerance - * (pre-check the destination `_metadata` for each database / table / column - * / index, tolerate existing in Skip/Upsert), re-migration completes - * cleanly — no more schema-level errors to tolerate. The test asserts - * strict 'completed' status via performMigrationSync on every run. - */ + /** Rows under all three modes; schema tolerance lets every run hit 'completed'. */ public function testAppwriteMigrationRowsOnDuplicate(): void { $sourceHeaders = [ @@ -854,14 +846,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Re-migrating unchanged source (Skip / Upsert) completes cleanly without - * touching destination rows. Proves the schema-tolerance path: every - * database / table / attribute on destination already exists with a - * matching spec, so DestinationAppwrite's pre-check returns Tolerate for - * every resource and no-ops row writes go through the DB-native conflict - * primitives (INSERT IGNORE / ON DUPLICATE KEY UPDATE). - */ + /** Unchanged source under Skip/Upsert is a no-op — every resource Tolerated. */ public function testAppwriteMigrationReRunIsIdempotent(): void { $sourceHeaders = [ @@ -939,13 +924,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Upsert re-migration reconciles container metadata drift: when source's - * database or table was modified between runs (rename, enabled flag, - * permissions tightening), the Upsert pre-check returns UpdateInPlace - * and migration's updateDocument propagates source values to dest. - * Children (rows) are preserved. - */ + /** Upsert reconciles container drift via UpdateInPlace; children (rows) preserved. */ public function testAppwriteMigrationUpsertUpdatesContainerMetadata(): void { $sourceHeaders = [ @@ -1035,14 +1014,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Skip mode is strict "don't touch" — destination container metadata - * drift (permissions tightened post-migration, rename on dest) is - * preserved on every re-run, even when source has diverged. Guards - * against a common production workflow: dev→prod migrate, ops tightens - * prod permissions, later schema-only re-sync must not wipe out the - * tightening. - */ + /** Skip preserves dest container drift even when source has diverged. */ public function testAppwriteMigrationSkipPreservesContainerDrift(): void { $sourceHeaders = [ @@ -1117,14 +1089,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Upsert re-migration reconciles column-level drift: a column that - * exists only on destination (e.g. from a subsequent dest-side edit, or - * left over after a source-side rename) must be dropped so destination's - * schema matches what source declares. Source-declared columns are - * preserved. Rows land after the orphan drop so row upsert doesn't - * fail on orphan required columns. - */ + /** Upsert drops dest columns source no longer declares; cleanup runs before rows land. */ public function testAppwriteMigrationUpsertDropsOrphanColumn(): void { $sourceHeaders = [ @@ -1211,11 +1176,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Skip mode never touches destination, including orphan columns. - * Pairs with testAppwriteMigrationUpsertDropsOrphanColumn to prove the - * cleanup is gated correctly: only Upsert reconciles, Skip tolerates. - */ + /** Skip preserves orphan columns; cleanup is Upsert-only. */ public function testAppwriteMigrationSkipKeepsOrphanColumn(): void { $sourceHeaders = [ @@ -1291,14 +1252,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Upsert reconciles attribute-level metadata edits using the Appwrite SDK's - * per-type updateXAttribute endpoint instead of drop+recreate. Source-side - * PATCH of fields the SDK can express (`required`, `default`, `size` for - * strings) — same `$createdAt` on both sides, source `$updatedAt` newer — - * routes through `updateAttributeInPlace` on DestinationAppwrite. Existing - * row data must survive (drop+recreate would have wiped the column). - */ + /** SDK-reachable attribute change propagates via updateAttributeInPlace; row data preserved. */ public function testAppwriteMigrationUpsertUpdatesAttributeInPlace(): void { $sourceHeaders = [ @@ -1396,14 +1350,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Skip mode is "don't touch" at the attribute level too: destination - * column drift (ops loosened a column post-migration) must survive a - * Skip re-run, even when source's `$updatedAt` is strictly newer. - * Pairs with testAppwriteMigrationSkipPreservesContainerDrift but - * exercises the leaf path (`canDrop = true`) instead of the container - * path. Regression guard against Skip ever consulting timestamps. - */ + /** Skip preserves dest attribute drift; leaf-level analog of the container drift test. */ public function testAppwriteMigrationSkipPreservesAttributeDrift(): void { $sourceHeaders = [ @@ -1489,17 +1436,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Upsert reconciles two-way relationship `onDelete` drift via the SDK's - * updateRelationshipAttribute (only `onDelete`/`newKey` are SDK-reachable). - * Two-way is required to exercise updateRelationshipInPlace — one-way + - * onDelete change falls through to DropAndRecreate (utopia's - * updateRelationship partner-cascade throws on one-way). - * - * Asserts both sides of the relationship reflect the new onDelete on dest: - * utopia's updateRelationship syncs the physical constraint on both sides, - * but the Appwrite-level partner meta doc has to be refreshed explicitly. - */ + /** Two-way onDelete change updates in place on both sides; partner meta refreshed by hand. */ public function testAppwriteMigrationUpsertUpdatesRelationshipOnDeleteInPlace(): void { $sourceHeaders = [ @@ -1621,16 +1558,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Two-way DropAndRecreate path: source recreates the relationship between - * runs (createdAt diff). Without partner-side pair-key dedup, the second - * createField pass for the partner can re-fire DropAndRecreate after the - * first side already reconciled both physical columns, destroying rows - * already migrated to the partner table this run. - * - * Asserts: migration completes, both sides exist on dest, child rows - * referencing the parent are preserved end-to-end. - */ + /** Pair-key dedup prevents partner DropAndRecreate from wiping rows already migrated this run. */ public function testAppwriteMigrationUpsertTwoWayRecreateSkipsPartnerSide(): void { $sourceHeaders = [ @@ -1775,14 +1703,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * One-way + onDelete change is gated to false in updateRelationshipInPlace - * (utopia's updateRelationship partner-cascade throws on one-way), so the - * caller falls through to DropAndRecreate via deleteRelationship. Asserts - * dest's onDelete reflects source's new value end-to-end. Companion to - * testAppwriteMigrationUpsertUpdatesRelationshipOnDeleteInPlace which - * exercises the two-way in-place path. - */ + /** One-way + onDelete change falls through to DropAndRecreate (in-place gated off for one-way). */ public function testAppwriteMigrationUpsertOneWayRelationshipDropAndRecreate(): void { $sourceHeaders = [ @@ -1885,14 +1806,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Source drops + recreates a regular attribute between runs (createdAt - * differs, leaf, canDrop=true). Re-migration must DropAndRecreate the - * destination attribute and re-flow the row data — proving the - * createdAt-aware decision distinguishes "physically recreated" from - * "metadata edit". Pairs with testAppwriteMigrationUpsertUpdatesAttributeInPlace - * which exercises the same-createdAt + newer-updatedAt path. - */ + /** Source drops+recreates with DIFFERENT spec: DropAndRecreate, row pass refills. */ public function testAppwriteMigrationUpsertAttributeRecreateDropsAndRecreates(): void { $sourceHeaders = [ @@ -1990,17 +1904,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** - * Source drops + recreates a column with the EXACT same spec. createdAt - * advances on source, but the spec-match guard short-circuits the - * DropAndRecreate to Tolerate — dest's column meta doc stays untouched - * (verified via $createdAt invariance). Row pass under Upsert still - * propagates source's new row values via upsertDocuments. - * - * Companion to testAppwriteMigrationUpsertAttributeRecreateDropsAndRecreates - * which exercises the spec-DIFFERS path: same precondition, different - * outcome based on whether spec matches. - */ + /** Source drops+recreates with SAME spec: spec-match guard forces Tolerate; dest meta untouched. */ public function testAppwriteMigrationUpsertSameSpecRecreateTolerates(): void { $sourceHeaders = [ From f5730e8eedc41895a050896caae4e5ebdce57bce Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 05:16:46 +0100 Subject: [PATCH 30/40] composer: bump utopia-php/migration to 36a1acf (spec-match order fix) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 15a7039b6d..b1790ec212 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "c8d17898458de4a297ba03fd1a126f7aa13227af" + "reference": "36a1acf75027a22b0ad1a7374b8f88c94ab30f68" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/c8d17898458de4a297ba03fd1a126f7aa13227af", - "reference": "c8d17898458de4a297ba03fd1a126f7aa13227af", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/36a1acf75027a22b0ad1a7374b8f88c94ab30f68", + "reference": "36a1acf75027a22b0ad1a7374b8f88c94ab30f68", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-28T03:12:58+00:00" + "time": "2026-04-28T04:16:28+00:00" }, { "name": "utopia-php/mongo", From 5a928f2c0f0e5b7de7605706a1479b18b39b43a0 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 05:43:43 +0100 Subject: [PATCH 31/40] composer: bump utopia-php/migration to 6ec2c45 (drop createdAt) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index b1790ec212..032f778700 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "36a1acf75027a22b0ad1a7374b8f88c94ab30f68" + "reference": "6ec2c45a4077d9452ae32e4faa1b49375ad77cae" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/36a1acf75027a22b0ad1a7374b8f88c94ab30f68", - "reference": "36a1acf75027a22b0ad1a7374b8f88c94ab30f68", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/6ec2c45a4077d9452ae32e4faa1b49375ad77cae", + "reference": "6ec2c45a4077d9452ae32e4faa1b49375ad77cae", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-28T04:16:28+00:00" + "time": "2026-04-28T04:43:11+00:00" }, { "name": "utopia-php/mongo", From 443f5cfb0ec9ce813006e66ec1e0fd566141a839 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 05:55:38 +0100 Subject: [PATCH 32/40] tests: switch attribute-recreate test to non-SDK change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After dropping createdAt from resolveSchemaAction, source-side recreate no longer routes through DropAndRecreate via the outer decision. The inner fallthrough still drops+recreates when the spec diff is a non-SDK change, so this test now toggles 'array' (a non-SDK field) on recreate to actually exercise the drop+recreate path it pins. Also clarifies the two-way recreate test's docblock — with createdAt gone and identical spec on recreate, it exercises spec-match + pair-key dedup (both tolerate paths) rather than parent-side drop. End-state assertions unchanged. --- .../e2e/Services/Migrations/MigrationsBase.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 5d8ff48e53..35dfad297d 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1558,7 +1558,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** Pair-key dedup prevents partner DropAndRecreate from wiping rows already migrated this run. */ + /** Two-way recreate with same spec: spec-match guard tolerates parent; pair-key dedup tolerates partner. Both sides + child rows preserved. */ public function testAppwriteMigrationUpsertTwoWayRecreateSkipsPartnerSide(): void { $sourceHeaders = [ @@ -1806,7 +1806,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** Source drops+recreates with DIFFERENT spec: DropAndRecreate, row pass refills. */ + /** Recreate with non-SDK spec change (array toggle): updateAttributeInPlace bails → drop+recreate; row pass refills. */ public function testAppwriteMigrationUpsertAttributeRecreateDropsAndRecreates(): void { $sourceHeaders = [ @@ -1858,10 +1858,15 @@ trait MigrationsBase $this->assertEquals(404, $r['headers']['status-code']); }, 10000, 500); + // Recreate with `array: true` — a non-SDK change (`array` is in + // ATTRIBUTE_NON_SDK_FIELDS). Forces updateAttributeInPlace to bail + // and the caller to fall through to drop+recreate, which is what + // this test pins. $recreate = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', $sourceHeaders, [ 'key' => 'name', 'size' => 100, 'required' => false, + 'array' => true, ]); $this->assertEquals(202, $recreate['headers']['status-code']); @@ -1871,9 +1876,9 @@ trait MigrationsBase $this->assertEquals('available', $r['body']['status']); }, 10000, 500); - // Source row's data was nulled by the source-side delete. Set fresh value. + // Source row's data was nulled by the source-side delete. Set a list value (column is array=true now). $relink = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $sourceHeaders, [ - 'data' => ['name' => 'after-recreate'], + 'data' => ['name' => ['after-recreate']], ]); $this->assertEquals(200, $relink['headers']['status-code']); @@ -1890,12 +1895,13 @@ trait MigrationsBase $col = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); $this->assertEquals(200, $col['headers']['status-code']); $this->assertEquals('available', $col['body']['status']); - $this->assertFalse($col['body']['required'], 'recreated column must reflect the new spec (required=false)'); + $this->assertTrue($col['body']['array'], 'recreated column must reflect the new spec (array=true)'); + $this->assertFalse($col['body']['required']); }, 10000, 500); $rowAfter = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); $this->assertEquals(200, $rowAfter['headers']['status-code']); - $this->assertEquals('after-recreate', $rowAfter['body']['name'], 'row pass must repopulate the recreated column with source value'); + $this->assertEquals(['after-recreate'], $rowAfter['body']['name'], 'row pass must repopulate the recreated column with source value'); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); From 4ae4531355d89a5075cedac72459120555e11fca Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 07:06:25 +0100 Subject: [PATCH 33/40] composer: bump utopia-php/migration to aa1e7c7 (drop+recreate fixes) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 032f778700..87f442e9f1 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "6ec2c45a4077d9452ae32e4faa1b49375ad77cae" + "reference": "aa1e7c706326542a050354635f4290df43f06443" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/6ec2c45a4077d9452ae32e4faa1b49375ad77cae", - "reference": "6ec2c45a4077d9452ae32e4faa1b49375ad77cae", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/aa1e7c706326542a050354635f4290df43f06443", + "reference": "aa1e7c706326542a050354635f4290df43f06443", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-28T04:43:11+00:00" + "time": "2026-04-28T06:03:39+00:00" }, { "name": "utopia-php/mongo", From 40aa8cb1bdd3219a55c6b78722138e7ebfef4ea0 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 07:11:23 +0100 Subject: [PATCH 34/40] composer: bump utopia-php/migration to 4c3965b (tighten dropAttributeForRecreate) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 87f442e9f1..675b7d1d14 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "aa1e7c706326542a050354635f4290df43f06443" + "reference": "4c3965bc89557109d80ee1443f0efa59c07c8465" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/aa1e7c706326542a050354635f4290df43f06443", - "reference": "aa1e7c706326542a050354635f4290df43f06443", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/4c3965bc89557109d80ee1443f0efa59c07c8465", + "reference": "4c3965bc89557109d80ee1443f0efa59c07c8465", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-28T06:03:39+00:00" + "time": "2026-04-28T06:11:04+00:00" }, { "name": "utopia-php/mongo", From e1209614c5252c168bb44823795f6dcbec80efb3 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 07:56:14 +0100 Subject: [PATCH 35/40] composer: bump utopia-php/migration to d37efed (track partner key for orphan cleanup) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 675b7d1d14..77ac114061 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "4c3965bc89557109d80ee1443f0efa59c07c8465" + "reference": "d37efed0d977a5f0c164861e58fc807763b1115f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/4c3965bc89557109d80ee1443f0efa59c07c8465", - "reference": "4c3965bc89557109d80ee1443f0efa59c07c8465", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/d37efed0d977a5f0c164861e58fc807763b1115f", + "reference": "d37efed0d977a5f0c164861e58fc807763b1115f", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-28T06:11:04+00:00" + "time": "2026-04-28T06:55:53+00:00" }, { "name": "utopia-php/mongo", From 8f32d0168617ced138fb840e6daa8843952fecb9 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 08:10:39 +0100 Subject: [PATCH 36/40] composer: bump utopia-php/migration to 7d71505 (createIndex pre-check before count/validator) --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 77ac114061..6ee740d807 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "d37efed0d977a5f0c164861e58fc807763b1115f" + "reference": "7d71505ec0a5731f322bbdc83cc9bee32053df2e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/d37efed0d977a5f0c164861e58fc807763b1115f", - "reference": "d37efed0d977a5f0c164861e58fc807763b1115f", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/7d71505ec0a5731f322bbdc83cc9bee32053df2e", + "reference": "7d71505ec0a5731f322bbdc83cc9bee32053df2e", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-28T06:55:53+00:00" + "time": "2026-04-28T07:10:11+00:00" }, { "name": "utopia-php/mongo", From d4e32af792883202c047f81cfc9acce70889b908 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 30 Apr 2026 11:45:33 +0100 Subject: [PATCH 37/40] Migrate appwrite to OnDuplicate::Overwrite ('overwrite') MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Maintainer review on utopia-php/migration#171 renamed OnDuplicate::Upsert -> OnDuplicate::Overwrite (value 'upsert' -> 'overwrite') to align with Appwrite terms (skip / overwrite / fail). Applying the cross-repo ripple here: - app/controllers/api/migrations.php: 3 endpoint param descriptions updated ('upsert' -> 'overwrite' in the help text). The validator still uses OnDuplicate::values() so it auto-picks up the new value. - tests/e2e/Services/Migrations/MigrationsBase.php: all 'onDuplicate' => 'upsert' -> 'overwrite'; method names testAppwriteMigrationUpsert* -> testAppwriteMigrationOverwrite*; comments / assertion messages / local var names switched. - Left untouched: utopia's upsertDocuments operation, transaction TransactionState 'upsert' action, Operation validator — those refer to the database-level upsert primitive, not the OnDuplicate enum. composer.lock: utopia-php/migration 7d71505 -> b8ae7bc. --- app/controllers/api/migrations.php | 6 +- composer.lock | 8 +- .../Services/Migrations/MigrationsBase.php | 130 +++++++++--------- 3 files changed, 72 insertions(+), 72 deletions(-) diff --git a/app/controllers/api/migrations.php b/app/controllers/api/migrations.php index c9f810f353..73b93c4c5f 100644 --- a/app/controllers/api/migrations.php +++ b/app/controllers/api/migrations.php @@ -89,7 +89,7 @@ Http::post('/v1/migrations/appwrite') ->param('endpoint', '', new URL(), 'Source Appwrite endpoint') ->param('projectId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Source Project ID', false, ['dbForProject']) ->param('apiKey', '', new Text(512), 'Source API Key') - ->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. "upsert": replace existing row.', 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) ->inject('response') ->inject('dbForProject') ->inject('project') @@ -358,7 +358,7 @@ Http::post('/v1/migrations/csv/imports') ->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('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. "upsert": replace existing row.', 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) ->inject('response') ->inject('dbForProject') ->inject('dbForPlatform') @@ -665,7 +665,7 @@ Http::post('/v1/migrations/json/imports') ->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('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. "upsert": replace existing row.', 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) ->inject('response') ->inject('dbForProject') ->inject('dbForPlatform') diff --git a/composer.lock b/composer.lock index 6ee740d807..191eba9608 100644 --- a/composer.lock +++ b/composer.lock @@ -4532,12 +4532,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "7d71505ec0a5731f322bbdc83cc9bee32053df2e" + "reference": "b8ae7bc953d1897994eaa0de7e677687a19e5133" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/7d71505ec0a5731f322bbdc83cc9bee32053df2e", - "reference": "7d71505ec0a5731f322bbdc83cc9bee32053df2e", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/b8ae7bc953d1897994eaa0de7e677687a19e5133", + "reference": "b8ae7bc953d1897994eaa0de7e677687a19e5133", "shasum": "" }, "require": { @@ -4579,7 +4579,7 @@ "issues": "https://github.com/utopia-php/migration/issues", "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" }, - "time": "2026-04-28T07:10:11+00:00" + "time": "2026-04-30T10:24:12+00:00" }, { "name": "utopia-php/mongo", diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 35dfad297d..0f54eff658 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -824,20 +824,20 @@ trait MigrationsBase $this->assertEquals(200, $rowAfterSkip['headers']['status-code']); $this->assertEquals('Mutated', $rowAfterSkip['body']['name'], 'onDuplicate=skip must not overwrite destination row'); - // Re-migration with onDuplicate=upsert — strict completion; destination + // Re-migration with onDuplicate=overwrite — strict completion; destination // row restored to source value. - $upsertResult = $this->performMigrationSync([ + $overwriteResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); - $this->assertEquals('completed', $upsertResult['status']); + $this->assertEquals('completed', $overwriteResult['status']); - $rowAfterUpsert = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); - $this->assertEquals(200, $rowAfterUpsert['headers']['status-code']); - $this->assertEquals('Original', $rowAfterUpsert['body']['name'], 'onDuplicate=upsert must restore source value'); + $rowAfterOverwrite = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); + $this->assertEquals(200, $rowAfterOverwrite['headers']['status-code']); + $this->assertEquals('Original', $rowAfterOverwrite['body']['name'], 'onDuplicate=overwrite must restore source value'); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); @@ -846,7 +846,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** Unchanged source under Skip/Upsert is a no-op — every resource Tolerated. */ + /** Unchanged source under Skip/Overwrite is a no-op — every resource Tolerated. */ public function testAppwriteMigrationReRunIsIdempotent(): void { $sourceHeaders = [ @@ -900,16 +900,16 @@ trait MigrationsBase ]); $this->assertEquals('completed', $reRunSkip['status']); - // Re-run under Upsert: same unchanged source. Schema tolerance path + // Re-run under Overwrite: same unchanged source. Schema tolerance path // fires for each resource; rows go through DB-native upsert. - $reRunUpsert = $this->performMigrationSync([ + $reRunOverwrite = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); - $this->assertEquals('completed', $reRunUpsert['status']); + $this->assertEquals('completed', $reRunOverwrite['status']); foreach (['row-a', 'row-b'] as $rowId) { $check = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); @@ -924,8 +924,8 @@ trait MigrationsBase self::$cachedTableData = []; } - /** Upsert reconciles container drift via UpdateInPlace; children (rows) preserved. */ - public function testAppwriteMigrationUpsertUpdatesContainerMetadata(): void + /** Overwrite reconciles container drift via UpdateInPlace; children (rows) preserved. */ + public function testAppwriteMigrationOverwriteUpdatesContainerMetadata(): void { $sourceHeaders = [ 'content-type' => 'application/json', @@ -980,15 +980,15 @@ trait MigrationsBase 'enabled' => false, ]); - // Upsert re-migration: UpdateInPlace path fires for database + table. - $upsertResult = $this->performMigrationSync([ + // Overwrite re-migration: UpdateInPlace path fires for database + table. + $overwriteResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); - $this->assertEquals('completed', $upsertResult['status']); + $this->assertEquals('completed', $overwriteResult['status']); // Assert dest database metadata reflects source's new values. $destDb = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId, $destHeaders); @@ -999,13 +999,13 @@ trait MigrationsBase $destTable = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId, $destHeaders); $this->assertEquals(200, $destTable['headers']['status-code']); $this->assertEquals('Renamed Source Table', $destTable['body']['name']); - $this->assertFalse($destTable['body']['enabled'], 'Upsert must propagate source enabled=false'); - $this->assertTrue($destTable['body']['documentSecurity'] ?? $destTable['body']['rowSecurity'], 'Upsert must propagate source rowSecurity=true'); + $this->assertFalse($destTable['body']['enabled'], 'Overwrite must propagate source enabled=false'); + $this->assertTrue($destTable['body']['documentSecurity'] ?? $destTable['body']['rowSecurity'], 'Overwrite must propagate source rowSecurity=true'); // Child row untouched — UpdateInPlace only rewrites container metadata. $row = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); $this->assertEquals(200, $row['headers']['status-code']); - $this->assertEquals('SeedRow', $row['body']['name'], 'Upsert must not touch child rows when updating container metadata'); + $this->assertEquals('SeedRow', $row['body']['name'], 'Overwrite must not touch child rows when updating container metadata'); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); @@ -1089,8 +1089,8 @@ trait MigrationsBase self::$cachedTableData = []; } - /** Upsert drops dest columns source no longer declares; cleanup runs before rows land. */ - public function testAppwriteMigrationUpsertDropsOrphanColumn(): void + /** Overwrite drops dest columns source no longer declares; cleanup runs before rows land. */ + public function testAppwriteMigrationOverwriteDropsOrphanColumn(): void { $sourceHeaders = [ 'content-type' => 'application/json', @@ -1151,23 +1151,23 @@ trait MigrationsBase 'data' => ['name' => 'seed'], ]); - // Upsert re-migration: orphan_col must be dropped from dest. - $upsertResult = $this->performMigrationSync([ + // Overwrite re-migration: orphan_col must be dropped from dest. + $overwriteResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); - $this->assertEquals('completed', $upsertResult['status']); + $this->assertEquals('completed', $overwriteResult['status']); // Orphan column dropped. $orphanCheck = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/orphan_col', $destHeaders); - $this->assertEquals(404, $orphanCheck['headers']['status-code'], 'Upsert must drop destination column source no longer declares'); + $this->assertEquals(404, $orphanCheck['headers']['status-code'], 'Overwrite must drop destination column source no longer declares'); // Source's column preserved. $nameCheck = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); - $this->assertEquals(200, $nameCheck['headers']['status-code'], 'Upsert must preserve columns source declared'); + $this->assertEquals(200, $nameCheck['headers']['status-code'], 'Overwrite must preserve columns source declared'); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $destHeaders); $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, $sourceHeaders); @@ -1176,7 +1176,7 @@ trait MigrationsBase self::$cachedTableData = []; } - /** Skip preserves orphan columns; cleanup is Upsert-only. */ + /** Skip preserves orphan columns; cleanup is Overwrite-only. */ public function testAppwriteMigrationSkipKeepsOrphanColumn(): void { $sourceHeaders = [ @@ -1253,7 +1253,7 @@ trait MigrationsBase } /** SDK-reachable attribute change propagates via updateAttributeInPlace; row data preserved. */ - public function testAppwriteMigrationUpsertUpdatesAttributeInPlace(): void + public function testAppwriteMigrationOverwriteUpdatesAttributeInPlace(): void { $sourceHeaders = [ 'content-type' => 'application/json', @@ -1320,14 +1320,14 @@ trait MigrationsBase $this->assertEquals('unknown', $r['body']['default']); }, 5000, 500); - $upsertResult = $this->performMigrationSync([ + $overwriteResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); - $this->assertEquals('completed', $upsertResult['status']); + $this->assertEquals('completed', $overwriteResult['status']); $this->assertEventually(function () use ($databaseId, $tableId, $destHeaders) { $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); @@ -1401,7 +1401,7 @@ trait MigrationsBase sleep(1); // Source advances strictly later (and to a different value). Under - // Upsert this would propagate to dest; under Skip it must not. + // Overwrite this would propagate to dest; under Skip it must not. $sourcePatch = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string/name', $sourceHeaders, [ 'required' => true, 'default' => null, @@ -1437,7 +1437,7 @@ trait MigrationsBase } /** Two-way onDelete change updates in place on both sides; partner meta refreshed by hand. */ - public function testAppwriteMigrationUpsertUpdatesRelationshipOnDeleteInPlace(): void + public function testAppwriteMigrationOverwriteUpdatesRelationshipOnDeleteInPlace(): void { $sourceHeaders = [ 'content-type' => 'application/json', @@ -1525,14 +1525,14 @@ trait MigrationsBase $this->assertEquals(Database::RELATION_MUTATE_RESTRICT, $r['body']['onDelete']); }, 5000, 500); - $upsertResult = $this->performMigrationSync([ + $overwriteResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); - $this->assertEquals('completed', $upsertResult['status']); + $this->assertEquals('completed', $overwriteResult['status']); // Both sides on dest must reflect onDelete=restrict. Asserting the // partner side is the regression guard for the previously-missed @@ -1559,7 +1559,7 @@ trait MigrationsBase } /** Two-way recreate with same spec: spec-match guard tolerates parent; pair-key dedup tolerates partner. Both sides + child rows preserved. */ - public function testAppwriteMigrationUpsertTwoWayRecreateSkipsPartnerSide(): void + public function testAppwriteMigrationOverwriteTwoWayRecreateSkipsPartnerSide(): void { $sourceHeaders = [ 'content-type' => 'application/json', @@ -1670,14 +1670,14 @@ trait MigrationsBase ]); $this->assertEquals(200, $relink['headers']['status-code']); - $upsertResult = $this->performMigrationSync([ + $overwriteResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); - $this->assertEquals('completed', $upsertResult['status']); + $this->assertEquals('completed', $overwriteResult['status']); $this->assertEventually(function () use ($databaseId, $destHeaders) { $parent = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); @@ -1704,7 +1704,7 @@ trait MigrationsBase } /** One-way + onDelete change falls through to DropAndRecreate (in-place gated off for one-way). */ - public function testAppwriteMigrationUpsertOneWayRelationshipDropAndRecreate(): void + public function testAppwriteMigrationOverwriteOneWayRelationshipDropAndRecreate(): void { $sourceHeaders = [ 'content-type' => 'application/json', @@ -1781,14 +1781,14 @@ trait MigrationsBase $this->assertEquals(Database::RELATION_MUTATE_RESTRICT, $r['body']['onDelete']); }, 5000, 500); - $upsertResult = $this->performMigrationSync([ + $overwriteResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); - $this->assertEquals('completed', $upsertResult['status']); + $this->assertEquals('completed', $overwriteResult['status']); $this->assertEventually(function () use ($databaseId, $destHeaders) { $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/kids', $destHeaders); @@ -1807,7 +1807,7 @@ trait MigrationsBase } /** Recreate with non-SDK spec change (array toggle): updateAttributeInPlace bails → drop+recreate; row pass refills. */ - public function testAppwriteMigrationUpsertAttributeRecreateDropsAndRecreates(): void + public function testAppwriteMigrationOverwriteAttributeRecreateDropsAndRecreates(): void { $sourceHeaders = [ 'content-type' => 'application/json', @@ -1882,14 +1882,14 @@ trait MigrationsBase ]); $this->assertEquals(200, $relink['headers']['status-code']); - $upsertResult = $this->performMigrationSync([ + $overwriteResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); - $this->assertEquals('completed', $upsertResult['status']); + $this->assertEquals('completed', $overwriteResult['status']); $this->assertEventually(function () use ($databaseId, $tableId, $destHeaders) { $col = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/name', $destHeaders); @@ -1911,7 +1911,7 @@ trait MigrationsBase } /** Source drops+recreates with SAME spec: spec-match guard forces Tolerate; dest meta untouched. */ - public function testAppwriteMigrationUpsertSameSpecRecreateTolerates(): void + public function testAppwriteMigrationOverwriteSameSpecRecreateTolerates(): void { $sourceHeaders = [ 'content-type' => 'application/json', @@ -1984,14 +1984,14 @@ trait MigrationsBase ]); $this->assertEquals(200, $relink['headers']['status-code']); - $upsertResult = $this->performMigrationSync([ + $overwriteResult = $this->performMigrationSync([ 'resources' => $resources, 'endpoint' => $this->webEndpoint, 'projectId' => $this->getProject()['$id'], 'apiKey' => $this->getProject()['apiKey'], - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); - $this->assertEquals('completed', $upsertResult['status']); + $this->assertEquals('completed', $overwriteResult['status']); // Spec-match guard fired → dest column's $createdAt stayed at the // first-migration value. If DropAndRecreate had run, $createdAt @@ -2002,7 +2002,7 @@ trait MigrationsBase $this->assertEquals(100, $destAfter['body']['size']); $this->assertTrue($destAfter['body']['required']); - // Row pass under Upsert still propagated source's new row value. + // Row pass under Overwrite still propagated source's new row value. $rowAfter = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, $destHeaders); $this->assertEquals(200, $rowAfter['headers']['status-code']); $this->assertEquals('after-recreate', $rowAfter['body']['name']); @@ -2884,7 +2884,7 @@ trait MigrationsBase } /** - * onDuplicate=upsert on re-import: existing rows are replaced with imported values. + * onDuplicate=overwrite on re-import: existing rows are replaced with imported values. */ public function testCreateCSVImportOverwrite(): void { @@ -2905,7 +2905,7 @@ trait MigrationsBase $this->assertEquals(100, $migration['body']['statusCounters'][Resource::TYPE_ROW]['success']); }, 10_000, 500); - // Mutate one row so we can prove upsert restores it to the CSV's original value + // Mutate one row so we can prove overwrite restores it to the CSV's original value $mutate = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, [ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -2916,12 +2916,12 @@ trait MigrationsBase $this->assertEquals(200, $mutate['headers']['status-code']); $this->assertEquals(22, $mutate['body']['age']); - // Second import with onDuplicate=upsert: mutated row restored to CSV value + // Second import with onDuplicate=overwrite: mutated row restored to CSV value $second = $this->performCsvMigration([ 'fileId' => $fileId, 'bucketId' => $bucketId, 'resourceId' => $databaseId . ':' . $tableId, - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); $this->assertEventually(function () use ($second) { $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([ @@ -2931,14 +2931,14 @@ trait MigrationsBase $this->assertEquals('completed', $migration['body']['status']); }, 10_000, 500); - // Mutated row is back to CSV's original age (proving upsert actually replaced the row) + // Mutated row is back to CSV's original age (proving overwrite actually replaced the row) $row = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders())); $this->assertEquals(200, $row['headers']['status-code']); $this->assertEquals($originalName, $row['body']['name']); - $this->assertEquals($originalAge, $row['body']['age'], 'onDuplicate=upsert must restore row to imported value'); + $this->assertEquals($originalAge, $row['body']['age'], 'onDuplicate=overwrite must restore row to imported value'); // Row count still 100 (no duplicates created) $rows = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', array_merge([ @@ -3141,7 +3141,7 @@ trait MigrationsBase } /** - * onDuplicate=upsert on JSON re-import: existing rows replaced with imported values. + * onDuplicate=overwrite on JSON re-import: existing rows replaced with imported values. */ public function testCreateJSONImportOverwrite(): void { @@ -3175,7 +3175,7 @@ trait MigrationsBase 'fileId' => $fileId, 'bucketId' => $bucketId, 'resourceId' => $databaseId . ':' . $tableId, - 'onDuplicate' => 'upsert', + 'onDuplicate' => 'overwrite', ]); $this->assertEventually(function () use ($second) { $migration = $this->client->call(Client::METHOD_GET, '/migrations/' . $second['body']['$id'], array_merge([ @@ -3191,7 +3191,7 @@ trait MigrationsBase ], $this->getHeaders())); $this->assertEquals(200, $row['headers']['status-code']); $this->assertEquals($originalName, $row['body']['name']); - $this->assertEquals($originalAge, $row['body']['age'], 'onDuplicate=upsert must restore row to imported value'); + $this->assertEquals($originalAge, $row['body']['age'], 'onDuplicate=overwrite must restore row to imported value'); $rows = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', array_merge([ 'content-type' => 'application/json', From b79637eef51a7cb900491d3d6e15b2ef5e8a49fb Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 30 Apr 2026 17:01:57 +0100 Subject: [PATCH 38/40] Re-add onDuplicate param to modular migration endpoints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.9.x reorganized app/controllers/api/migrations.php into the Platform/Modules/Migrations structure but dropped the onDuplicate param. After the merge, every e2e migration test that passed 'onDuplicate' => 'overwrite' would 400 since the param wasn't in the allowlist anymore. Restoring it on the three endpoints that take row-level conflict behavior: Appwrite/Create, CSV/Imports/Create, JSON/Imports/Create. Each: - Imports OnDuplicate + WhiteList. - Adds optional ->param('onDuplicate', OnDuplicate::Fail->value, new WhiteList(OnDuplicate::values()), …). - Threads $onDuplicate through the action signature. - Stores it on the migration document's 'options' attribute so Workers/Migrations.php can pick it up via OnDuplicate::tryFrom($options['onDuplicate'] ?? '') ?? OnDuplicate::Fail. Worker code already reads options['onDuplicate'] (unchanged) — no edits needed there. --- .../Modules/Migrations/Http/Migrations/Appwrite/Create.php | 6 ++++++ .../Migrations/Http/Migrations/CSV/Imports/Create.php | 5 +++++ .../Migrations/Http/Migrations/JSON/Imports/Create.php | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Appwrite/Create.php b/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Appwrite/Create.php index 006ab3ae90..fa700877a1 100644 --- a/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Appwrite/Create.php +++ b/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Appwrite/Create.php @@ -13,6 +13,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Validator\UID; +use Utopia\Migration\Destinations\OnDuplicate; use Utopia\Migration\Sources\Appwrite as AppwriteSource; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -57,6 +58,7 @@ class Create extends Action ->param('endpoint', '', new URL(), 'Source Appwrite endpoint') ->param('projectId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Source Project ID', false, ['dbForProject']) ->param('apiKey', '', new Text(512), 'Source API Key') + ->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) ->inject('response') ->inject('dbForProject') ->inject('project') @@ -71,6 +73,7 @@ class Create extends Action string $endpoint, string $projectId, string $apiKey, + string $onDuplicate, Response $response, Database $dbForProject, Document $project, @@ -93,6 +96,9 @@ class Create extends Action 'statusCounters' => '{}', 'resourceData' => '{}', 'errors' => [], + 'options' => [ + 'onDuplicate' => $onDuplicate, + ], ])); $queueForEvents->setParam('migrationId', $migration->getId()); 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 5cc21241c3..4b47ed7d58 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 @@ -20,6 +20,7 @@ use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; +use Utopia\Migration\Destinations\OnDuplicate; use Utopia\Migration\Resource; use Utopia\Migration\Sources\Appwrite as AppwriteSource; use Utopia\Migration\Sources\CSV; @@ -29,6 +30,7 @@ use Utopia\Platform\Scope\HTTP; use Utopia\Storage\Device; use Utopia\System\System; use Utopia\Validator\Boolean; +use Utopia\Validator\WhiteList; class Create extends Action { @@ -67,6 +69,7 @@ class Create extends Action ->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('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) ->inject('response') ->inject('dbForProject') ->inject('dbForPlatform') @@ -85,6 +88,7 @@ class Create extends Action string $fileId, string $resourceId, bool $internalFile, + string $onDuplicate, Response $response, Database $dbForProject, Database $dbForPlatform, @@ -183,6 +187,7 @@ class Create extends Action 'options' => [ 'path' => $newPath, 'size' => $fileSize, + 'onDuplicate' => $onDuplicate, ], ])); 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 55081b2645..c5d936711e 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 @@ -20,6 +20,7 @@ use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; +use Utopia\Migration\Destinations\OnDuplicate; use Utopia\Migration\Resource; use Utopia\Migration\Sources\Appwrite as AppwriteSource; use Utopia\Migration\Sources\JSON as JSONSource; @@ -29,6 +30,7 @@ use Utopia\Platform\Scope\HTTP; use Utopia\Storage\Device; use Utopia\System\System; use Utopia\Validator\Boolean; +use Utopia\Validator\WhiteList; class Create extends Action { @@ -66,6 +68,7 @@ class Create extends Action ->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('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) ->inject('response') ->inject('dbForProject') ->inject('dbForPlatform') @@ -84,6 +87,7 @@ class Create extends Action string $fileId, string $resourceId, bool $internalFile, + string $onDuplicate, Response $response, Database $dbForProject, Database $dbForPlatform, @@ -183,6 +187,7 @@ class Create extends Action 'options' => [ 'path' => $newPath, 'size' => $fileSize, + 'onDuplicate' => $onDuplicate, ], ])); From 9ab546d743ed4932974111884f3fc0817b23e1b4 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 5 May 2026 09:11:18 +0100 Subject: [PATCH 39/40] composer: pin utopia-php/migration to 1.9.7 (released) --- composer.json | 2 +- composer.lock | 27 +++++++++------------------ 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index a040ce2de8..683da6f21b 100644 --- a/composer.json +++ b/composer.json @@ -74,7 +74,7 @@ "utopia-php/locale": "0.8.*", "utopia-php/logger": "0.6.*", "utopia-php/messaging": "0.22.*", - "utopia-php/migration": "dev-feat/skip-duplicates as 1.9.99", + "utopia-php/migration": "1.9.*", "utopia-php/platform": "0.13.*", "utopia-php/pools": "1.*", "utopia-php/span": "1.1.*", diff --git a/composer.lock b/composer.lock index ee77812b4b..6323a9a20a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "988e787481b0b962a21266142ec03467", + "content-hash": "4bee36b21a57e754d2b3417e72dc9599", "packages": [ { "name": "adhocore/jwt", @@ -4530,16 +4530,16 @@ }, { "name": "utopia-php/migration", - "version": "dev-feat/skip-duplicates", + "version": "1.9.7", "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "d7c3d0536dbb9e37a4894cd0b3b0134a483e04e8" + "reference": "81b608a6871f56b70496803d12010823300aab6e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/d7c3d0536dbb9e37a4894cd0b3b0134a483e04e8", - "reference": "d7c3d0536dbb9e37a4894cd0b3b0134a483e04e8", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/81b608a6871f56b70496803d12010823300aab6e", + "reference": "81b608a6871f56b70496803d12010823300aab6e", "shasum": "" }, "require": { @@ -4579,9 +4579,9 @@ ], "support": { "issues": "https://github.com/utopia-php/migration/issues", - "source": "https://github.com/utopia-php/migration/tree/feat/skip-duplicates" + "source": "https://github.com/utopia-php/migration/tree/1.9.7" }, - "time": "2026-04-30T11:08:08+00:00" + "time": "2026-05-05T07:18:48+00:00" }, { "name": "utopia-php/mongo", @@ -8442,18 +8442,9 @@ "time": "2024-11-07T12:36:22+00:00" } ], - "aliases": [ - { - "package": "utopia-php/migration", - "version": "dev-feat/skip-duplicates", - "alias": "1.9.99", - "alias_normalized": "1.9.99.0" - } - ], + "aliases": [], "minimum-stability": "dev", - "stability-flags": { - "utopia-php/migration": 20 - }, + "stability-flags": {}, "prefer-stable": true, "prefer-lowest": false, "platform": { From e63f9fd6a5784f0ebe9a063372453766dccd7161 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 6 May 2026 08:40:17 +0100 Subject: [PATCH 40/40] tests: send non-empty parent row data in TwoWayRecreate test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit server-ce 1.9.x's tablesdb POST /rows tightened input validation: the modular Documents/Create.php rejects `data => []` with a 400 "missing data" because TablesDB's Rows/Create.php inherits the strict default of getSupportForEmptyDocument() = false (only DocumentsDB overrides it to true). The test was relying on the older permissive behavior to seed an empty parent row before the relationship cascade links it. Add a non-relationship `label` string column on the parents table and populate it with `data => ['label' => 'p1']` so the POST passes the empty-data guard. The test's actual assertion target — partner-side pair-key dedup on DropAndRecreate — is unchanged. Cascade fixes: testAppwriteMigrationOverwriteAttributeRecreate and testAppwriteMigrationOverwriteSameSpecRecreate were failing in the retry pass because TwoWayRecreate's bail at L1616 left source/dest state uncleaned. Once TwoWayRecreate completes, those tests see a clean project again. Caught in CI run 25419479164 / job 74562934987 on the MongoDB (dedicated) Migrations matrix. --- .../e2e/Services/Migrations/MigrationsBase.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index fa360bfce8..387f4fe0e1 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -1593,6 +1593,22 @@ trait MigrationsBase $this->assertEquals(201, $createTable['headers']['status-code']); } + // Add a non-relationship column on parents so we can POST a row with + // non-empty data. tablesdb POST /rows rejects empty data arrays in + // 1.9.x (Create.php:161 — getSupportForEmptyDocument() defaults false). + $createLabel = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/parents/columns/string', $sourceHeaders, [ + 'key' => 'label', + 'size' => 32, + 'required' => false, + ]); + $this->assertEquals(202, $createLabel['headers']['status-code']); + + $this->assertEventually(function () use ($databaseId, $sourceHeaders) { + $r = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/parents/columns/label', $sourceHeaders); + $this->assertEquals(200, $r['headers']['status-code']); + $this->assertEquals('available', $r['body']['status']); + }, 10000, 500); + $createRel = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/parents/columns/relationship', $sourceHeaders, [ 'relatedTableId' => 'children', 'type' => Database::RELATION_ONE_TO_MANY, @@ -1611,7 +1627,7 @@ trait MigrationsBase $parentRow = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/parents/rows', $sourceHeaders, [ 'rowId' => 'parent-1', - 'data' => [], + 'data' => ['label' => 'p1'], ]); $this->assertEquals(201, $parentRow['headers']['status-code']); $childRow = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/children/rows', $sourceHeaders, [