Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnFailure="true"
>
<testsuites>
<testsuite name="unit">
Expand Down
16 changes: 13 additions & 3 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\NotFound as NotFoundException;
use Utopia\Database\Exception\Query as QueryException;
Comment thread
fogelito marked this conversation as resolved.
Outdated
use Utopia\Database\Exception\Transaction as TransactionException;
use Utopia\Database\Query;

Expand Down Expand Up @@ -426,10 +427,20 @@ public function updateDocuments(string $collection, Document $updates, array $do
$attributes['_createdAt'] = $updates->getCreatedAt();
}

if (!empty($updates->getPermissions())) {
if ($updates->offsetExists('$permissions')) {
$attributes['_permissions'] = json_encode($updates->getPermissions());
}

// if ($updatePermissions) {
// $originalPermissions = $document->getPermissions();
// $currentPermissions = $updates->getPermissions();
//
// sort($originalPermissions);
// sort($currentPermissions);
//
// $skipPermissionsUpdate = ($originalPermissions === $currentPermissions);
// }

if (empty($attributes)) {
return 0;
}
Expand Down Expand Up @@ -484,15 +495,14 @@ public function updateDocuments(string $collection, Document $updates, array $do
$affected = $stmt->rowCount();

// Permissions logic
if (!empty($updates->getPermissions())) {
if ($updates->offsetExists('$permissions')) {
$removeQueries = [];
$removeBindValues = [];

$addQuery = '';
$addBindValues = [];

foreach ($documents as $index => $document) {
// Permissions logic
$sql = "
SELECT _type, _permission
FROM {$this->getSQLTable($name . '_perms')}
Expand Down
7 changes: 5 additions & 2 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4375,13 +4375,16 @@ public function updateDocuments(
if (!empty($cursor) && $cursor->getCollection() !== $collection->getId()) {
throw new DatabaseException("Cursor document must be from the same Collection.");
}

unset($updates['$id']);
unset($updates['$tenant']);

if (($updates->getCreatedAt() === null || !$this->preserveDates)) {
unset($updates['$createdAt']);
} else {
$updates['$createdAt'] = $updates->getCreatedAt();
}

if ($this->adapter->getSharedTables()) {
$updates['$tenant'] = $this->adapter->getTenant();
}
Expand Down Expand Up @@ -4431,7 +4434,7 @@ public function updateDocuments(
}

$this->withTransaction(function () use ($collection, $updates, &$batch) {
foreach ($batch as &$document) {
foreach ($batch as $index => $document) {
$new = new Document(\array_merge($document->getArrayCopy(), $updates->getArrayCopy()));

if ($this->resolveRelationships) {
Expand All @@ -4451,7 +4454,7 @@ public function updateDocuments(
throw new ConflictException('Document was updated after the request timestamp');
}

$document = $this->encode($collection, $document);
$batch[$index] = $this->encode($collection, $document);
}

$this->adapter->updateDocuments(
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

abstract class Base extends TestCase
{
use CollectionTests;
use DocumentTests;
use AttributeTests;
use IndexTests;
// use CollectionTests;
// use DocumentTests;
// use AttributeTests;
// use IndexTests;
use PermissionTests;
use RelationshipTests;
use GeneralTests;
Expand Down
7 changes: 7 additions & 0 deletions tests/e2e/Adapter/Scopes/PermissionTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,20 @@ public function testUnsetPermissions(): void
}

$documents = $database->find(__FUNCTION__);
$this->assertEquals(0, count($documents));

Authorization::disable();
$documents = $database->find(__FUNCTION__);
Authorization::reset();

$this->assertEquals(3, count($documents));

foreach ($documents as $document) {
$this->assertEquals('Richard Nixon', $document->getAttribute('president'));
$this->assertEquals([], $document->getPermissions());
}

$this->assertEquals('shmuel', 'fogel');
Comment thread
fogelito marked this conversation as resolved.
Outdated
}

public function testCreateDocumentsEmptyPermission(): void
Expand Down
Loading