Skip to content

Commit d1f639a

Browse files
committed
Merge branch 'main' of github.com:utopia-php/database into primary-bigint
# Conflicts: # docker-compose.yml
2 parents c6cba6e + e68f2e3 commit d1f639a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+10608
-1594
lines changed

composer.lock

Lines changed: 239 additions & 105 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ services:
1717
- ./dev/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
1818
- /var/run/docker.sock:/var/run/docker.sock
1919
- ./docker-compose.yml:/usr/src/code/docker-compose.yml
20+
environment:
21+
PHP_IDE_CONFIG: serverName=tests
2022

2123
adminer:
2224
image: adminer
@@ -28,7 +30,11 @@ services:
2830
- database
2931

3032
postgres:
31-
image: postgres:17.5
33+
build:
34+
context: .
35+
dockerfile: postgres.dockerfile
36+
args:
37+
POSTGRES_VERSION: 16
3238
container_name: utopia-postgres
3339
networks:
3440
- database
@@ -37,9 +43,14 @@ services:
3743
environment:
3844
POSTGRES_USER: root
3945
POSTGRES_PASSWORD: password
46+
POSTGRES_DB: root
4047

4148
postgres-mirror:
42-
image: postgres:17.5
49+
build:
50+
context: .
51+
dockerfile: postgres.dockerfile
52+
args:
53+
POSTGRES_VERSION: 16
4354
container_name: utopia-postgres-mirror
4455
networks:
4556
- database
@@ -48,6 +59,7 @@ services:
4859
environment:
4960
POSTGRES_USER: root
5061
POSTGRES_PASSWORD: password
62+
POSTGRES_DB: root
5163

5264
mariadb:
5365
image: mariadb:10.11

