mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
fix(destination): stop propagating source DSN to destination database documents
Restore an opt-in resolver for `_databases.database` on the Appwrite destination so cross-instance migrations don't write the source's DSN into the destination project's metadata. Without a resolver, the field is left blank and the runtime falls back to the destination project's DSN — correct for legacy single-DSN projects. Why: PR #151 removed the `getDatabaseDSN` callable on the assumption it was unused, and replaced it with `\$resource->getDatabase()` (the source DSN). On Cloud this routed destination reads to the source's host with the destination's project sequence as the namespace, producing `Table 'appwrite._<tenant>__metadata' doesn't exist` for every migrated database. Multi-type setups (documentsdb / vectorsdb) were silently broken the same way. Refs PR #151.
This commit is contained in:
2 files changed
+126
-3
No files matched your search
@@ -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<UtopiaDocument>
|
||||
*/
|
||||
@@ -139,6 +150,7 @@ class Appwrite extends Destination
|
||||
* @param callable(UtopiaDocument $database):UtopiaDatabase $getDatabasesDB
|
||||
* @param array<array<string, mixed>> $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());
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Tests\Unit\Destinations;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionClass;
|
||||
use Utopia\Database\Database as UtopiaDatabase;
|
||||
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 (): 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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user