-
Notifications
You must be signed in to change notification settings - Fork 3
Fix duplicate document error during database migration #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
claudear
wants to merge
1
commit into
main
Choose a base branch
from
fix/handle-duplicate-document-exception
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Tests\Unit\Destinations; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Utopia\Database\Database as UtopiaDatabase; | ||
| use Utopia\Database\Document as UtopiaDocument; | ||
| use Utopia\Database\Exception\Duplicate as DuplicateException; | ||
| use Utopia\Migration\Cache; | ||
| use Utopia\Migration\Destinations\Appwrite; | ||
| use Utopia\Migration\Resources\Database\Database; | ||
| use Utopia\Migration\Resources\Database\Row; | ||
| use Utopia\Migration\Resources\Database\Table; | ||
|
|
||
| class AppwriteTest extends TestCase | ||
| { | ||
| private function createAppwriteDestination( | ||
| UtopiaDatabase $dbForProject, | ||
| UtopiaDatabase $dbForDatabases, | ||
| ): Appwrite { | ||
| $getDatabasesDB = function () use ($dbForDatabases) { | ||
| return $dbForDatabases; | ||
| }; | ||
|
|
||
| $appwrite = new Appwrite( | ||
| 'test-project', | ||
| 'https://localhost', | ||
| 'test-key', | ||
| $dbForProject, | ||
| $getDatabasesDB, | ||
| [] | ||
| ); | ||
|
|
||
| $cache = new Cache(); | ||
| $appwrite->registerCache($cache); | ||
|
|
||
| return $appwrite; | ||
| } | ||
|
|
||
| private function createMockDatabases(): array | ||
| { | ||
| $dbForProject = $this->createMock(UtopiaDatabase::class); | ||
|
|
||
| $dbDoc = new UtopiaDocument([ | ||
| '$id' => 'db1', | ||
| '$sequence' => '1', | ||
| ]); | ||
| $tableDoc = new UtopiaDocument([ | ||
| '$id' => 'table1', | ||
| '$sequence' => '2', | ||
| 'attributes' => [], | ||
| ]); | ||
|
|
||
| $dbForProject->method('getDocument') | ||
| ->willReturnCallback(function (string $collection) use ($dbDoc, $tableDoc) { | ||
| if ($collection === 'databases') { | ||
| return $dbDoc; | ||
| } | ||
| return $tableDoc; | ||
| }); | ||
|
|
||
| $dbForDatabases = $this->createMock(UtopiaDatabase::class); | ||
|
|
||
| $adapter = $this->createMock(\Utopia\Database\Adapter::class); | ||
| $adapter->method('getSupportForAttributes')->willReturn(false); | ||
| $dbForDatabases->method('getAdapter')->willReturn($adapter); | ||
|
|
||
| $dbForDatabases->method('skipRelationshipsExistCheck') | ||
| ->willReturnCallback(function (callable $callback) { | ||
| return $callback(); | ||
| }); | ||
|
|
||
| return [$dbForProject, $dbForDatabases]; | ||
| } | ||
|
|
||
| /** | ||
| * Test that createRecord handles DuplicateException from batch createDocuments | ||
| * by falling back to one-by-one insertion and skipping duplicates. | ||
| * | ||
| * This reproduces the "Document already exists" error from Sentry (CLOUD-3JMT). | ||
| */ | ||
| public function testCreateRecordHandlesDuplicateDocuments(): void | ||
| { | ||
| [$dbForProject, $dbForDatabases] = $this->createMockDatabases(); | ||
|
|
||
| // Batch createDocuments throws DuplicateException | ||
| $dbForDatabases->method('createDocuments') | ||
| ->willThrowException(new DuplicateException('Document already exists')); | ||
|
|
||
| // Fallback createDocument: first succeeds, second throws duplicate (skipped) | ||
| $createDocumentCallCount = 0; | ||
| $dbForDatabases->method('createDocument') | ||
| ->willReturnCallback(function (string $collection, UtopiaDocument $doc) use (&$createDocumentCallCount) { | ||
| $createDocumentCallCount++; | ||
| if ($createDocumentCallCount === 2) { | ||
| throw new DuplicateException('Document already exists'); | ||
| } | ||
| return $doc; | ||
| }); | ||
|
|
||
| $appwrite = $this->createAppwriteDestination($dbForProject, $dbForDatabases); | ||
|
|
||
| $database = new Database('db1', 'Test DB'); | ||
| $table = new Table($database, 'Test Table', 'table1'); | ||
|
|
||
| $method = new \ReflectionMethod(Appwrite::class, 'createRecord'); | ||
|
|
||
| $row1 = new Row('row1', $table, ['field1' => 'value1']); | ||
| $row2 = new Row('row2', $table, ['field1' => 'value2']); | ||
|
|
||
| // Buffer row1 (not last) | ||
| $result1 = $method->invoke($appwrite, $row1, false); | ||
| $this->assertTrue($result1); | ||
|
|
||
| // Buffer row2 and flush (isLast=true) - should NOT throw | ||
| $result2 = $method->invoke($appwrite, $row2, true); | ||
| $this->assertTrue($result2); | ||
|
|
||
| // Verify fallback was used | ||
| $this->assertEquals(2, $createDocumentCallCount); | ||
| } | ||
|
|
||
| /** | ||
| * Test that when batch createDocuments succeeds, no fallback is needed. | ||
| */ | ||
| public function testCreateRecordBatchSucceeds(): void | ||
| { | ||
| [$dbForProject, $dbForDatabases] = $this->createMockDatabases(); | ||
|
|
||
| // Batch insert succeeds | ||
| $dbForDatabases->method('createDocuments') | ||
| ->willReturn(0); | ||
|
|
||
| // createDocument (singular) should NOT be called | ||
| $dbForDatabases->expects($this->never())->method('createDocument'); | ||
|
|
||
| $appwrite = $this->createAppwriteDestination($dbForProject, $dbForDatabases); | ||
|
|
||
| $database = new Database('db1', 'Test DB'); | ||
| $table = new Table($database, 'Test Table', 'table1'); | ||
|
|
||
| $method = new \ReflectionMethod(Appwrite::class, 'createRecord'); | ||
|
|
||
| $row = new Row('row1', $table, ['field1' => 'value1']); | ||
| $result = $method->invoke($appwrite, $row, true); | ||
|
|
||
| $this->assertTrue($result); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fallback duplicates are reported as success, not skipped.
This loop only has buffered
UtopiaDocuments, so when a duplicate is skipped here there is no way to update the matchingRowresource back toSTATUS_SKIPPEDlike the explicit duplicate branch at Lines 989-994. In practice, the migration cache/result will report these rows as successfully imported even though they were only ignored as pre-existing.💡 Sketch of a fix
🤖 Prompt for AI Agents