postgres.dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM postgres:16
2+
3+
RUN apt-get update \
4+
&& apt-get install -y --no-install-recommends \
5+
postgresql-16-postgis-3 \
6+
postgresql-16-postgis-3-scripts \
7+
&& rm -rf /var/lib/apt/lists/*

src/Database/Adapter.php

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
use Exception;
66
use Utopia\Database\Exception as DatabaseException;
7+
use Utopia\Database\Exception\Authorization as AuthorizationException;
8+
use Utopia\Database\Exception\Conflict as ConflictException;
79
use Utopia\Database\Exception\Duplicate as DuplicateException;
10+
use Utopia\Database\Exception\Limit as LimitException;
11+
use Utopia\Database\Exception\Relationship as RelationshipException;
12+
use Utopia\Database\Exception\Restricted as RestrictedException;
813
use Utopia\Database\Exception\Timeout as TimeoutException;
914
use Utopia\Database\Exception\Transaction as TransactionException;
1015

@@ -371,7 +376,10 @@ public function inTransaction(): bool
371376
*/
372377
public function withTransaction(callable $callback): mixed
373378
{
374-
for ($attempts = 0; $attempts < 3; $attempts++) {
379+
$sleep = 50_000; // 50 milliseconds
380+
$retries = 2;
381+
382+
for ($attempts = 0; $attempts <= $retries; $attempts++) {
375383
try {
376384
$this->startTransaction();
377385
$result = $callback();
@@ -380,18 +388,31 @@ public function withTransaction(callable $callback): mixed
380388
} catch (\Throwable $action) {
381389
try {
382390
$this->rollbackTransaction();
391+
392+
if (
393+
$action instanceof DuplicateException ||
394+
$action instanceof RestrictedException ||
395+
$action instanceof AuthorizationException ||
396+
$action instanceof RelationshipException ||
397+
$action instanceof ConflictException ||
398+
$action instanceof LimitException
399+
) {
400+
$this->inTransaction = 0;
401+
throw $action;
402+
}
403+
383404
} catch (\Throwable $rollback) {
384-
if ($attempts < 2) {
385-
\usleep(5000); // 5ms
405+
if ($attempts < $retries) {
406+
\usleep($sleep * ($attempts + 1));
386407
continue;
387408
}
388409

389410
$this->inTransaction = 0;
390411
throw $rollback;
391412
}
392413

393-
if ($attempts < 2) {
394-
\usleep(5000); // 5ms
414+
if ($attempts < $retries) {
415+
\usleep($sleep * ($attempts + 1));
395416
continue;
396417
}
397418

@@ -534,7 +555,7 @@ abstract public function analyzeCollection(string $collection): bool;
534555
* @throws TimeoutException
535556
* @throws DuplicateException
536557
*/
537-
abstract public function createAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false): bool;
558+
abstract public function createAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, bool $required = false): bool;
538559

539560
/**
540561
* Create Attributes
@@ -557,10 +578,11 @@ abstract public function createAttributes(string $collection, array $attributes)
557578
* @param bool $signed
558579
* @param bool $array
559580
* @param string|null $newKey
581+
* @param bool $required
560582
*
561583
* @return bool
562584
*/
563-
abstract public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool;
585+
abstract public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool;
564586

565587
/**
566588
* Delete Attribute
@@ -661,75 +683,75 @@ abstract public function deleteIndex(string $collection, string $id): bool;
661683
/**
662684
* Get Document
663685
*
664-
* @param string $collection
686+
* @param Document $collection
665687
* @param string $id
666688
* @param array<Query> $queries
667689
* @param bool $forUpdate
668690
* @return Document
669691
*/
670-
abstract public function getDocument(string $collection, string $id, array $queries = [], bool $forUpdate = false): Document;
692+
abstract public function getDocument(Document $collection, string $id, array $queries = [], bool $forUpdate = false): Document;
671693

672694
/**
673695
* Create Document
674696
*
675-
* @param string $collection
697+
* @param Document $collection
676698
* @param Document $document
677699
*
678700
* @return Document
679701
*/
680-
abstract public function createDocument(string $collection, Document $document): Document;
702+
abstract public function createDocument(Document $collection, Document $document): Document;
681703

682704
/**
683705
* Create Documents in batches
684706
*
685-
* @param string $collection
707+
* @param Document $collection
686708
* @param array<Document> $documents
687709
*
688710
* @return array<Document>
689711
*
690712
* @throws DatabaseException
691713
*/
692-
abstract public function createDocuments(string $collection, array $documents): array;
714+
abstract public function createDocuments(Document $collection, array $documents): array;
693715

694716
/**
695717
* Update Document
696718
*
697-
* @param string $collection
719+
* @param Document $collection
698720
* @param string $id
699721
* @param Document $document
700722
* @param bool $skipPermissions
701723
*
702724
* @return Document
703725
*/
704-
abstract public function updateDocument(string $collection, string $id, Document $document, bool $skipPermissions): Document;
726+
abstract public function updateDocument(Document $collection, string $id, Document $document, bool $skipPermissions): Document;
705727

706728
/**
707729
* Update documents
708730
*
709731
* Updates all documents which match the given query.
710732
*
711-
* @param string $collection
733+
* @param Document $collection
712734
* @param Document $updates
713735
* @param array<Document> $documents
714736
*
715737
* @return int
716738
*
717739
* @throws DatabaseException
718740
*/
719-
abstract public function updateDocuments(string $collection, Document $updates, array $documents): int;
741+
abstract public function updateDocuments(Document $collection, Document $updates, array $documents): int;
720742

721743
/**
722744
* Create documents if they do not exist, otherwise update them.
723745
*
724746
* If attribute is not empty, only the specified attribute will be increased, by the new value in each document.
725747
*
726-
* @param string $collection
748+
* @param Document $collection
727749
* @param string $attribute
728750
* @param array<Change> $changes
729751
* @return array<Document>
730752
*/
731-
abstract public function createOrUpdateDocuments(
732-
string $collection,
753+
abstract public function upsertDocuments(
754+
Document $collection,
733755
string $attribute,
734756
array $changes
735757
): array;
@@ -767,7 +789,7 @@ abstract public function deleteDocuments(string $collection, array $sequences, a
767789
*
768790
* Find data sets using chosen queries
769791
*
770-
* @param string $collection
792+
* @param Document $collection
771793
* @param array<Query> $queries
772794
* @param int|null $limit
773795
* @param int|null $offset
@@ -776,33 +798,32 @@ abstract public function deleteDocuments(string $collection, array $sequences, a
776798
* @param array<string, mixed> $cursor
777799
* @param string $cursorDirection
778800
* @param string $forPermission
779-
*
780801
* @return array<Document>
781802
*/
782-
abstract public function find(string $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER, string $forPermission = Database::PERMISSION_READ): array;
803+
abstract public function find(Document $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER, string $forPermission = Database::PERMISSION_READ): array;
783804

784805
/**
785806
* Sum an attribute
786807
*
787-
* @param string $collection
808+
* @param Document $collection
788809
* @param string $attribute
789810
* @param array<Query> $queries
790811
* @param int|null $max
791812
*
792813
* @return int|float
793814
*/
794-
abstract public function sum(string $collection, string $attribute, array $queries = [], ?int $max = null): float|int;
815+
abstract public function sum(Document $collection, string $attribute, array $queries = [], ?int $max = null): float|int;
795816

796817
/**
797818
* Count Documents
798819
*
799-
* @param string $collection
820+
* @param Document $collection
800821
* @param array<Query> $queries
801822
* @param int|null $max
802823
*
803824
* @return int
804825
*/
805-
abstract public function count(string $collection, array $queries = [], ?int $max = null): int;
826+
abstract public function count(Document $collection, array $queries = [], ?int $max = null): int;
806827

807828
/**
808829
* Get Collection Size of the raw data
@@ -862,6 +883,13 @@ abstract public function getMaxIndexLength(): int;
862883
*/
863884
abstract public function getMinDateTime(): \DateTime;
864885

886+
/**
887+
* Get the primitive type of the primary key type for this adapter
888+
*
889+
* @return string
890+
*/
891+
abstract public function getIdAttributeType(): string;
892+
865893
/**
866894
* Get the maximum supported DateTime value
867895
*
@@ -1022,6 +1050,48 @@ abstract public function getSupportForHostname(): bool;
10221050
*/
10231051
abstract public function getSupportForBatchCreateAttributes(): bool;
10241052

1053+
/**
1054+
* Is spatial attributes supported?
1055+
*
1056+
* @return bool
1057+
*/
1058+
abstract public function getSupportForSpatialAttributes(): bool;
1059+
1060+
/**
1061+
* Does the adapter support null values in spatial indexes?
1062+
*
1063+
* @return bool
1064+
*/
1065+
abstract public function getSupportForSpatialIndexNull(): bool;
1066+
1067+
/**
1068+
* Does the adapter support order attribute in spatial indexes?
1069+
*
1070+
* @return bool
1071+
*/
1072+
abstract public function getSupportForSpatialIndexOrder(): bool;
1073+
1074+
/**
1075+
* Does the adapter support spatial axis order specification?
1076+
*
1077+
* @return bool
1078+
*/
1079+
abstract public function getSupportForSpatialAxisOrder(): bool;
1080+
1081+
/**
1082+
* Does the adapter includes boundary during spatial contains?
1083+
*
1084+
* @return bool
1085+
*/
1086+
abstract public function getSupportForBoundaryInclusiveContains(): bool;
1087+
1088+
/**
1089+
* Does the adapter support calculating distance(in meters) between multidimension geometry(line, polygon,etc)?
1090+
*
1091+
* @return bool
1092+
*/
1093+
abstract public function getSupportForDistanceBetweenMultiDimensionGeometryInMeters(): bool;
1094+
10251095
/**
10261096
* Get current attribute count from collection document
10271097
*

0 commit comments

Comments
 (0)