Skip to content

Commit 40d48a7

Browse files
Merge branch 'main' into fix/test-split
2 parents 3f8aad9 + 1c4aa4f commit 40d48a7

1 file changed

Lines changed: 63 additions & 18 deletions

File tree

src/Database/Database.php

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,13 @@ class Database
349349

350350
protected bool $migrating = false;
351351

352+
/**
353+
* List of collections that should be treated as globally accessible
354+
*
355+
* @var array<string, bool>
356+
*/
357+
protected array $globalCollections = [];
358+
352359
/**
353360
* Stack of collection IDs when creating or updating related documents
354361
* @var array<string>
@@ -1013,6 +1020,31 @@ public function getMaxQueryValues(): int
10131020
return $this->maxQueryValues;
10141021
}
10151022

1023+
/**
1024+
* Set list of collections which are globally accessible
1025+
*
1026+
* @param array<string> $collections
1027+
* @return $this
1028+
*/
1029+
public function setGlobalCollections(array $collections): static
1030+
{
1031+
foreach ($collections as $collection) {
1032+
$this->globalCollections[$collection] = true;
1033+
}
1034+
1035+
return $this;
1036+
}
1037+
1038+
/**
1039+
* Get list of collections which are globally accessible
1040+
*
1041+
* @return array<string>
1042+
*/
1043+
public function getGlobalCollections(): array
1044+
{
1045+
return \array_keys($this->globalCollections);
1046+
}
1047+
10161048
/**
10171049
* Get list of keywords that cannot be used
10181050
*
@@ -4710,11 +4742,8 @@ public function createOrUpdateDocumentsWithIncrease(
47104742
$documentSecurity = $collection->getAttribute('documentSecurity', false);
47114743
$time = DateTime::now();
47124744

4713-
$selects = ['$internalId', '$permissions'];
4714-
4715-
if ($this->getSharedTables()) {
4716-
$selects[] = '$tenant';
4717-
}
4745+
$created = 0;
4746+
$updated = 0;
47184747

47194748
foreach ($documents as $key => $document) {
47204749
if ($this->getSharedTables() && $this->getTenantPerDocument()) {
@@ -4732,7 +4761,13 @@ public function createOrUpdateDocumentsWithIncrease(
47324761
$updatesPermissions = \in_array('$permissions', \array_keys($document->getArrayCopy()))
47334762
&& $document->getPermissions() != $old->getPermissions();
47344763

4735-
if ($old->getAttributes() == $document->getAttributes() && !$updatesPermissions) {
4764+
if (
4765+
empty($attribute)
4766+
&& !$updatesPermissions
4767+
&& $old->getAttributes() == $document->getAttributes()
4768+
) {
4769+
// If not updating a single attribute and the
4770+
// document is the same as the old one, skip it
47364771
unset($documents[$key]);
47374772
continue;
47384773
}
@@ -4806,8 +4841,6 @@ public function createOrUpdateDocumentsWithIncrease(
48064841
);
48074842
}
48084843

4809-
$modified = 0;
4810-
48114844
foreach (\array_chunk($documents, $batchSize) as $chunk) {
48124845
/**
48134846
* @var array<Change> $chunk
@@ -4818,11 +4851,21 @@ public function createOrUpdateDocumentsWithIncrease(
48184851
$chunk
48194852
)));
48204853

4854+
foreach ($chunk as $change) {
4855+
if ($change->getOld()->isEmpty()) {
4856+
$created++;
4857+
} else {
4858+
$updated++;
4859+
}
4860+
}
4861+
48214862
foreach ($batch as $doc) {
48224863
if ($this->resolveRelationships) {
48234864
$doc = $this->silent(fn () => $this->populateDocumentRelationships($collection, $doc));
48244865
}
48254866

4867+
$doc = $this->decode($collection, $doc);
4868+
48264869
if ($this->getSharedTables() && $this->getTenantPerDocument()) {
48274870
$this->withTenant($doc->getTenant(), function () use ($collection, $doc) {
48284871
$this->purgeCachedDocument($collection->getId(), $doc->getId());
@@ -4832,16 +4875,16 @@ public function createOrUpdateDocumentsWithIncrease(
48324875
}
48334876

48344877
$onNext && $onNext($doc);
4835-
$modified++;
48364878
}
48374879
}
48384880

48394881
$this->trigger(self::EVENT_DOCUMENTS_UPSERT, new Document([
48404882
'$collection' => $collection->getId(),
4841-
'modified' => $modified,
4883+
'created' => $created,
4884+
'updated' => $updated,
48424885
]));
48434886

4844-
return $modified;
4887+
return $created + $updated;
48454888
}
48464889

48474890
/**
@@ -5595,16 +5638,12 @@ public function deleteDocuments(
55955638
}
55965639
}
55975640

5598-
$this->withTransaction(function () use ($collection, $skipAuth, $authorization, $internalIds, $permissionIds) {
5599-
$getResults = fn () => $this->adapter->deleteDocuments(
5641+
$this->withTransaction(function () use ($collection, $internalIds, $permissionIds) {
5642+
$this->adapter->deleteDocuments(
56005643
$collection->getId(),
56015644
$internalIds,
56025645
$permissionIds
56035646
);
5604-
5605-
$skipAuth
5606-
? $authorization->skip($getResults)
5607-
: $getResults();
56085647
});
56095648

56105649
foreach ($batch as $document) {
@@ -6461,12 +6500,18 @@ public function getCacheKeys(string $collectionId, ?string $documentId = null, a
64616500
$hostname = $this->adapter->getHostname();
64626501
}
64636502

6503+
$tenantSegment = $this->adapter->getTenant();
6504+
6505+
if (isset($this->globalCollections[$collectionId])) {
6506+
$tenantSegment = null;
6507+
}
6508+
64646509
$collectionKey = \sprintf(
64656510
'%s-cache-%s:%s:%s:collection:%s',
64666511
$this->cacheName,
64676512
$hostname ?? '',
64686513
$this->getNamespace(),
6469-
$this->adapter->getTenant(),
6514+
$tenantSegment,
64706515
$collectionId
64716516
);
64726517

0 commit comments

Comments
 (0)