-
Notifications
You must be signed in to change notification settings - Fork 4
fix(destination): stop propagating source DSN to destination database documents #180
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
Merged
+131
−4
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ff3b444
fix(destination): stop propagating source DSN to destination database…
abnegate 8a0ebdb
docs: clarify resolver scope — when source/destination DSNs differ
abnegate 2eefb59
fix(test): add UtopiaDocument parameter to getDatabasesDB closure sig…
Copilot 9c9df8f
fix(destination): use resolveDestinationDsn in databaseSpecMatches in…
Copilot 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
Some comments aren't visible on the classic Files Changed page.
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
96 changes: 96 additions & 0 deletions
96
tests/Migration/Unit/Destinations/AppwriteDestinationDsnTest.php
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,96 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Tests\Unit\Destinations; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use ReflectionClass; | ||
| use Utopia\Database\Database as UtopiaDatabase; | ||
| use Utopia\Database\Document as UtopiaDocument; | ||
| use Utopia\Migration\Destinations\Appwrite as AppwriteDestination; | ||
| use Utopia\Migration\Destinations\OnDuplicate; | ||
| use Utopia\Migration\Resources\Database\Database as DatabaseResource; | ||
|
|
||
| /** | ||
| * Regression for PR #151: the destination must never write the source's DSN | ||
| * into `_databases.database`. With no resolver, the value must be blank so | ||
| * the runtime falls back to the destination project's DSN. With a resolver, | ||
| * the resolver's value must be written and the source's value ignored. | ||
| * | ||
| * Reproduces the comuneo-pre-production incident where post-migration | ||
| * `_databases.database` rows pointed at the source's host (db11) and | ||
| * destination reads hit `Table 'appwrite._<tenant>__metadata' doesn't exist`. | ||
| */ | ||
| class AppwriteDestinationDsnTest extends TestCase | ||
| { | ||
| public function testWithoutResolverReturnsEmptyString(): void | ||
| { | ||
| $destination = $this->makeDestination(getDatabaseDSN: null); | ||
| $resource = $this->makeResource(sourceDsn: 'database_db_fra1_self_hosted_11_0'); | ||
|
|
||
| $resolved = $this->invokeResolver($destination, $resource); | ||
|
|
||
| $this->assertSame('', $resolved, 'Without a resolver the destination must not propagate the source DSN.'); | ||
| } | ||
|
|
||
| public function testWithResolverUsesItsReturnValue(): void | ||
| { | ||
| $expected = 'appwrite://database_db_fra1_self_hosted_17_0?database=appwrite&namespace=_1'; | ||
| $destination = $this->makeDestination( | ||
| getDatabaseDSN: fn (DatabaseResource $r): string => $expected, | ||
| ); | ||
| $resource = $this->makeResource(sourceDsn: 'database_db_fra1_self_hosted_11_0'); | ||
|
|
||
| $resolved = $this->invokeResolver($destination, $resource); | ||
|
|
||
| $this->assertSame($expected, $resolved); | ||
| $this->assertNotSame($resource->getDatabase(), $resolved, 'Source DSN must not leak through the resolver path.'); | ||
| } | ||
|
|
||
| public function testResolverReceivesTheResource(): void | ||
| { | ||
| $captured = null; | ||
| $destination = $this->makeDestination( | ||
| getDatabaseDSN: function (DatabaseResource $r) use (&$captured): string { | ||
| $captured = $r; | ||
| return 'resolved'; | ||
| }, | ||
| ); | ||
| $resource = $this->makeResource(sourceDsn: 'src'); | ||
|
|
||
| $this->invokeResolver($destination, $resource); | ||
|
|
||
| $this->assertSame($resource, $captured); | ||
| } | ||
|
|
||
| private function makeDestination(?callable $getDatabaseDSN): AppwriteDestination | ||
| { | ||
| return new AppwriteDestination( | ||
| project: 'destination-project', | ||
| endpoint: 'http://example.test/v1', | ||
| key: 'test-key', | ||
| dbForProject: $this->createStub(UtopiaDatabase::class), | ||
| getDatabasesDB: fn (UtopiaDocument $database): UtopiaDatabase => $this->createStub(UtopiaDatabase::class), | ||
| collectionStructure: ['attributes' => [], 'indexes' => []], | ||
| onDuplicate: OnDuplicate::Fail, | ||
| getDatabaseDSN: $getDatabaseDSN, | ||
| ); | ||
| } | ||
|
|
||
| private function makeResource(string $sourceDsn): DatabaseResource | ||
| { | ||
| return new DatabaseResource( | ||
| id: 'src-database', | ||
| name: 'src', | ||
| type: 'legacy', | ||
| database: $sourceDsn, | ||
| ); | ||
| } | ||
|
|
||
| private function invokeResolver(AppwriteDestination $destination, DatabaseResource $resource): string | ||
| { | ||
| $method = (new ReflectionClass(AppwriteDestination::class))->getMethod('resolveDestinationDsn'); | ||
|
abnegate marked this conversation as resolved.
|
||
| /** @var string $value */ | ||
| $value = $method->invoke($destination, $resource); | ||
| return $value; | ||
| } | ||
|
abnegate marked this conversation as resolved.
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.