Skip to content

Commit 60e97f7

Browse files
committed
test hostname
1 parent 6d3483a commit 60e97f7

4 files changed

Lines changed: 73 additions & 20 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"utopia-php/console": "0.1.*",
4242
"utopia-php/cache": "1.*",
4343
"utopia-php/pools": "1.*",
44-
"utopia-php/mongo": "dev-get-host as 1.0.3"
44+
"utopia-php/mongo": "1.*"
4545
},
4646
"require-dev": {
4747
"fakerphp/faker": "1.23.*",

composer.lock

Lines changed: 9 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Database/Database.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9343,11 +9343,17 @@ public function getCacheKeys(string $collectionId, ?string $documentId = null, a
93439343
{
93449344
if ($this->adapter->getSupportForHostname()) {
93459345
$hostname = $this->adapter->getHostname();
9346+
$parts = parse_url($hostname);
9347+
$hostname = $parts['host'] ?? $hostname;
93469348
}
93479349

93489350
$tenantSegment = $this->adapter->getTenant();
93499351

9350-
if ($collectionId === self::METADATA && isset($this->globalCollections[$documentId])) {
9352+
if (
9353+
$collectionId === self::METADATA &&
9354+
$this->adapter->getSharedTables() &&
9355+
isset($this->globalCollections[$documentId])
9356+
) {
93519357
$tenantSegment = null;
93529358
}
93539359

tests/unit/CacheKeyTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,60 @@ public function testFiltersDisabledEntirelyProducesDifferentCacheKey(): void
130130

131131
$this->assertNotEquals($hashEnabled, $hashDisabled);
132132
}
133+
134+
public function testParseHostname(): void
135+
{
136+
$hostname = 'appwrite://database_db_nyc3_self_hosted_0_0?database=appwrite&namespace=_1';
137+
138+
$adapter = $this->createMock(Adapter::class);
139+
$adapter->method('getSupportForHostname')->willReturn(true);
140+
$adapter->method('getHostname')->willReturn($hostname);
141+
$adapter->method('getTenant')->willReturn(999);
142+
$adapter->method('getSharedTables')->willReturn(true);
143+
$adapter->method('getNamespace')->willReturn('_ns');
144+
145+
$db = new Database($adapter, new Cache(new None()), []);
146+
147+
/**
148+
* Check DSN is parsed correctly
149+
*/
150+
[$collectionKey, $documentKey] = $db->getCacheKeys('users');
151+
$this->assertEquals('default-cache-database_db_nyc3_self_hosted_0_0:_ns:999:collection:users', $collectionKey);
152+
$this->assertEquals('', $documentKey);
153+
154+
$db->setGlobalCollections(['users']);
155+
$this->assertEquals(['users'], $db->getGlobalCollections());
156+
157+
/**
158+
* Check that tenant 999 exists
159+
*/
160+
161+
[$collectionKey, $documentKey] = $db->getCacheKeys(Database::METADATA, 'audit');
162+
$this->assertEquals('default-cache-database_db_nyc3_self_hosted_0_0:_ns:999:collection:_metadata', $collectionKey);
163+
$this->assertEquals('default-cache-database_db_nyc3_self_hosted_0_0:_ns:999:collection:_metadata:audit', $documentKey);
164+
165+
/**
166+
* Check that tenant 999 was removed
167+
*/
168+
[$collectionKey, $documentKey] = $db->getCacheKeys(Database::METADATA, 'users');
169+
$this->assertEquals('default-cache-database_db_nyc3_self_hosted_0_0:_ns::collection:_metadata', $collectionKey);
170+
$this->assertEquals('default-cache-database_db_nyc3_self_hosted_0_0:_ns::collection:_metadata:users', $documentKey);
171+
}
172+
173+
public function testSimpleHostname(): void
174+
{
175+
$hostname = 'database_db_nyc3_self_hosted_0_0';
176+
177+
$adapter = $this->createMock(Adapter::class);
178+
$adapter->method('getSupportForHostname')->willReturn(true);
179+
$adapter->method('getHostname')->willReturn($hostname);
180+
$adapter->method('getTenant')->willReturn(999);
181+
$adapter->method('getSharedTables')->willReturn(true);
182+
$adapter->method('getNamespace')->willReturn('_ns');
183+
184+
$db = new Database($adapter, new Cache(new None()), []);
185+
186+
[$collectionKey] = $db->getCacheKeys('animals');
187+
$this->assertEquals('default-cache-database_db_nyc3_self_hosted_0_0:_ns:999:collection:animals', $collectionKey);
188+
}
133189
}

0 commit comments

Comments
 (0)