Skip to content

Commit

Permalink
fix: use insert ignore for filecache_extended
Browse files Browse the repository at this point in the history
The current approach is to insert a new row and, if a unique constraint violation occurs, update an existing one. PostgreSQL logs the unique constraint violation as error "duplicate key value violates unique constraint".

Our Adapter.insertIgnoreConflict method provides a way to run an insert query without logging such errors by using the vendor-specific sql extensions like "on conflict do nothing" on Postgres.

Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb committed Oct 7, 2024
1 parent bbb6cb2 commit af89838
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,12 @@ public function update($id, array $data) {
}

if (count($extensionValues)) {
try {
$query = $this->getQueryBuilder();
$query->insert('filecache_extended');
$query->hintShardKey('storage', $this->getNumericStorageId());

$query->setValue('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT));
foreach ($extensionValues as $column => $value) {
$query->setValue($column, $query->createNamedParameter($value));
}
$insertCount = $this->connection->insertIgnoreConflict(
'filecache_extended',
array_merge(['fileid' => $id], $extensionValues)
);

$query->execute();
} catch (UniqueConstraintViolationException $e) {
if ($insertCount === 0) {
$query = $this->getQueryBuilder();
$query->update('filecache_extended')
->whereFileId($id)
Expand All @@ -386,7 +380,7 @@ public function update($id, array $data) {
$query->set($key, $query->createNamedParameter($value));
}

$query->execute();
$query->executeStatement();
}
}

Expand Down

0 comments on commit af89838

Please sign in to comment.