Skip to content

Commit

Permalink
Merge pull request #514 from utopia-php/feat-batch-upsert
Browse files Browse the repository at this point in the history
Remove redundant method
  • Loading branch information
abnegate authored Jan 23, 2025
2 parents 6ec27ad + 938e699 commit bd6f080
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 119 deletions.
4 changes: 1 addition & 3 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,19 +640,17 @@ abstract public function updateDocuments(string $collection, Document $updates,
/**
* Create documents if they do not exist, otherwise update them.
*
* If both attribute and value are not empty, only the specified attribute will be increased, by the provided value.
* If attribute is not empty, only the specified attribute will be increased, by the new value in each document.
*
* @param string $collection
* @param string $attribute
* @param int|float $value
* @param array<Document> $documents
* @param int $batchSize
* @return array<Document>
*/
abstract public function createOrUpdateDocuments(
string $collection,
string $attribute,
int|float $value,
array $documents,
int $batchSize
): array;
Expand Down
13 changes: 1 addition & 12 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,6 @@ public function updateDocuments(string $collection, Document $updates, array $do
/**
* @param string $collection
* @param string $attribute
* @param int|float $value
* @param array<Document> $documents
* @param int $batchSize
* @return array<Document>
Expand All @@ -1603,7 +1602,6 @@ public function updateDocuments(string $collection, Document $updates, array $do
public function createOrUpdateDocuments(
string $collection,
string $attribute,
int|float $value,
array $documents,
int $batchSize
): array {
Expand Down Expand Up @@ -1664,12 +1662,7 @@ public function createOrUpdateDocuments(
$batchKeys[] = '(' . \implode(', ', $bindKeys) . ')';
}

if (!empty($attribute) && !empty($value)) {
// Increment specific column by the specified value
$updateColumns = [
"`{$attribute}` = `{$attribute}` + :_increment"
];
} elseif (!empty($attribute) && empty($value)) {
if (!empty($attribute)) {
// Increment specific column by its new value in place
$updateColumns = [
"`{$attribute}` = `{$attribute}` + VALUES(`{$attribute}`)"
Expand All @@ -1694,10 +1687,6 @@ public function createOrUpdateDocuments(
$stmt->bindValue($key, $binding, $this->getPDOType($binding));
}

if (!empty($attribute) && !empty($value)) {
$stmt->bindValue(':_increment', $value, $this->getPDOType($value));
}

$stmt->execute();

// Fetch existing permissions in bulk after data updates
Expand Down
3 changes: 1 addition & 2 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,11 @@ public function updateDocuments(string $collection, Document $updates, array $do
/**
* @param string $collection
* @param string $attribute
* @param float|int $value
* @param array<Document> $documents
* @param int $batchSize
* @return array<Document>
*/
public function createOrUpdateDocuments(string $collection, string $attribute, float|int $value, array $documents, int $batchSize): array
public function createOrUpdateDocuments(string $collection, string $attribute, array $documents, int $batchSize): array
{
return $documents;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1608,12 +1608,11 @@ public function updateDocuments(string $collection, Document $updates, array $do
/**
* @param string $collection
* @param string $attribute
* @param float|int $value
* @param array<Document> $documents
* @param int $batchSize
* @return array<Document>
*/
public function createOrUpdateDocuments(string $collection, string $attribute, float|int $value, array $documents, int $batchSize): array
public function createOrUpdateDocuments(string $collection, string $attribute, array $documents, int $batchSize): array
{
return $documents;
}
Expand Down
36 changes: 4 additions & 32 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ function (mixed $value) {
$value = new \DateTime($value);
$value->setTimezone(new \DateTimeZone(date_default_timezone_get()));
return DateTime::format($value);
} catch (\Throwable $th) {
} catch (\Throwable) {
return $value;
}
},
Expand Down Expand Up @@ -4536,7 +4536,7 @@ private function getJunctionCollection(Document $collection, Document $relatedCo
}

/**
* Create or update documents
* Create or update documents.
*
* @param string $collection
* @param array<Document> $documents
Expand All @@ -4552,14 +4552,13 @@ public function createOrUpdateDocuments(
return $this->createOrUpdateDocumentsWithIncrease(
$collection,
'',
0,
$documents,
$batchSize
);
}

/**
* Create or update documents
* Create or update documents, increasing the value of the given attribute by the value in each document.
*
* @param string $collection
* @param string $attribute
Expand All @@ -4568,37 +4567,11 @@ public function createOrUpdateDocuments(
* @return array<Document>
* @throws StructureException
* @throws \Throwable
*/
public function createOrUpdateDocumentsWithInplaceIncrease(
string $collection,
string $attribute,
array $documents,
int $batchSize = self::INSERT_BATCH_SIZE
): array {
return $this->createOrUpdateDocumentsWithIncrease(
$collection,
$attribute,
0,
$documents,
$batchSize
);
}

/**
* @param string $collection
* @param string $attribute
* @param int|float $value
* @param array<Document> $documents
* @param int $batchSize
* @return array<Document>
* @throws StructureException
* @throws \Throwable
* @throws Exception
*/
public function createOrUpdateDocumentsWithIncrease(
string $collection,
string $attribute,
int|float $value,
array $documents,
int $batchSize = self::INSERT_BATCH_SIZE
): array {
Expand Down Expand Up @@ -4652,11 +4625,10 @@ public function createOrUpdateDocumentsWithIncrease(
$documents[$key] = $document;
}

$documents = $this->withTransaction(function () use ($collection, $attribute, $value, $documents, $batchSize) {
$documents = $this->withTransaction(function () use ($collection, $attribute, $documents, $batchSize) {
return $this->adapter->createOrUpdateDocuments(
$collection->getId(),
$attribute,
$value,
$documents,
$batchSize,
);
Expand Down
69 changes: 1 addition & 68 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2341,73 +2341,6 @@ public function testCreateOrUpdateDocumentsWithIncrease(): void
return;
}

$collection = 'testCreateOrUpdateWithIncrease';

static::getDatabase()->createCollection($collection);
static::getDatabase()->createAttribute($collection, 'string', Database::VAR_STRING, 128, true);
static::getDatabase()->createAttribute($collection, 'integer', Database::VAR_INTEGER, 0, true);

$documents = [
new Document([
'$id' => 'first',
'string' => 'text📝',
'integer' => 5,
'$permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
]),
new Document([
'$id' => 'second',
'string' => 'text📝',
'integer' => 5,
'$permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
]),
];

static::getDatabase()->createDocuments($collection, $documents);

static::getDatabase()->createOrUpdateDocumentsWithIncrease(
collection: $collection,
attribute:'integer',
value: 1,
documents: $documents
);

$documents = static::getDatabase()->find($collection);

foreach ($documents as $document) {
$this->assertEquals(6, $document->getAttribute('integer'));
}

static::getDatabase()->createOrUpdateDocumentsWithIncrease(
collection: $collection,
attribute:'integer',
value: -1,
documents: $documents
);

$documents = static::getDatabase()->find($collection);

foreach ($documents as $document) {
$this->assertEquals(5, $document->getAttribute('integer'));
}
}

public function testCreateOrUpdateDocumentsWithInplaceIncrease(): void
{
if (!static::getDatabase()->getAdapter()->getSupportForUpserts()) {
$this->expectNotToPerformAssertions();
return;
}

$collection = 'testCreateOrUpdateInplace';

static::getDatabase()->createCollection($collection);
Expand Down Expand Up @@ -2444,7 +2377,7 @@ public function testCreateOrUpdateDocumentsWithInplaceIncrease(): void
$documents[0]->setAttribute('integer', 1);
$documents[1]->setAttribute('integer', 1);

static::getDatabase()->createOrUpdateDocumentsWithInplaceIncrease(
static::getDatabase()->createOrUpdateDocumentsWithIncrease(
collection: $collection,
attribute:'integer',
documents: $documents
Expand Down

0 comments on commit bd6f080

Please sign in to comment.