Skip to content

Commit e629078

Browse files
Merge branch 'feat-database-and-namespace' of https://github.com/utopia-php/database into feat-database-and-namespace
2 parents 5fe9aca + 414178d commit e629078

8 files changed

Lines changed: 123 additions & 5 deletions

File tree

src/Database/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ abstract public function getSupportForCasting(): bool;
426426
*/
427427
public function filter(string $value):string
428428
{
429-
$value = preg_replace("/[^A-Za-z0-9]_/", '', $value);
429+
$value = preg_replace("/[^A-Za-z0-9\_\-\.]/", '', $value);
430430

431431
if(\is_null($value)) {
432432
throw new Exception('Failed to filter key');

src/Database/Adapter/MariaDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function createCollection(string $name, array $attributes = [], array $in
115115

116116
if (!empty($attributes) || !empty($indexes)) {
117117
foreach ($attributes as &$attribute) {
118-
$attrId = $attribute->getId();
118+
$attrId = $this->filter($attribute->getId());
119119
$attrType = $this->getSQLType($attribute->getAttribute('type'), $attribute->getAttribute('size', 0), $attribute->getAttribute('signed', true));
120120

121121
if($attribute->getAttribute('array')) {

src/Database/Database.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ public function getDefaultDatabase(): string
241241
public function create(string $name): bool
242242
{
243243
$this->adapter->create($name);
244+
245+
// Temporarily change defaultDatabase to create metadata collection
246+
$defaultDatabase = $this->getDefaultDatabase();
247+
244248
$this->setDefaultDatabase($name);
245249

246250
/**
@@ -261,7 +265,7 @@ public function create(string $name): bool
261265
]);
262266

263267
$this->createCollection(self::METADATA, $attributes);
264-
$this->setDefaultDatabase('', reset: true);
268+
$this->setDefaultDatabase($defaultDatabase);
265269

266270
return true;
267271
}

src/Database/Validator/Key.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public function isValid($value): bool
4444
return false;
4545
}
4646

47-
// Valid chars: A-Z, a-z, 0-9, underscore, period, hyphen, space
48-
if (\preg_match('/[^A-Za-z0-9\_\.\-]/', $value)) {
47+
// Valid chars: A-Z, a-z, 0-9, underscore, hyphen, period
48+
if (\preg_match('/[^A-Za-z0-9\_\-\.]/', $value)) {
4949
return false;
5050
}
5151

tests/Database/Adapter/MariaDBTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static function getDatabase(): Database
6767
$cache = new Cache(new RedisAdapter($redis));
6868

6969
$database = new Database(new MariaDB($pdo), $cache);
70+
$database->setDefaultDatabase('utopiaTests');
7071
$database->setNamespace('myapp_'.uniqid());
7172

7273
return self::$database = $database;

tests/Database/Adapter/MongoDBTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static function getDatabase(): Database
6363
$cache = new Cache(new RedisAdapter($redis));
6464

6565
$database = new Database(new MongoDB($client), $cache);
66+
$database->setDefaultDatabase('utopiaTests');
6667
$database->setNamespace('myapp_'.uniqid());
6768

6869
return self::$database = $database;

tests/Database/Adapter/MySQLTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static function getDatabase(): Database
7878
$cache = new Cache(new RedisAdapter($redis));
7979

8080
$database = new Database(new MySQL($pdo), $cache);
81+
$database->setDefaultDatabase('utopiaTests');
8182
$database->setNamespace('myapp_'.uniqid());
8283

8384
return self::$database = $database;

tests/Database/Base.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,117 @@ public function testCreateCollectionWithSchema()
342342
static::getDatabase()->deleteCollection('withSchema');
343343
}
344344

345+
public function testCreateCollectionValidator()
346+
{
347+
$collections = [
348+
"validatorTest",
349+
"validator-test",
350+
"validator_test",
351+
"validator.test",
352+
];
353+
354+
$attributes = [
355+
new Document([
356+
'$id' => 'attribute1',
357+
'type' => Database::VAR_STRING,
358+
'size' => 256,
359+
'required' => false,
360+
'signed' => true,
361+
'array' => false,
362+
'filters' => [],
363+
]),
364+
new Document([
365+
'$id' => 'attribute-2',
366+
'type' => Database::VAR_INTEGER,
367+
'size' => 0,
368+
'required' => false,
369+
'signed' => true,
370+
'array' => false,
371+
'filters' => [],
372+
]),
373+
new Document([
374+
'$id' => 'attribute_3',
375+
'type' => Database::VAR_BOOLEAN,
376+
'size' => 0,
377+
'required' => false,
378+
'signed' => true,
379+
'array' => false,
380+
'filters' => [],
381+
]),
382+
new Document([
383+
'$id' => 'attribute.4',
384+
'type' => Database::VAR_BOOLEAN,
385+
'size' => 0,
386+
'required' => false,
387+
'signed' => true,
388+
'array' => false,
389+
'filters' => [],
390+
]),
391+
];
392+
393+
$indexes = [
394+
new Document([
395+
'$id' => 'index1',
396+
'type' => Database::INDEX_KEY,
397+
'attributes' => ['attribute1'],
398+
'lengths' => [256],
399+
'orders' => ['ASC'],
400+
]),
401+
new Document([
402+
'$id' => 'index-2',
403+
'type' => Database::INDEX_KEY,
404+
'attributes' => ['attribute-2'],
405+
'lengths' => [],
406+
'orders' => ['ASC'],
407+
]),
408+
new Document([
409+
'$id' => 'index_3',
410+
'type' => Database::INDEX_KEY,
411+
'attributes' => ['attribute_3'],
412+
'lengths' => [],
413+
'orders' => ['ASC'],
414+
]),
415+
new Document([
416+
'$id' => 'index.4',
417+
'type' => Database::INDEX_KEY,
418+
'attributes' => ['attribute.4'],
419+
'lengths' => [],
420+
'orders' => ['ASC'],
421+
]),
422+
];
423+
424+
foreach ($collections as $id) {
425+
$collection = static::getDatabase()->createCollection($id, $attributes, $indexes);
426+
427+
$this->assertEquals(false, $collection->isEmpty());
428+
$this->assertEquals($id, $collection->getId());
429+
430+
$this->assertIsArray($collection->getAttribute('attributes'));
431+
$this->assertCount(4, $collection->getAttribute('attributes'));
432+
$this->assertEquals('attribute1', $collection->getAttribute('attributes')[0]['$id']);
433+
$this->assertEquals(Database::VAR_STRING, $collection->getAttribute('attributes')[0]['type']);
434+
$this->assertEquals('attribute-2', $collection->getAttribute('attributes')[1]['$id']);
435+
$this->assertEquals(Database::VAR_INTEGER, $collection->getAttribute('attributes')[1]['type']);
436+
$this->assertEquals('attribute_3', $collection->getAttribute('attributes')[2]['$id']);
437+
$this->assertEquals(Database::VAR_BOOLEAN, $collection->getAttribute('attributes')[2]['type']);
438+
$this->assertEquals('attribute.4', $collection->getAttribute('attributes')[3]['$id']);
439+
$this->assertEquals(Database::VAR_BOOLEAN, $collection->getAttribute('attributes')[3]['type']);
440+
441+
$this->assertIsArray($collection->getAttribute('indexes'));
442+
$this->assertCount(4, $collection->getAttribute('indexes'));
443+
$this->assertEquals('index1', $collection->getAttribute('indexes')[0]['$id']);
444+
$this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[0]['type']);
445+
$this->assertEquals('index-2', $collection->getAttribute('indexes')[1]['$id']);
446+
$this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[1]['type']);
447+
$this->assertEquals('index_3', $collection->getAttribute('indexes')[2]['$id']);
448+
$this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[2]['type']);
449+
$this->assertEquals('index.4', $collection->getAttribute('indexes')[3]['$id']);
450+
$this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[3]['type']);
451+
452+
static::getDatabase()->deleteCollection($id);
453+
}
454+
}
455+
345456
public function testCreateDocument()
346457
{
347458
static::getDatabase()->createCollection('documents');

0 commit comments

Comments
 (0)