Skip to content

Commit cf463c0

Browse files
authored
Merge pull request #590 from utopia-php/feat-internal-to-sequence
Change internal ID to sequence
2 parents 3e2b50b + ba0b166 commit cf463c0

File tree

9 files changed

+68
-68
lines changed

9 files changed

+68
-68
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ public function updateRelationship(
564564
$collection = $this->getDocument(Database::METADATA, $collection);
565565
$relatedCollection = $this->getDocument(Database::METADATA, $relatedCollection);
566566

567-
$junction = $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId());
567+
$junction = $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence());
568568

569569
if (!\is_null($newKey)) {
570570
$sql = "ALTER TABLE {$junction} RENAME COLUMN `{$key}` TO `{$newKey}`;";
@@ -648,12 +648,12 @@ public function deleteRelationship(
648648
$relatedCollection = $this->getDocument(Database::METADATA, $relatedCollection);
649649

650650
$junction = $side === Database::RELATION_SIDE_PARENT
651-
? $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId())
652-
: $this->getSQLTable('_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId());
651+
? $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence())
652+
: $this->getSQLTable('_' . $relatedCollection->getSequence() . '_' . $collection->getSequence());
653653

654654
$perms = $side === Database::RELATION_SIDE_PARENT
655-
? $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId() . '_perms')
656-
: $this->getSQLTable('_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId() . '_perms');
655+
? $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence() . '_perms')
656+
: $this->getSQLTable('_' . $relatedCollection->getSequence() . '_' . $collection->getSequence() . '_perms');
657657

658658
$sql = "DROP TABLE {$junction}; DROP TABLE {$perms}";
659659
break;
@@ -718,7 +718,7 @@ public function createIndex(string $collection, string $id, string $type, array
718718
}
719719

720720
/**
721-
* We do not have internalId's added to list, since we check only for array field
721+
* We do not have sequence's added to list, since we check only for array field
722722
*/
723723
$collectionAttributes = \json_decode($collection->getAttribute('attributes', []), true);
724724

@@ -843,7 +843,7 @@ public function createDocument(string $collection, Document $document): Document
843843
}
844844

