Skip to content

Commit 3037e58

Browse files
committed
Postgres adapter:
1 parent e138860 commit 3037e58

3 files changed

Lines changed: 46 additions & 13 deletions

File tree

src/Database/Adapter/Memory.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,14 +1246,29 @@ public function updateDocument(Document $collection, string $id, Document $docum
12461246
$document->setAttribute($attribute, $value);
12471247
}
12481248

1249-
$newId = $document->getId();
1249+
$newId = $document->offsetExists('$id') ? $document->getId() : $id;
12501250
$newKey = $this->documentKey($newId);
12511251
if ($newId !== $id && isset($this->data[$key]['documents'][$newKey])) {
12521252
throw new DuplicateException('Document already exists');
12531253
}
12541254

12551255
$update = $this->documentToRow($document);
12561256

1257+
// For partial updates, documentToRow unconditionally maps internal fields
1258+
// ($id → _uid, $createdAt → _createdAt, $permissions → _permissions) from
1259+
// the partial document, which would overwrite existing row values with
1260+
// empty defaults. Strip any internal field not actually present in the
1261+
// document so the sparse array_merge below only touches what the caller set.
1262+
if (!$document->offsetExists('$id')) {
1263+
unset($update['_uid']);
1264+
}
1265+
if (!$document->offsetExists('$createdAt')) {
1266+
unset($update['_createdAt']);
1267+
}
1268+
if (!$document->offsetExists('$permissions')) {
1269+
unset($update['_permissions']);
1270+
}
1271+
12571272
// Sparse update — MariaDB's UPDATE only sets columns present in the
12581273
// document; absent columns retain their previous values. The wrapper
12591274
// relies on this for relationship updates, where it removes
@@ -1338,6 +1353,9 @@ public function updateDocument(Document $collection, string $id, Document $docum
13381353
if ($newId !== $id) {
13391354
$this->removePermissionsForDocument($key, $newId, $tenant, $this->sharedTables);
13401355
}
1356+
if (! $document->offsetExists('$id')) {
1357+
$document->setAttribute('$id', $newId);
1358+
}
13411359
$this->writePermissions($key, $document);
13421360
} elseif ($newId !== $id) {
13431361
// Rename-only path: rebind every permission entry whose document

src/Database/Adapter/SQLite.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,14 +1272,28 @@ public function updateDocument(Document $collection, string $id, Document $docum
12721272
$spatialAttributes = $this->getSpatialAttributes($collection);
12731273
$collection = $collection->getId();
12741274
$attributes = $document->getAttributes();
1275-
$attributes['_createdAt'] = $document->getCreatedAt();
1276-
$attributes['_updatedAt'] = $document->getUpdatedAt();
1277-
$attributes['_permissions'] = json_encode($document->getPermissions());
1275+
1276+
if ($document->offsetExists('$updatedAt')) {
1277+
$attributes['_updatedAt'] = $document->getUpdatedAt();
1278+
}
1279+
if ($document->offsetExists('$createdAt')) {
1280+
$attributes['_createdAt'] = $document->getCreatedAt();
1281+
}
1282+
if ($document->offsetExists('$id')) {
1283+
$attributes['_uid'] = $document->getId();
1284+
}
1285+
if ($document->offsetExists('$permissions')) {
1286+
$attributes['_permissions'] = json_encode($document->getPermissions());
1287+
}
12781288

12791289
if ($this->sharedTables) {
12801290
$attributes['_tenant'] = $this->tenant;
12811291
}
12821292

1293+
if (empty($attributes)) {
1294+
return $document;
1295+
}
1296+
12831297
$name = $this->filter($collection);
12841298
$columns = '';
12851299

@@ -1297,7 +1311,7 @@ public function updateDocument(Document $collection, string $id, Document $docum
12971311
* Get current permissions from the database
12981312
*/
12991313
$permissionsStmt = $this->getPDO()->prepare($sql);
1300-
$permissionsStmt->bindValue(':_uid', $document->getId());
1314+
$permissionsStmt->bindValue(':_uid', $id);
13011315

13021316
if ($this->sharedTables) {
13031317
$permissionsStmt->bindValue(':_tenant', $this->tenant);
@@ -1369,7 +1383,7 @@ public function updateDocument(Document $collection, string $id, Document $docum
13691383
$removeQuery = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $removeQuery);
13701384

13711385
$stmtRemovePermissions = $this->getPDO()->prepare($removeQuery);
1372-
$stmtRemovePermissions->bindValue(':_uid', $document->getId());
1386+
$stmtRemovePermissions->bindValue(':_uid', $id);
13731387

13741388
if ($this->sharedTables) {
13751389
$stmtRemovePermissions->bindValue(':_tenant', $this->tenant);
@@ -1404,7 +1418,8 @@ public function updateDocument(Document $collection, string $id, Document $docum
14041418

14051419
$stmtAddPermissions = $this->getPDO()->prepare($sql);
14061420

1407-
$stmtAddPermissions->bindValue(":_uid", $document->getId());
1421+
$newUid = $document->offsetExists('$id') ? $document->getId() : $id;
1422+
$stmtAddPermissions->bindValue(":_uid", $newUid);
14081423
if ($this->sharedTables) {
14091424
$stmtAddPermissions->bindValue(":_tenant", $this->tenant);
14101425
}
@@ -1456,7 +1471,7 @@ public function updateDocument(Document $collection, string $id, Document $docum
14561471

14571472
$sql = "
14581473
UPDATE `{$this->getNamespace()}_{$name}`
1459-
SET {$columns}, _uid = :_newUid
1474+
SET {$columns}
14601475
WHERE _uid = :_existingUid
14611476
{$this->getTenantQuery($collection)}
14621477
";
@@ -1466,7 +1481,6 @@ public function updateDocument(Document $collection, string $id, Document $docum
14661481
$stmt = $this->getPDO()->prepare($sql);
14671482

14681483
$stmt->bindValue(':_existingUid', $id);
1469-
$stmt->bindValue(':_newUid', $document->getId());
14701484

14711485
if ($this->sharedTables) {
14721486
$stmt->bindValue(':_tenant', $this->tenant);

src/Database/Document.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ArrayObject;
66
use Utopia\Database\Exception as DatabaseException;
77
use Utopia\Database\Exception\Structure as StructureException;
8+
use MongoDB\BSON\UTCDateTime;
89

910
/**
1011
* @extends ArrayObject<string, mixed>
@@ -157,17 +158,17 @@ public function getPermissionsByType(string $type): array
157158
}
158159

159160
/**
160-
* @return string|null
161+
* @return string|null|UTCDateTime
161162
*/
162-
public function getCreatedAt(): ?string
163+
public function getCreatedAt(): string|UTCDateTime|null
163164
{
164165
return $this->getAttribute('$createdAt');
165166
}
166167

167168
/**
168-
* @return string|null
169+
* @return string|null|UTCDateTime
169170
*/
170-
public function getUpdatedAt(): ?string
171+
public function getUpdatedAt(): string|UTCDateTime|null
171172
{
172173
return $this->getAttribute('$updatedAt');
173174
}

0 commit comments

Comments
 (0)