Skip to content

Commit ff4c8af

Browse files
added mongodb suite for schemaless
1 parent 4240449 commit ff4c8af

File tree

8 files changed

+141
-4
lines changed

8 files changed

+141
-4
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ jobs:
8484
SharedTables/MySQL,
8585
SharedTables/Postgres,
8686
SharedTables/SQLite,
87+
Schemaless/MongoDB,
8788
]
8889

8990
steps:

src/Database/Adapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,4 +1383,12 @@ abstract public function getSupportForUTCCasting(): bool;
13831383
*/
13841384
abstract public function setUTCDatetime(string $value): mixed;
13851385

1386+
/**
1387+
* Set support for attributes
1388+
*
1389+
* @param bool $support
1390+
* @return bool
1391+
*/
1392+
abstract public function setSupportForAttributes(bool $support): bool;
1393+
13861394
}

src/Database/Adapter/Mongo.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Mongo extends Adapter
5757
*/
5858
private ?array $session = null; // Store session array from startSession
5959
protected int $inTransaction = 0;
60+
protected bool $supportForAttributes = true;
6061

6162
/**
6263
* Constructor.
@@ -2585,7 +2586,13 @@ public function setUTCDatetime(string $value): mixed
25852586
*/
25862587
public function getSupportForAttributes(): bool
25872588
{
2588-
return false;
2589+
return $this->supportForAttributes;
2590+
}
2591+
2592+
public function setSupportForAttributes(bool $support): bool
2593+
{
2594+
$this->supportForAttributes = $support;
2595+
return $this->supportForAttributes;
25892596
}
25902597

25912598
/**

src/Database/Adapter/Pool.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,4 +599,9 @@ public function setUTCDatetime(string $value): mixed
599599
{
600600
return $this->delegate(__FUNCTION__, \func_get_args());
601601
}
602+
603+
public function setSupportForAttributes(bool $support): bool
604+
{
605+
return $this->delegate(__FUNCTION__, \func_get_args());
606+
}
602607
}

src/Database/Adapter/SQL.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,4 +2930,9 @@ public function decodePolygon(string $wkb): array
29302930

29312931
return $rings;
29322932
}
2933+
2934+
public function setSupportForAttributes(bool $support): bool
2935+
{
2936+
return true;
2937+
}
29332938
}

tests/e2e/Adapter/MongoDBTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static function getDatabase(): Database
5151
);
5252

5353
$database = new Database(new Mongo($client), $cache);
54+
$database->getAdapter()->setSupportForAttributes(true);
5455
$database
5556
->setDatabase($schema)
5657
->setNamespace(static::$namespace = 'myapp_' . uniqid());
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Tests\E2E\Adapter;
4+
5+
use Exception;
6+
use Redis;
7+
use Utopia\Cache\Adapter\Redis as RedisAdapter;
8+
use Utopia\Cache\Cache;
9+
use Utopia\Database\Adapter\Mongo;
10+
use Utopia\Database\Database;
11+
use Utopia\Mongo\Client;
12+
13+
class MongoDBTest extends Base
14+
{
15+
public static ?Database $database = null;
16+
protected static string $namespace;
17+
18+
/**
19+
* Return name of adapter
20+
*
21+
* @return string
22+
*/
23+
public static function getAdapterName(): string
24+
{
25+
return "mongodb";
26+
}
27+
28+
/**
29+
* @return Database
30+
* @throws Exception
31+
*/
32+
public static function getDatabase(): Database
33+
{
34+
if (!is_null(self::$database)) {
35+
return self::$database;
36+
}
37+
38+
$redis = new Redis();
39+
$redis->connect('redis', 6379);
40+
$redis->flushAll();
41+
$cache = new Cache(new RedisAdapter($redis));
42+
43+
$schema = 'utopiaTests'; // same as $this->testDatabase
44+
$client = new Client(
45+
$schema,
46+
'mongo',
47+
27017,
48+
'root',
49+
'password',
50+
false
51+
);
52+
53+
$database = new Database(new Mongo($client), $cache);
54+
$database->getAdapter()->setSupportForAttributes(false);
55+
$database
56+
->setDatabase($schema)
57+
->setNamespace(static::$namespace = 'myapp_' . uniqid());
58+
59+
if ($database->exists()) {
60+
$database->delete();
61+
}
62+
63+
64+
$database->create();
65+
66+
return self::$database = $database;
67+
}
68+
69+
/**
70+
* @throws Exception
71+
*/
72+
public function testCreateExistsDelete(): void
73+
{
74+
// Mongo creates databases on the fly, so exists would always pass. So we override this test to remove the exists check.
75+
$this->assertNotNull(static::getDatabase()->create());
76+
$this->assertEquals(true, static::getDatabase()->delete($this->testDatabase));
77+
$this->assertEquals(true, static::getDatabase()->create());
78+
$this->assertEquals(static::getDatabase(), static::getDatabase()->setDatabase($this->testDatabase));
79+
}
80+
81+
public function testRenameAttribute(): void
82+
{
83+
$this->assertTrue(true);
84+
}
85+
86+
public function testRenameAttributeExisting(): void
87+
{
88+
$this->assertTrue(true);
89+
}
90+
91+
public function testUpdateAttributeStructure(): void
92+
{
93+
$this->assertTrue(true);
94+
}
95+
96+
public function testKeywords(): void
97+
{
98+
$this->assertTrue(true);
99+
}
100+
101+
protected static function deleteColumn(string $collection, string $column): bool
102+
{
103+
return true;
104+
}
105+
106+
protected static function deleteIndex(string $collection, string $index): bool
107+
{
108+
return true;
109+
}
110+
}

tests/e2e/Adapter/Scopes/IndexTests.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ public function testIndexValidation(): void
269269

270270
$this->assertFalse($validator->isValid($newIndex));
271271

272-
if ($database->getAdapter()->getSupportForAttributes()) {
273-
$this->assertEquals('Attribute "integer" cannot be part of a fulltext index, must be of type string', $validator->getDescription());
274-
} elseif (!$database->getAdapter()->getSupportForMultipleFulltextIndexes()) {
272+
if (!$database->getAdapter()->getSupportForMultipleFulltextIndexes()) {
275273
$this->assertEquals('There is already a fulltext index in the collection', $validator->getDescription());
274+
} elseif ($database->getAdapter()->getSupportForAttributes()) {
275+
$this->assertEquals('Attribute "integer" cannot be part of a fulltext index, must be of type string', $validator->getDescription());
276276
}
277277

278278
try {

0 commit comments

Comments
 (0)