diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index c47b9cdc22..fa464e1104 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -103,6 +103,17 @@ class Appwrite extends Destination */ protected $getDatabasesDB; + /** + * Resolves the DSN written into the destination's `_databases.database` for + * a migrated database. When unset, the attribute is left blank and the + * runtime falls back to the destination project's DSN — correct for legacy + * single-DSN projects, but cross-instance / multi-type setups must inject + * a resolver so source DSNs don't bleed into the destination metadata. + * + * @var (callable(Database $resource): string)|null + */ + protected $getDatabaseDSN; + /** * @var array */ @@ -139,6 +150,7 @@ class Appwrite extends Destination * @param callable(UtopiaDocument $database):UtopiaDatabase $getDatabasesDB * @param array> $collectionStructure * @param OnDuplicate $onDuplicate Behavior when a row with an existing $id is encountered. + * @param (callable(Database $resource): string)|null $getDatabaseDSN Resolver for the destination's `_databases.database` value. Required for cross-instance migrations to prevent the source DSN from being written into the destination project's metadata. */ public function __construct( string $project, @@ -148,6 +160,7 @@ class Appwrite extends Destination callable $getDatabasesDB, protected array $collectionStructure, protected OnDuplicate $onDuplicate = OnDuplicate::Fail, + ?callable $getDatabaseDSN = null, ) { $this->project = $project; $this->endpoint = $endpoint; @@ -166,6 +179,21 @@ class Appwrite extends Destination $this->users = new Users($this->client); $this->getDatabasesDB = $getDatabasesDB; + $this->getDatabaseDSN = $getDatabaseDSN; + } + + /** + * Resolve the DSN written into the destination's `_databases.database`. + * Without a resolver, leave it blank — the source DSN must never be + * propagated, since cross-instance source/destination DSNs differ and + * propagation routes destination reads to the wrong host (see PR #151). + */ + private function resolveDestinationDsn(Database $resource): string + { + if ($this->getDatabaseDSN === null) { + return ''; + } + return ($this->getDatabaseDSN)($resource); } /** Orphan cleanup runs only after a successful migration — a mid-run throw preserves the destination as-is. */ @@ -519,7 +547,7 @@ class Appwrite extends Destination 'enabled' => $resource->getEnabled(), 'type' => empty($resource->getType()) ? 'legacy' : $resource->getType(), 'originalId' => empty($resource->getOriginalId()) ? null : $resource->getOriginalId(), - 'database' => $resource->getDatabase(), + 'database' => $this->resolveDestinationDsn($resource), '$updatedAt' => $updatedAt, ])); $resource->setSequence($existing->getSequence()); @@ -541,8 +569,8 @@ class Appwrite extends Destination '$updatedAt' => $updatedAt, 'originalId' => empty($resource->getOriginalId()) ? null : $resource->getOriginalId(), 'type' => empty($resource->getType()) ? 'legacy' : $resource->getType(), - // source and destination can be in different location - 'database' => $resource->getDatabase() + // Source and destination can be in different locations; never write the source DSN here. + 'database' => $this->resolveDestinationDsn($resource), ])); $resource->setSequence($database->getSequence()); diff --git a/tests/Migration/Unit/Destinations/AppwriteDestinationDsnTest.php b/tests/Migration/Unit/Destinations/AppwriteDestinationDsnTest.php new file mode 100644 index 0000000000..892868db96 --- /dev/null +++ b/tests/Migration/Unit/Destinations/AppwriteDestinationDsnTest.php @@ -0,0 +1,95 @@ +__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 (): 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'); + /** @var string $value */ + $value = $method->invoke($destination, $resource); + return $value; + } +}