845845
// Insert internal ID if set
846-
if (!empty($document->getInternalId())) {
846+
if (!empty($document->getSequence())) {
847847
$bindKey = '_id';
848848
$columns .= "_id, ";
849849
$columnNames .= ':' . $bindKey . ', ';
@@ -860,8 +860,8 @@ public function createDocument(string $collection, Document $document): Document
860860

861861
$stmt->bindValue(':_uid', $document->getId());
862862

863-
if (!empty($document->getInternalId())) {
864-
$stmt->bindValue(':_id', $document->getInternalId());
863+
if (!empty($document->getSequence())) {
864+
$stmt->bindValue(':_id', $document->getSequence());
865865
}
866866

867867
$attributeIndex = 0;
@@ -943,15 +943,15 @@ public function createDocuments(string $collection, array $documents): array
943943

944944
$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;
945945

946-
$hasInternalId = null;
946+
$hasSequence = null;
947947
foreach ($documents as $document) {
948948
$attributes = $document->getAttributes();
949949
$attributeKeys = [...$attributeKeys, ...\array_keys($attributes)];
950950

951-
if ($hasInternalId === null) {
952-
$hasInternalId = !empty($document->getInternalId());
953-
} elseif ($hasInternalId == empty($document->getInternalId())) {
954-
throw new DatabaseException('All documents must have an internalId if one is set');
951+
if ($hasSequence === null) {
952+
$hasSequence = !empty($document->getSequence());
953+
} elseif ($hasSequence == empty($document->getSequence())) {
954+
throw new DatabaseException('All documents must have an sequence if one is set');
955955
}
956956
}
957957
$attributeKeys = array_unique($attributeKeys);
@@ -980,8 +980,8 @@ public function createDocuments(string $collection, array $documents): array
980980
$attributes['_updatedAt'] = $document->getUpdatedAt();
981981
$attributes['_permissions'] = \json_encode($document->getPermissions());
982982

983-
if (!empty($document->getInternalId())) {
984-
$attributes['_id'] = $document->getInternalId();
983+
if (!empty($document->getSequence())) {
984+
$attributes['_id'] = $document->getSequence();
985985
$attributeKeys[] = '_id';
986986
} else {
987987
$documentIds[] = $document->getId();
@@ -1051,7 +1051,7 @@ public function createDocuments(string $collection, array $documents): array
10511051
$stmtPermissions?->execute();
10521052
}
10531053

1054-
$sequences = $this->getInternalIds(
1054+
$sequences = $this->getSequences(
10551055
$collection,
10561056
$documentIds,
10571057
$documentTenants
@@ -1253,15 +1253,15 @@ public function updateDocument(string $collection, string $id, Document $documen
12531253
$sql = "
12541254
UPDATE {$this->getSQLTable($name)}
12551255
SET {$columns} _uid = :_newUid
1256-
WHERE _id=:_internalId
1256+
WHERE _id=:_sequence
12571257
{$this->getTenantQuery($collection)}
12581258
";
12591259

12601260
$sql = $this->trigger(Database::EVENT_DOCUMENT_UPDATE, $sql);
12611261

12621262
$stmt = $this->getPDO()->prepare($sql);
12631263

1264-
$stmt->bindValue(':_internalId', $document->getInternalId());
1264+
$stmt->bindValue(':_sequence', $document->getSequence());
12651265
$stmt->bindValue(':_newUid', $document->getId());
12661266

12671267
if ($this->sharedTables) {
@@ -1331,8 +1331,8 @@ public function createOrUpdateDocuments(
13311331
$attributes['_updatedAt'] = $document->getUpdatedAt();
13321332
$attributes['_permissions'] = \json_encode($document->getPermissions());
13331333

1334-
if (!empty($document->getInternalId())) {
1335-
$attributes['_id'] = $document->getInternalId();
1334+
if (!empty($document->getSequence())) {
1335+
$attributes['_id'] = $document->getSequence();
13361336
} else {
13371337
$documentIds[] = $document->getId();
13381338
}
@@ -1495,7 +1495,7 @@ public function createOrUpdateDocuments(
14951495
$stmtAddPermissions->execute();
14961496
}
14971497

1498-
$sequences = $this->getInternalIds(
1498+
$sequences = $this->getSequences(
14991499
$collection,
15001500
$documentIds,
15011501
$documentTenants
@@ -1663,12 +1663,12 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
16631663

16641664
// Get most dominant/first order attribute
16651665
if ($i === 0 && !empty($cursor)) {
1666-
$orderMethodInternalId = Query::TYPE_GREATER; // To preserve natural order
1666+
$orderMethodSequence = Query::TYPE_GREATER; // To preserve natural order
16671667
$orderMethod = $orderType === Database::ORDER_DESC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
16681668

16691669
if ($cursorDirection === Database::CURSOR_BEFORE) {
16701670
$orderType = $orderType === Database::ORDER_ASC ? Database::ORDER_DESC : Database::ORDER_ASC;
1671-
$orderMethodInternalId = $orderType === Database::ORDER_ASC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
1671+
$orderMethodSequence = $orderType === Database::ORDER_ASC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
16721672
$orderMethod = $orderType === Database::ORDER_DESC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
16731673
}
16741674

@@ -1686,7 +1686,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
16861686
OR (
16871687
{$this->quote($alias)}.{$this->quote($attribute)} = :cursor
16881688
AND
1689-
{$this->quote($alias)}._id {$this->getSQLOperator($orderMethodInternalId)} {$cursor['$sequence']}
1689+
{$this->quote($alias)}._id {$this->getSQLOperator($orderMethodSequence)} {$cursor['$sequence']}
16901690
)
16911691
)";
16921692
} elseif ($cursorDirection === Database::CURSOR_BEFORE) {

src/Database/Adapter/Postgres.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ public function updateRelationship(
710710
$collection = $this->getDocument(Database::METADATA, $collection);
711711
$relatedCollection = $this->getDocument(Database::METADATA, $relatedCollection);
712712

713-
$junction = $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId());
713+
$junction = $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence());
714714

715715
if (!\is_null($newKey)) {
716716
$sql = "ALTER TABLE {$junction} RENAME COLUMN \"{$key}\" TO \"{$newKey}\";";
@@ -795,12 +795,12 @@ public function deleteRelationship(
795795
$relatedCollection = $this->getDocument(Database::METADATA, $relatedCollection);
796796

797797
$junction = $side === Database::RELATION_SIDE_PARENT
798-
? $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId())
799-
: $this->getSQLTable('_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId());
798+
? $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence())
799+
: $this->getSQLTable('_' . $relatedCollection->getSequence() . '_' . $collection->getSequence());
800800

801801
$perms = $side === Database::RELATION_SIDE_PARENT
802-
? $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId() . '_perms')
803-
: $this->getSQLTable('_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId() . '_perms');
802+
? $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence() . '_perms')
803+
: $this->getSQLTable('_' . $relatedCollection->getSequence() . '_' . $collection->getSequence() . '_perms');
804804

805805
$sql = "DROP TABLE {$junction}; DROP TABLE {$perms}";
806806
break;
@@ -957,7 +957,7 @@ public function createDocument(string $collection, Document $document): Document
957957
$columnNames = '';
958958

959959
// Insert internal id if set
960-
if (!empty($document->getInternalId())) {
960+
if (!empty($document->getSequence())) {
961961
$bindKey = '_id';
962962
$columns .= "\"_id\", ";
963963
$columnNames .= ':' . $bindKey . ', ';
@@ -983,8 +983,8 @@ public function createDocument(string $collection, Document $document): Document
983983

984984
$stmt->bindValue(':_uid', $document->getId(), PDO::PARAM_STR);
985985

986-
if (!empty($document->getInternalId())) {
987-
$stmt->bindValue(':_id', $document->getInternalId(), PDO::PARAM_STR);
986+
if (!empty($document->getSequence())) {
987+
$stmt->bindValue(':_id', $document->getSequence(), PDO::PARAM_STR);
988988
}
989989

990990
$attributeIndex = 0;
@@ -1029,7 +1029,7 @@ public function createDocument(string $collection, Document $document): Document
10291029
try {
10301030
$this->execute($stmt);
10311031
$lastInsertedId = $this->getPDO()->lastInsertId();
1032-
// internalId can be manually as well
1032+
// Sequence can be manually set as well
10331033
$document['$sequence'] ??= $lastInsertedId;
10341034

10351035
if (isset($stmtPermissions)) {
@@ -1062,15 +1062,15 @@ public function createDocuments(string $collection, array $documents): array
10621062
$name = $this->filter($collection);
10631063
$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;
10641064

1065-
$hasInternalId = null;
1065+
$hasSequence = null;
10661066
foreach ($documents as $document) {
10671067
$attributes = $document->getAttributes();
10681068
$attributeKeys = array_merge($attributeKeys, array_keys($attributes));
10691069

1070-
if ($hasInternalId === null) {
1071-
$hasInternalId = !empty($document->getInternalId());
1072-
} elseif ($hasInternalId == empty($document->getInternalId())) {
1073-
throw new DatabaseException('All documents must have an internalId if one is set');
1070+
if ($hasSequence === null) {
1071+
$hasSequence = !empty($document->getSequence());
1072+
} elseif ($hasSequence == empty($document->getSequence())) {
1073+
throw new DatabaseException('All documents must have an sequence if one is set');
10741074
}
10751075
}
10761076
$attributeKeys = array_unique($attributeKeys);
@@ -1099,9 +1099,9 @@ public function createDocuments(string $collection, array $documents): array
10991099
$attributes['_updatedAt'] = $document->getUpdatedAt();
11001100
$attributes['_permissions'] = \json_encode($document->getPermissions());
11011101

1102-
if (!empty($document->getInternalId())) {
1102+
if (!empty($document->getSequence())) {
11031103
$sequences[$document->getId()] = true;
1104-
$attributes['_id'] = $document->getInternalId();
1104+
$attributes['_id'] = $document->getSequence();
11051105
$attributeKeys[] = '_id';
11061106
}
11071107

@@ -1177,7 +1177,7 @@ public function createDocuments(string $collection, array $documents): array
11771177
$collection,
11781178
$document->getId(),
11791179
[Query::select(['$sequence'])]
1180-
)->getInternalId();
1180+
)->getSequence();
11811181
}
11821182
}
11831183

@@ -1352,15 +1352,15 @@ public function updateDocument(string $collection, string $id, Document $documen
13521352
$sql = "
13531353
UPDATE {$this->getSQLTable($name)}
13541354
SET {$columns} _uid = :_newUid
1355-
WHERE _id=:_internalId
1355+
WHERE _id=:_sequence
13561356
{$this->getTenantQuery($collection)}
13571357
";
13581358

13591359
$sql = $this->trigger(Database::EVENT_DOCUMENT_UPDATE, $sql);
13601360

13611361
$stmt = $this->getPDO()->prepare($sql);
13621362

1363-
$stmt->bindValue(':_internalId', $document->getInternalId());
1363+
$stmt->bindValue(':_sequence', $document->getSequence());
13641364
$stmt->bindValue(':_newUid', $document->getId());
13651365

13661366
if ($this->sharedTables) {
@@ -1555,12 +1555,12 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
15551555

15561556
// Get most dominant/first order attribute
15571557
if ($i === 0 && !empty($cursor)) {
1558-
$orderMethodInternalId = Query::TYPE_GREATER; // To preserve natural order
1558+
$orderMethodSequence = Query::TYPE_GREATER; // To preserve natural order
15591559
$orderMethod = $orderType === Database::ORDER_DESC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
15601560

15611561
if ($cursorDirection === Database::CURSOR_BEFORE) {
15621562
$orderType = $orderType === Database::ORDER_ASC ? Database::ORDER_DESC : Database::ORDER_ASC;
1563-
$orderMethodInternalId = $orderType === Database::ORDER_ASC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
1563+
$orderMethodSequence = $orderType === Database::ORDER_ASC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
15641564
$orderMethod = $orderType === Database::ORDER_DESC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
15651565
}
15661566

@@ -1578,7 +1578,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
15781578
OR (
15791579
{$this->quote($alias)}.{$this->quote($attribute)} = :cursor
15801580
AND
1581-
{$this->quote($alias)}._id {$this->getSQLOperator($orderMethodInternalId)} {$cursor['$sequence']}
1581+
{$this->quote($alias)}._id {$this->getSQLOperator($orderMethodSequence)} {$cursor['$sequence']}
15821582
)
15831583
)";
15841584
} elseif ($cursorDirection === Database::CURSOR_BEFORE) {
@@ -1662,7 +1662,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
16621662
try {
16631663
$stmt = $this->getPDO()->prepare($sql);
16641664
foreach ($binds as $key => $value) {
1665-
if ($key === ":internalId") {
1665+
if ($key === ":sequence") {
16661666
$stmt->bindValue($key, $value, PDO::PARAM_INT);
16671667
} else {
16681668
$stmt->bindValue($key, $value, $this->getPDOType($value));
@@ -1927,8 +1927,8 @@ protected function getSQLCondition(Query $query, array &$binds): string
19271927
default => $value
19281928
};
19291929
if ($attribute === $this->quote("_id")) {
1930-
$binds[":internalId"] = $value;
1931-
$conditions[] = "{$alias}.{$attribute} {$operator} :internalId";
1930+
$binds[":sequence"] = $value;
1931+
$conditions[] = "{$alias}.{$attribute} {$operator} :sequence";
19321932
} else {
19331933
$binds[":{$placeholder}_{$key}"] = $value;
19341934
$conditions[] = "{$alias}.{$attribute} {$operator} :{$placeholder}_{$key}";

src/Database/Adapter/SQL.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ public function updateDocuments(string $collection, Document $updates, array $do
441441
}
442442

443443
$name = $this->filter($collection);
444-
$sequences = \array_map(fn ($document) => $document->getInternalId(), $documents);
444+
$sequences = \array_map(fn ($document) => $document->getSequence(), $documents);
445445

446446
$sql = "
447447
UPDATE {$this->getSQLTable($name)}
@@ -716,7 +716,7 @@ public function deleteDocuments(string $collection, array $sequences, array $per
716716
* @return array<string>
717717
* @throws DatabaseException
718718
*/
719-
protected function getInternalIds(string $collection, array $documentIds, array $documentTenants = []): array
719+
protected function getSequences(string $collection, array $documentIds, array $documentTenants = []): array
720720
{
721721
$sequences = [];
722722

@@ -744,7 +744,7 @@ protected function getInternalIds(string $collection, array $documentIds, array
744744
}
745745

746746
$stmt->execute();
747-
$results = $stmt->fetchAll(\PDO::FETCH_KEY_PAIR); // Fetch as [documentId => internalId]
747+
$results = $stmt->fetchAll(\PDO::FETCH_KEY_PAIR); // Fetch as [documentId => sequence]
748748
$stmt->closeCursor();
749749

750750
$sequences = [...$sequences, ...$results];

src/Database/Adapter/SQLite.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ public function createDocument(string $collection, Document $document): Document
543543
}
544544

545545
// Insert manual id if set
546-
if (!empty($document->getInternalId())) {
546+
if (!empty($document->getSequence())) {
547547
$values[] = '_id';
548548
$columns[] = "_id";
549549
}
@@ -560,8 +560,8 @@ public function createDocument(string $collection, Document $document): Document
560560
$stmt->bindValue(':_uid', $document->getId(), PDO::PARAM_STR);
561561

562562
// Bind internal id if set
563-
if (!empty($document->getInternalId())) {
564-
$stmt->bindValue(':_id', $document->getInternalId(), PDO::PARAM_STR);
563+
if (!empty($document->getSequence())) {
564+
$stmt->bindValue(':_id', $document->getSequence(), PDO::PARAM_STR);
565565
}
566566

567567
$attributeIndex = 0;

0 commit comments

Comments
 (0)