Skip to content

Commit 619176f

Browse files
committed
Merge branch 'feat-mongo-2' of github.com:utopia-php/database into mongo-transactions
# Conflicts: # src/Database/Adapter/Mongo.php
2 parents b262c21 + f0a9d05 commit 619176f

5 files changed

Lines changed: 19 additions & 36 deletions

File tree

src/Database/Adapter.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ public function withTransaction(callable $callback): mixed
376376
$this->startTransaction();
377377
$result = $callback();
378378
$this->commitTransaction();
379-
380379
return $result;
381380
} catch (\Throwable $action) {
382381
try {

src/Database/Adapter/Mongo.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ public function createCollection(string $name, array $attributes = [], array $in
340340
// Returns an array/object with the result document
341341
try {
342342
$this->getClient()->createCollection($id);
343+
343344
} catch (MongoException $e) {
344345
throw new Duplicate($e->getMessage(), $e->getCode(), $e);
345346
}
@@ -534,6 +535,7 @@ public function createAttributes(string $collection, array $attributes): bool
534535
public function deleteAttribute(string $collection, string $id): bool
535536
{
536537
$collection = $this->getNamespace() . '_' . $this->filter($collection);
538+
537539
$this->getClient()->update(
538540
$collection,
539541
[],
@@ -734,6 +736,7 @@ public function createIndex(string $collection, string $id, string $type, array
734736
$id = $this->filter($id);
735737

736738
$indexes = [];
739+
$options = [];
737740

738741
// pass in custom index name
739742
$indexes['name'] = $id;
@@ -774,7 +777,7 @@ public function createIndex(string $collection, string $id, string $type, array
774777
];
775778
}
776779

777-
return $this->client->createIndexes($name, [$indexes]);
780+
return $this->client->createIndexes($name, [$indexes], $options);
778781
}
779782

780783
/**
@@ -865,7 +868,7 @@ public function getDocument(string $collection, string $id, array $queries = [],
865868
}
866869

867870
$result = $this->client->find($name, $filters, $options)->cursor->firstBatch;
868-
871+
869872
if (empty($result)) {
870873
return new Document([]);
871874
}
@@ -927,7 +930,6 @@ public function createDocuments(string $collection, array $documents): array
927930
{
928931
$name = $this->getNamespace() . '_' . $this->filter($collection);
929932

930-
// Initialize transaction context before validation to ensure transaction is active
931933
$options = $this->addTransactionContext([]);
932934

933935
$records = [];
@@ -1081,7 +1083,7 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
10811083
if (empty($changes)) {
10821084
return $changes;
10831085
}
1084-
1086+
10851087
try {
10861088
$name = $this->getNamespace() . '_' . $this->filter($collection);
10871089
$attribute = $this->filter($attribute);
@@ -1098,7 +1100,7 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
10981100
$attributes['_createdAt'] = $document->getCreatedAt();
10991101
$attributes['_updatedAt'] = $document->getUpdatedAt();
11001102
$attributes['_permissions'] = $document->getPermissions();
1101-
1103+
11021104
if (!empty($document->getSequence())) {
11031105
$attributes['_id'] = new ObjectId($document->getSequence());
11041106
} else {
@@ -1113,7 +1115,7 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
11131115
$record = $this->replaceChars('$', '_', $attributes);
11141116
$record = $this->timeToMongo($record);
11151117
$record = $this->removeNullKeys($record);
1116-
1118+
11171119

11181120
// Build filter for upsert
11191121
$filter = ['_uid' => $document->getId()];
@@ -1149,7 +1151,7 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
11491151
// Get sequences for documents that were created
11501152
if (!empty($documentIds)) {
11511153
$sequences = $this->getSequences($collection, $documentIds, $documentTenants);
1152-
1154+
11531155
foreach ($changes as $change) {
11541156
if (isset($sequences[$change->getNew()->getId()])) {
11551157
$change->getNew()->setAttribute('$sequence', $sequences[$change->getNew()->getId()]);
@@ -1160,7 +1162,7 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
11601162
} catch (MongoException $e) {
11611163
throw $this->processException($e);
11621164
}
1163-
1165+
11641166
return \array_map(fn ($change) => $change->getNew(), $changes);
11651167
}
11661168

@@ -1189,7 +1191,7 @@ protected function getSequences(string $collection, array $documentIds, array $d
11891191

11901192
try {
11911193
$results = $this->client->find($name, $filters, ['projection' => ['_uid' => 1, '_id' => 1]]);
1192-
1194+
11931195
foreach ($results->cursor->firstBatch as $result) {
11941196
$sequences[$result->_uid] = (string)$result->_id;
11951197
}

tests/e2e/Adapter/Scopes/CollectionTests.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -649,15 +649,12 @@ public function testCreateCollectionWithSchemaIndexes(): void
649649
]),
650650
];
651651

652-
/**
653-
* Update array length check to 255
654-
*/
655652
$indexes = [
656653
new Document([
657654
'$id' => ID::custom('idx_cards'),
658655
'type' => Database::INDEX_KEY,
659656
'attributes' => ['cards'],
660-
'lengths' => [500],
657+
'lengths' => [500], // Will be changed to Database::ARRAY_INDEX_LENGTH (255)
661658
'orders' => [Database::ORDER_DESC],
662659
]),
663660
new Document([
@@ -686,10 +683,6 @@ public function testCreateCollectionWithSchemaIndexes(): void
686683
);
687684

688685
$this->assertEquals($collection->getAttribute('indexes')[0]['attributes'][0], 'cards');
689-
/**
690-
* If we set getMaxIndexLength to 1024 then this tests pass but other tests that depend on index length fail
691-
*/
692-
693686
$this->assertEquals($collection->getAttribute('indexes')[0]['lengths'][0], Database::ARRAY_INDEX_LENGTH);
694687
$this->assertEquals($collection->getAttribute('indexes')[0]['orders'][0], null);
695688

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,16 @@ public function testUpsertDocuments(): void
431431
];
432432

433433
$results = [];
434-
435434
$count = $database->createOrUpdateDocuments(
436435
__FUNCTION__,
437436
$documents,
438437
onNext: function ($doc) use (&$results) {
439438
$results[] = $doc;
440439
}
441440
);
442-
443441

444442
$this->assertEquals(2, $count);
445-
443+
446444
$createdAt = [];
447445
foreach ($results as $index => $document) {
448446
$createdAt[$index] = $document->getCreatedAt();
@@ -455,8 +453,8 @@ public function testUpsertDocuments(): void
455453
$this->assertEquals(Database::BIG_INT_MAX, $document->getAttribute('bigint'));
456454
}
457455

458-
459456
$documents = $database->find(__FUNCTION__);
457+
460458
$this->assertEquals(2, count($documents));
461459

462460
foreach ($documents as $document) {
@@ -565,15 +563,15 @@ public function testUpsertDocumentsInc(): void
565563

566564
$documents[0]->setAttribute('integer', -1);
567565
$documents[1]->setAttribute('integer', -1);
568-
566+
569567
$database->createOrUpdateDocumentsWithIncrease(
570568
collection: __FUNCTION__,
571569
attribute: 'integer',
572570
documents: $documents
573571
);
574572

575573
$documents = $database->find(__FUNCTION__);
576-
574+
577575
foreach ($documents as $document) {
578576
$this->assertEquals(5, $document->getAttribute('integer'));
579577
}
@@ -1408,15 +1406,13 @@ public function testFindBasicChecks(): void
14081406
$this->assertEquals($firstDocumentId, $documents[0]->getId());
14091407

14101408
/**
1411-
* Check internal numeric ID sorting
1412-
*/
1409+
* Check internal numeric ID sorting
1410+
*/
14131411
$documents = $database->find('movies', [
14141412
Query::limit(25),
14151413
Query::offset(0),
14161414
Query::orderDesc(''),
14171415
]);
1418-
1419-
14201416
$this->assertEquals($movieDocuments[\count($movieDocuments) - 1]->getId(), $documents[0]->getId());
14211417
$documents = $database->find('movies', [
14221418
Query::limit(25),
@@ -1982,8 +1978,6 @@ public function testFindOrderByAfterNaturalOrder(): void
19821978
Query::orderDesc(''),
19831979
Query::cursorAfter($movies[1])
19841980
]);
1985-
//var_dump($movieDocuments);
1986-
19871981
$this->assertEquals(2, count($documents));
19881982
$this->assertEquals($movies[2]['name'], $documents[0]['name']);
19891983
$this->assertEquals($movies[3]['name'], $documents[1]['name']);
@@ -2013,7 +2007,6 @@ public function testFindOrderByAfterNaturalOrder(): void
20132007
Query::orderDesc(''),
20142008
Query::cursorAfter($movies[5])
20152009
]);
2016-
20172010
$this->assertEmpty(count($documents));
20182011
}
20192012
public function testFindOrderByBeforeNaturalOrder(): void
@@ -4277,7 +4270,7 @@ public function testEmptyTenant(): void
42774270
$document = $database->getDocument('documents', $document->getId());
42784271
$this->assertArrayHasKey('$id', $document);
42794272
$this->assertArrayNotHasKey('$tenant', $document);
4280-
4273+
42814274
$document = $database->updateDocument('documents', $document->getId(), $document);
42824275
$this->assertArrayHasKey('$id', $document);
42834276
$this->assertArrayNotHasKey('$tenant', $document);

tests/e2e/Adapter/Scopes/GeneralTests.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ public function testPreserveDatesUpdate(): void
120120
'attr1' => 'value3',
121121
]));
122122

123-
124-
125-
126123
$newDate = '2000-01-01T10:00:00.000+00:00';
127124

128125
$doc1->setAttribute('$updatedAt', $newDate);
@@ -540,7 +537,6 @@ public function testSharedTablesTenantPerDocument(): void
540537

541538
public function testCacheFallback(): void
542539
{
543-
544540
/** @var Database $database */
545541
$database = static::getDatabase();
546542

0 commit comments

Comments
 (0)