Skip to content

Commit

Permalink
Merge pull request #12411 from nextcloud/bugfix/6160/oc_filecache-uni…
Browse files Browse the repository at this point in the history
…que-constraint

Fix UniqueConstraintViolationException while insert into oc_filecache
  • Loading branch information
MorrisJobke authored Nov 12, 2018
2 parents 237c70d + 93c62d7 commit dcf6861
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

namespace OC\Files\Cache;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Driver\Statement;
use OCP\Files\Cache\ICache;
Expand Down Expand Up @@ -238,6 +239,8 @@ public function put($file, array $data) {
*
* @return int file id
* @throws \RuntimeException
*
* @suppress SqlInjectionChecker
*/
public function insert($file, array $data) {
// normalize file
Expand Down Expand Up @@ -268,20 +271,28 @@ public function insert($file, array $data) {
return trim($item, "`");
}, $queryParts);
$values = array_combine($queryParts, $params);
if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
'storage',
'path_hash',
])
) {
return (int)$this->connection->lastInsertId('*PREFIX*filecache');

try {
$builder = $this->connection->getQueryBuilder();
$builder->insert('filecache');

foreach ($values as $column => $value) {
$builder->setValue($column, $builder->createNamedParameter($value));
}

if ($builder->execute()) {
return (int)$this->connection->lastInsertId('*PREFIX*filecache');
}
} catch(UniqueConstraintViolationException $e) {
// entry exists already
}

// The file was created in the mean time
if (($id = $this->getId($file)) > -1) {
$this->update($id, $data);
return $id;
} else {
throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.');
throw new \RuntimeException('File entry could not be inserted but could also not be selected with getId() in order to perform an update. Please try again.');
}
}

Expand Down

0 comments on commit dcf6861

Please sign in to comment.