Skip to content

Commit 641980d

Browse files
authored
Merge pull request #672 from utopia-php/feat-mongo-tmp-with-uuid7
Feat mongo tmp with uuid7
2 parents 2ac6b4b + 0cbd6bb commit 641980d

7 files changed

Lines changed: 60 additions & 51 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"utopia-php/framework": "0.33.*",
4040
"utopia-php/cache": "0.13.*",
4141
"utopia-php/pools": "0.8.*",
42-
"utopia-php/mongo": "dev-feat-partial-filter-expression as 0.5.3"
42+
"utopia-php/mongo": "dev-feat-create-UUID as 0.5.3"
4343
},
4444
"require-dev": {
4545
"fakerphp/faker": "1.23.*",

composer.lock

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

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ services:
1717
- ./dev/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
1818
- /var/run/docker.sock:/var/run/docker.sock
1919
- ./docker-compose.yml:/usr/src/code/docker-compose.yml
20-
#- ./vendor/utopia-php/mongo/src/Client.php:/usr/src/code/vendor/utopia-php/mongo/src/Client.php
20+
- ./vendor/utopia-php/mongo/src/Client.php:/usr/src/code/vendor/utopia-php/mongo/src/Client.php
2121
environment:
2222
PHP_IDE_CONFIG: serverName=tests
2323

src/Database/Adapter/Mongo.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,6 @@ public function createIndex(string $collection, string $id, string $type, array
652652

653653
$indexes = [];
654654
$options = [];
655-
656655
$indexes['name'] = $id;
657656

658657
// If sharedTables, always add _tenant as the first key
@@ -661,15 +660,15 @@ public function createIndex(string $collection, string $id, string $type, array
661660
}
662661

663662
foreach ($attributes as $i => $attribute) {
664-
$attribute = $this->filter($this->getInternalKeyForAttribute($attribute));
663+
$attributes[$i] = $this->filter($this->getInternalKeyForAttribute($attribute));
665664
$orderType = $this->getOrder($this->filter($orders[$i] ?? Database::ORDER_ASC));
666-
$indexes['key'][$attribute] = $orderType;
665+
$indexes['key'][$attributes[$i] ] = $orderType;
667666

668667
switch ($type) {
669668
case Database::INDEX_KEY:
670669
break;
671670
case Database::INDEX_FULLTEXT:
672-
$indexes['key'][$attribute] = 'text';
671+
$indexes['key'][$attributes[$i]] = 'text';
673672
break;
674673
case Database::INDEX_UNIQUE:
675674
$indexes['unique'] = true;
@@ -1168,7 +1167,7 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
11681167
$attributes['_permissions'] = $document->getPermissions();
11691168

11701169
if (!empty($document->getSequence())) {
1171-
$attributes['_id'] = new ObjectId($document->getSequence());
1170+
$attributes['_id'] = $document->getSequence();
11721171
}
11731172

11741173
if ($this->sharedTables) {
@@ -1204,6 +1203,13 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
12041203
$update = [
12051204
'$set' => $record
12061205
];
1206+
1207+
// Add UUID7 _id for new documents in upsert operations
1208+
if (empty($document->getSequence())) {
1209+
$update['$setOnInsert'] = [
1210+
'_id' => $this->client->createUuid()
1211+
];
1212+
}
12071213
}
12081214

12091215
$operations[] = [
@@ -1358,7 +1364,7 @@ public function deleteDocuments(string $collection, array $sequences, array $per
13581364
$name = $this->getNamespace() . '_' . $this->filter($collection);
13591365

13601366
foreach ($sequences as $index => $sequence) {
1361-
$sequences[$index] = new ObjectId($sequence);
1367+
$sequences[$index] = $sequence;
13621368
}
13631369

13641370
$filters = $this->buildFilters([new Query(Query::TYPE_EQUAL, '_id', $sequences)]);
@@ -1512,7 +1518,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
15121518

15131519
$tmp = $cursor[$originalPrev];
15141520
if ($originalPrev === '$sequence') {
1515-
$tmp = new ObjectId($tmp);
1521+
$tmp = $tmp;
15161522
}
15171523

15181524
$andConditions[] = [
@@ -1523,8 +1529,6 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
15231529
$tmp = $cursor[$originalAttribute];
15241530

15251531
if ($originalAttribute === '$sequence') {
1526-
$tmp = new ObjectId($tmp);
1527-
15281532
/** If there is only $sequence attribute in $orderAttributes skip Or And operators **/
15291533
if (count($orderAttributes) === 1) {
15301534
$filters[$attribute] = [
@@ -1594,7 +1598,7 @@ private function getMongoTypeCode(string $appwriteType): string
15941598
Database::VAR_BOOLEAN => 'bool',
15951599
Database::VAR_DATETIME => 'date',
15961600
Database::VAR_ID => 'string',
1597-
Database::VAR_OBJECT_ID => 'objectId',
1601+
Database::VAR_UUID7 => 'string',
15981602
default => 'string'
15991603
};
16001604
}
@@ -1792,7 +1796,7 @@ protected function replaceChars(string $from, string $to, array $array): array
17921796
unset($result['$id']);
17931797
}
17941798
if (array_key_exists('$sequence', $array)) {
1795-
$result['_id'] = new ObjectId($array['$sequence']);
1799+
$result['_id'] = $array['$sequence'];
17961800
unset($result['$sequence']);
17971801
}
17981802
if (array_key_exists('$tenant', $array)) {
@@ -1842,7 +1846,7 @@ protected function buildFilter(Query $query): array
18421846
$query->setAttribute('_id');
18431847
$values = $query->getValues();
18441848
foreach ($values as $k => $v) {
1845-
$values[$k] = new ObjectId($v);
1849+
$values[$k] = $v;
18461850
}
18471851
$query->setValues($values);
18481852
} elseif ($query->getAttribute() === '$createdAt') {
@@ -2400,7 +2404,7 @@ protected function execute(mixed $stmt): bool
24002404
*/
24012405
public function getIdAttributeType(): string
24022406
{
2403-
return Database::VAR_OBJECT_ID;
2407+
return Database::VAR_UUID7;
24042408
}
24052409

24062410
/**

src/Database/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Database
4242
public const VAR_BOOLEAN = 'boolean';
4343
public const VAR_DATETIME = 'datetime';
4444
public const VAR_ID = 'id';
45-
public const VAR_OBJECT_ID = 'objectId';
45+
public const VAR_UUID7 = 'uuid7';
4646

4747
public const INT_MAX = 2147483647;
4848
public const BIG_INT_MAX = PHP_INT_MAX;

src/Database/Validator/Sequence.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ public function isValid($value): bool
4646
}
4747

4848
switch ($this->idAttributeType) {
49-
case Database::VAR_OBJECT_ID:
50-
return preg_match('/^[a-f0-9]{24}$/i', $value) === 1;
51-
49+
case Database::VAR_UUID7: //UUID7
50+
return preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-7[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}$/i', $value) === 1;
5251
case Database::VAR_INTEGER:
5352
$start = ($this->primary) ? 1 : 0;
5453
$validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER);

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public function testCreateDocument(): Document
4343
$this->assertEquals(true, $database->createAttribute('documents', 'id', Database::VAR_ID, 0, false, null));
4444

4545
$sequence = '1000000';
46-
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_OBJECT_ID) {
47-
$sequence = '6890c1e3c00288c2470de7a0' ;
46+
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_UUID7) {
47+
$sequence = '01890dd5-7331-7f3a-9c1b-123456789abc' ;
4848
}
4949

5050
$document = $database->createDocument('documents', new Document([
@@ -102,8 +102,8 @@ public function testCreateDocument(): Document
102102

103103

104104
$sequence = '56000';
105-
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_OBJECT_ID) {
106-
$sequence = '6890c1e3c00288c2470de7b3' ;
105+
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_UUID7) {
106+
$sequence = '01890dd5-7331-7f3a-9c1b-123456789def' ;
107107
}
108108

109109
// Test create document with manual internal id
@@ -279,8 +279,8 @@ public function testCreateDocument(): Document
279279
$this->assertNull($documentIdNull->getAttribute('id'));
280280

281281
$sequence = '0';
282-
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_OBJECT_ID) {
283-
$sequence = '6890c1e3c00288c0000de7b3';
282+
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_UUID7) {
283+
$sequence = '01890dd5-7331-7f3a-9c1b-123456789abc';
284284
}
285285

286286
/**
@@ -402,8 +402,10 @@ public function testCreateDocumentsWithAutoIncrement(): void
402402
$offset = 1000000;
403403
for ($i = $offset; $i <= ($offset + 10); $i++) {
404404
$sequence = (string)$i;
405-
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_OBJECT_ID) {
406-
$sequence = '689000288c0000de7'.$i;
405+
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_UUID7) {
406+
// Replace last 6 digits with $i to make it unique
407+
$suffix = str_pad(substr((string)$i, -6), 6, '0', STR_PAD_LEFT);
408+
$sequence = '01890dd5-7331-7f3a-9c1b-123456' . $suffix;
407409
}
408410

409411
$hash[$i] = $sequence;
@@ -4712,8 +4714,8 @@ public function testExceptionCaseInsensitiveDuplicate(Document $document): Docum
47124714
$database = static::getDatabase();
47134715

47144716
$sequence = '200';
4715-
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_OBJECT_ID) {
4716-
$sequence = '6890c1e3c00288c2470de7a0' ;
4717+
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_UUID7) {
4718+
$sequence = '01890dd5-7331-7f3a-9c1b-123456789abc' ;
47174719
}
47184720

47194721
$document->setAttribute('$id', 'caseSensitive');

0 commit comments

Comments
 (0)