diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index cc4acd8292..dd06390305 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -292,14 +292,16 @@ class Cache implements ICache { if ($builder->execute()) { $fileId = $builder->getLastInsertId(); - $query = $this->getQueryBuilder(); - $query->insert('filecache_extended'); + if (count($extensionValues)) { + $query = $this->getQueryBuilder(); + $query->insert('filecache_extended'); - $query->setValue('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)); - foreach ($extensionValues as $column => $value) { - $query->setValue($column, $query->createNamedParameter($value)); + $query->setValue('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)); + foreach ($extensionValues as $column => $value) { + $query->setValue($column, $query->createNamedParameter($value)); + } + $query->execute(); } - $query->execute(); $this->eventDispatcher->dispatch(CacheInsertEvent::class, new CacheInsertEvent($this->storage, $file, $fileId)); return $fileId; @@ -357,21 +359,33 @@ class Cache implements ICache { } if (count($extensionValues)) { - $query = $this->getQueryBuilder(); - $query->update('filecache_extended') - ->whereFileId($id) - ->andWhere($query->expr()->orX(...array_map(function ($key, $value) use ($query) { - return $query->expr()->orX( - $query->expr()->neq($key, $query->createNamedParameter($value)), - $query->expr()->isNull($key) - ); - }, array_keys($extensionValues), array_values($extensionValues)))); + try { + $query = $this->getQueryBuilder(); + $query->insert('filecache_extended'); - foreach ($extensionValues as $key => $value) { - $query->set($key, $query->createNamedParameter($value)); + $query->setValue('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)); + foreach ($extensionValues as $column => $value) { + $query->setValue($column, $query->createNamedParameter($value)); + } + + $query->execute(); + } catch (UniqueConstraintViolationException $e) { + $query = $this->getQueryBuilder(); + $query->update('filecache_extended') + ->whereFileId($id) + ->andWhere($query->expr()->orX(...array_map(function ($key, $value) use ($query) { + return $query->expr()->orX( + $query->expr()->neq($key, $query->createNamedParameter($value)), + $query->expr()->isNull($key) + ); + }, array_keys($extensionValues), array_values($extensionValues)))); + + foreach ($extensionValues as $key => $value) { + $query->set($key, $query->createNamedParameter($value)); + } + + $query->execute(); } - - $query->execute(); } $path = $this->getPathById($id); diff --git a/tests/lib/Files/Cache/CacheTest.php b/tests/lib/Files/Cache/CacheTest.php index 4ecccf2210..f6794cfa2f 100644 --- a/tests/lib/Files/Cache/CacheTest.php +++ b/tests/lib/Files/Cache/CacheTest.php @@ -718,7 +718,7 @@ class CacheTest extends \Test\TestCase { $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'metadata_etag' => 'foo']; $this->cache->put("foo3", $data); $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain']; - $this->cache->put("foo4", $data); + $id4 = $this->cache->put("foo4", $data); $entry = $this->cache->get($id1); $this->assertEquals(20, $entry->getCreationTime()); @@ -766,6 +766,13 @@ class CacheTest extends \Test\TestCase { $this->assertEquals(20, $entries[0]->getCreationTime()); $this->assertEquals(25, $entries[0]->getUploadTime()); $this->assertEquals(null, $entries[0]->getMetadataEtag()); + + $this->cache->update($id4, ['upload_time' => 25]); + + $entry = $this->cache->get($id4); + $this->assertEquals(0, $entry->getCreationTime()); + $this->assertEquals(25, $entry->getUploadTime()); + $this->assertEquals(null, $entry->getMetadataEtag()); } protected function tearDown() {