Skip to content

Commit c34cb23

Browse files
authored
Merge pull request #754 from utopia-php/sync-3.x
Sync 3.x
2 parents da9d021 + b1ba883 commit c34cb23

File tree

5 files changed

+264
-62
lines changed

5 files changed

+264
-62
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,12 +1312,12 @@ public function increaseDocumentAttribute(
13121312
$name = $this->filter($collection);
13131313
$attribute = $this->filter($attribute);
13141314

1315-
$sqlMax = $max ? " AND `{$attribute}` <= {$max}" : '';
1316-
$sqlMin = $min ? " AND `{$attribute}` >= {$min}" : '';
1315+
$sqlMax = $max !== null ? " AND `{$attribute}` <= :max" : '';
1316+
$sqlMin = $min !== null ? " AND `{$attribute}` >= :min" : '';
13171317

13181318
$sql = "
1319-
UPDATE {$this->getSQLTable($name)}
1320-
SET
1319+
UPDATE {$this->getSQLTable($name)}
1320+
SET
13211321
`{$attribute}` = `{$attribute}` + :val,
13221322
`_updatedAt` = :updatedAt
13231323
WHERE _uid = :_uid
@@ -1333,6 +1333,12 @@ public function increaseDocumentAttribute(
13331333
$stmt->bindValue(':val', $value);
13341334
$stmt->bindValue(':updatedAt', $updatedAt);
13351335

1336+
if ($max !== null) {
1337+
$stmt->bindValue(':max', $max);
1338+
}
1339+
if ($min !== null) {
1340+
$stmt->bindValue(':min', $min);
1341+
}
13361342
if ($this->sharedTables) {
13371343
$stmt->bindValue(':_tenant', $this->tenant);
13381344
}

src/Database/Adapter/Mongo.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,12 +1706,14 @@ public function increaseDocumentAttribute(string $collection, string $id, string
17061706
$filters['_tenant'] = $this->getTenantFilters($collection);
17071707
}
17081708

1709-
if ($max) {
1710-
$filters[$attribute] = ['$lte' => $max];
1711-
}
1712-
1713-
if ($min) {
1714-
$filters[$attribute] = ['$gte' => $min];
1709+
if ($max !== null || $min !== null) {
1710+
$filters[$attribute] = [];
1711+
if ($max !== null) {
1712+
$filters[$attribute]['$lte'] = $max;
1713+
}
1714+
if ($min !== null) {
1715+
$filters[$attribute]['$gte'] = $min;
1716+
}
17151717
}
17161718

17171719
$options = $this->getTransactionOptions();

src/Database/Adapter/Postgres.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,12 +1433,12 @@ public function increaseDocumentAttribute(string $collection, string $id, string
14331433
$name = $this->filter($collection);
14341434
$attribute = $this->filter($attribute);
14351435

1436-
$sqlMax = $max ? " AND \"{$attribute}\" <= {$max}" : "";
1437-
$sqlMin = $min ? " AND \"{$attribute}\" >= {$min}" : "";
1436+
$sqlMax = $max !== null ? " AND \"{$attribute}\" <= :max" : "";
1437+
$sqlMin = $min !== null ? " AND \"{$attribute}\" >= :min" : "";
14381438

14391439
$sql = "
1440-
UPDATE {$this->getSQLTable($name)}
1441-
SET
1440+
UPDATE {$this->getSQLTable($name)}
1441+
SET
14421442
\"{$attribute}\" = \"{$attribute}\" + :val,
14431443
\"_updatedAt\" = :updatedAt
14441444
WHERE _uid = :_uid
@@ -1454,6 +1454,12 @@ public function increaseDocumentAttribute(string $collection, string $id, string
14541454
$stmt->bindValue(':val', $value);
14551455
$stmt->bindValue(':updatedAt', $updatedAt);
14561456

1457+
if ($max !== null) {
1458+
$stmt->bindValue(':max', $max);
1459+
}
1460+
if ($min !== null) {
1461+
$stmt->bindValue(':min', $min);
1462+
}
14571463
if ($this->sharedTables) {
14581464
$stmt->bindValue(':_tenant', $this->tenant);
14591465
}

src/Database/Database.php

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4464,15 +4464,17 @@ public function createDocument(string $collection, Document $document): Document
44644464
}
44654465
}
44664466

4467-
$structure = new Structure(
4468-
$collection,
4469-
$this->adapter->getIdAttributeType(),
4470-
$this->adapter->getMinDateTime(),
4471-
$this->adapter->getMaxDateTime(),
4472-
$this->adapter->getSupportForAttributes()
4473-
);
4474-
if (!$structure->isValid($document)) {
4475-
throw new StructureException($structure->getDescription());
4467+
if ($this->validate) {
4468+
$structure = new Structure(
4469+
$collection,
4470+
$this->adapter->getIdAttributeType(),
4471+
$this->adapter->getMinDateTime(),
4472+
$this->adapter->getMaxDateTime(),
4473+
$this->adapter->getSupportForAttributes()
4474+
);
4475+
if (!$structure->isValid($document)) {
4476+
throw new StructureException($structure->getDescription());
4477+
}
44764478
}
44774479

44784480
$document = $this->adapter->castingBefore($collection, $document);
@@ -4565,15 +4567,17 @@ public function createDocuments(
45654567

45664568
$document = $this->encode($collection, $document);
45674569

4568-
$validator = new Structure(
4569-
$collection,
4570-
$this->adapter->getIdAttributeType(),
4571-
$this->adapter->getMinDateTime(),
4572-
$this->adapter->getMaxDateTime(),
4573-
$this->adapter->getSupportForAttributes()
4574-
);
4575-
if (!$validator->isValid($document)) {
4576-
throw new StructureException($validator->getDescription());
4570+
if ($this->validate) {
4571+
$validator = new Structure(
4572+
$collection,
4573+
$this->adapter->getIdAttributeType(),
4574+
$this->adapter->getMinDateTime(),
4575+
$this->adapter->getMaxDateTime(),
4576+
$this->adapter->getSupportForAttributes()
4577+
);
4578+
if (!$validator->isValid($document)) {
4579+
throw new StructureException($validator->getDescription());
4580+
}
45774581
}
45784582

45794583
if ($this->resolveRelationships) {
@@ -5127,16 +5131,18 @@ public function updateDocument(string $collection, string $id, Document $documen
51275131

51285132
$document = $this->encode($collection, $document);
51295133

5130-
$structureValidator = new Structure(
5131-
$collection,
5132-
$this->adapter->getIdAttributeType(),
5133-
$this->adapter->getMinDateTime(),
5134-
$this->adapter->getMaxDateTime(),
5135-
$this->adapter->getSupportForAttributes(),
5136-
$old
5137-
);
5138-
if (!$structureValidator->isValid($document)) { // Make sure updated structure still apply collection rules (if any)
5139-
throw new StructureException($structureValidator->getDescription());
5134+
if ($this->validate) {
5135+
$structureValidator = new Structure(
5136+
$collection,
5137+
$this->adapter->getIdAttributeType(),
5138+
$this->adapter->getMinDateTime(),
5139+
$this->adapter->getMaxDateTime(),
5140+
$this->adapter->getSupportForAttributes(),
5141+
$old
5142+
);
5143+
if (!$structureValidator->isValid($document)) { // Make sure updated structure still apply collection rules (if any)
5144+
throw new StructureException($structureValidator->getDescription());
5145+
}
51405146
}
51415147

51425148
if ($this->resolveRelationships) {
@@ -5282,17 +5288,19 @@ public function updateDocuments(
52825288
applyDefaults: false
52835289
);
52845290

5285-
$validator = new PartialStructure(
5286-
$collection,
5287-
$this->adapter->getIdAttributeType(),
5288-
$this->adapter->getMinDateTime(),
5289-
$this->adapter->getMaxDateTime(),
5290-
$this->adapter->getSupportForAttributes(),
5291-
null // No old document available in bulk updates
5292-
);
5291+
if ($this->validate) {
5292+
$validator = new PartialStructure(
5293+
$collection,
5294+
$this->adapter->getIdAttributeType(),
5295+
$this->adapter->getMinDateTime(),
5296+
$this->adapter->getMaxDateTime(),
5297+
$this->adapter->getSupportForAttributes(),
5298+
null // No old document available in bulk updates
5299+
);
52935300

5294-
if (!$validator->isValid($updates)) {
5295-
throw new StructureException($validator->getDescription());
5301+
if (!$validator->isValid($updates)) {
5302+
throw new StructureException($validator->getDescription());
5303+
}
52965304
}
52975305

52985306
$originalLimit = $limit;
@@ -6046,17 +6054,19 @@ public function upsertDocumentsWithIncrease(
60466054
}
60476055
}
60486056

6049-
$validator = new Structure(
6050-
$collection,
6051-
$this->adapter->getIdAttributeType(),
6052-
$this->adapter->getMinDateTime(),
6053-
$this->adapter->getMaxDateTime(),
6054-
$this->adapter->getSupportForAttributes(),
6055-
$old->isEmpty() ? null : $old
6056-
);
6057+
if ($this->validate) {
6058+
$validator = new Structure(
6059+
$collection,
6060+
$this->adapter->getIdAttributeType(),
6061+
$this->adapter->getMinDateTime(),
6062+
$this->adapter->getMaxDateTime(),
6063+
$this->adapter->getSupportForAttributes(),
6064+
$old->isEmpty() ? null : $old
6065+
);
60576066

6058-
if (!$validator->isValid($document)) {
6059-
throw new StructureException($validator->getDescription());
6067+
if (!$validator->isValid($document)) {
6068+
throw new StructureException($validator->getDescription());
6069+
}
60606070
}
60616071

60626072
$document = $this->encode($collection, $document);

0 commit comments

Comments
 (0)