Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"utopia-php/framework": "0.33.*",
"utopia-php/cache": "0.13.*",
"utopia-php/pools": "0.8.*",
"utopia-php/mongo": "dev-feat-partial-filter-expression as 0.5.3"
"utopia-php/mongo": "dev-feat-create-UUID as 0.5.3"
},
"require-dev": {
"fakerphp/faker": "1.23.*",
Expand Down
48 changes: 26 additions & 22 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
- ./dev/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
- /var/run/docker.sock:/var/run/docker.sock
- ./docker-compose.yml:/usr/src/code/docker-compose.yml
#- ./vendor/utopia-php/mongo/src/Client.php:/usr/src/code/vendor/utopia-php/mongo/src/Client.php
- ./vendor/utopia-php/mongo/src/Client.php:/usr/src/code/vendor/utopia-php/mongo/src/Client.php
environment:
PHP_IDE_CONFIG: serverName=tests

Expand Down
30 changes: 17 additions & 13 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,6 @@ public function createIndex(string $collection, string $id, string $type, array

$indexes = [];
$options = [];

$indexes['name'] = $id;

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

foreach ($attributes as $i => $attribute) {
$attribute = $this->filter($this->getInternalKeyForAttribute($attribute));
$attributes[$i] = $this->filter($this->getInternalKeyForAttribute($attribute));
$orderType = $this->getOrder($this->filter($orders[$i] ?? Database::ORDER_ASC));
$indexes['key'][$attribute] = $orderType;
$indexes['key'][$attributes[$i] ] = $orderType;

switch ($type) {
case Database::INDEX_KEY:
break;
case Database::INDEX_FULLTEXT:
$indexes['key'][$attribute] = 'text';
$indexes['key'][$attributes[$i]] = 'text';
break;
case Database::INDEX_UNIQUE:
$indexes['unique'] = true;
Expand Down Expand Up @@ -1168,7 +1167,7 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
$attributes['_permissions'] = $document->getPermissions();

if (!empty($document->getSequence())) {
$attributes['_id'] = new ObjectId($document->getSequence());
$attributes['_id'] = $document->getSequence();
}

if ($this->sharedTables) {
Expand Down Expand Up @@ -1204,6 +1203,13 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
$update = [
'$set' => $record
];

// Add UUID7 _id for new documents in upsert operations
if (empty($document->getSequence())) {
$update['$setOnInsert'] = [
'_id' => $this->client->createUuid()
];
}
}

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

foreach ($sequences as $index => $sequence) {
$sequences[$index] = new ObjectId($sequence);
$sequences[$index] = $sequence;
}

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

$tmp = $cursor[$originalPrev];
if ($originalPrev === '$sequence') {
$tmp = new ObjectId($tmp);
$tmp = $tmp;
}

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

if ($originalAttribute === '$sequence') {
$tmp = new ObjectId($tmp);

/** If there is only $sequence attribute in $orderAttributes skip Or And operators **/
if (count($orderAttributes) === 1) {
$filters[$attribute] = [
Expand Down Expand Up @@ -1594,7 +1598,7 @@ private function getMongoTypeCode(string $appwriteType): string
Database::VAR_BOOLEAN => 'bool',
Database::VAR_DATETIME => 'date',
Database::VAR_ID => 'string',
Database::VAR_OBJECT_ID => 'objectId',
Database::VAR_UUID7 => 'string',
default => 'string'
};
}
Expand Down Expand Up @@ -1792,7 +1796,7 @@ protected function replaceChars(string $from, string $to, array $array): array
unset($result['$id']);
}
if (array_key_exists('$sequence', $array)) {
$result['_id'] = new ObjectId($array['$sequence']);
$result['_id'] = $array['$sequence'];
unset($result['$sequence']);
}
if (array_key_exists('$tenant', $array)) {
Expand Down Expand Up @@ -1842,7 +1846,7 @@ protected function buildFilter(Query $query): array
$query->setAttribute('_id');
$values = $query->getValues();
foreach ($values as $k => $v) {
$values[$k] = new ObjectId($v);
$values[$k] = $v;
}
$query->setValues($values);
} elseif ($query->getAttribute() === '$createdAt') {
Expand Down Expand Up @@ -2400,7 +2404,7 @@ protected function execute(mixed $stmt): bool
*/
public function getIdAttributeType(): string
{
return Database::VAR_OBJECT_ID;
return Database::VAR_UUID7;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Database
public const VAR_BOOLEAN = 'boolean';
public const VAR_DATETIME = 'datetime';
public const VAR_ID = 'id';
public const VAR_OBJECT_ID = 'objectId';
public const VAR_UUID7 = 'uuid7';

public const INT_MAX = 2147483647;
public const BIG_INT_MAX = PHP_INT_MAX;
Expand Down
5 changes: 2 additions & 3 deletions src/Database/Validator/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ public function isValid($value): bool
}

switch ($this->idAttributeType) {
case Database::VAR_OBJECT_ID:
return preg_match('/^[a-f0-9]{24}$/i', $value) === 1;

case Database::VAR_UUID7: //UUID7
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;
case Database::VAR_INTEGER:
$start = ($this->primary) ? 1 : 0;
$validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER);
Expand Down
22 changes: 12 additions & 10 deletions tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function testCreateDocument(): Document
$this->assertEquals(true, $database->createAttribute('documents', 'id', Database::VAR_ID, 0, false, null));

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

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


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

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

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

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

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

$sequence = '200';
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_OBJECT_ID) {
$sequence = '6890c1e3c00288c2470de7a0' ;
if ($database->getAdapter()->getIdAttributeType() == Database::VAR_UUID7) {
$sequence = '01890dd5-7331-7f3a-9c1b-123456789abc' ;
}

$document->setAttribute('$id', 'caseSensitive');
Expand Down
Loading