aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@owncloud.com>2015-05-11 12:37:14 +0200
committerJoas Schilling <nickvergessen@owncloud.com>2015-05-11 12:38:33 +0200
commitdfed287dc02ba7b0fb8fe16542f9e7235eae9223 (patch)
treeb37fdd7bfcc4c2129db642b32513dd4b0e979ed3
parent39497b9c3a02c383b2bd660c9114e65b732848e3 (diff)
downloadnextcloud-server-dfed287dc02ba7b0fb8fe16542f9e7235eae9223.tar.gz
nextcloud-server-dfed287dc02ba7b0fb8fe16542f9e7235eae9223.zip
Use insertIfNotExists to avoid problems with parallel calls
-rw-r--r--lib/private/appconfig.php27
-rw-r--r--tests/lib/appconfig.php10
2 files changed, 22 insertions, 15 deletions
diff --git a/lib/private/appconfig.php b/lib/private/appconfig.php
index a2095c571f3..37532616e1e 100644
--- a/lib/private/appconfig.php
+++ b/lib/private/appconfig.php
@@ -42,13 +42,14 @@
namespace OC;
-use \OC\DB\Connection;
+use OC\DB\Connection;
+use OCP\IAppConfig;
/**
* This class provides an easy way for apps to store config values in the
* database.
*/
-class AppConfig implements \OCP\IAppConfig {
+class AppConfig implements IAppConfig {
/**
* @var \OC\DB\Connection $conn
*/
@@ -64,7 +65,7 @@ class AppConfig implements \OCP\IAppConfig {
private $apps = null;
/**
- * @param \OC\DB\Connection $conn
+ * @param Connection $conn
*/
public function __construct(Connection $conn) {
$this->conn = $conn;
@@ -172,27 +173,31 @@ class AppConfig implements \OCP\IAppConfig {
}
/**
- * sets a value in the appconfig
+ * Sets a value. If the key did not exist before it will be created.
*
* @param string $app app
* @param string $key key
* @param string $value value
- *
- * Sets a value. If the key did not exist before it will be created.
+ * @return void
*/
public function setValue($app, $key, $value) {
+ $inserted = false;
// Does the key exist? no: insert, yes: update.
if (!$this->hasKey($app, $key)) {
- $data = array(
+ $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [
'appid' => $app,
'configkey' => $key,
'configvalue' => $value,
- );
- $this->conn->insert('*PREFIX*appconfig', $data);
- } else {
+ ], [
+ 'appid',
+ 'configkey',
+ ]);
+ }
+
+ if (!$inserted) {
$oldValue = $this->getValue($app, $key);
if($oldValue === strval($value)) {
- return true;
+ return;
}
$data = array(
'configvalue' => $value,
diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php
index 99ec8c2382a..adff45706cc 100644
--- a/tests/lib/appconfig.php
+++ b/tests/lib/appconfig.php
@@ -215,7 +215,7 @@ class Test_Appconfig extends \Test\TestCase {
.' WHERE `appid` = ?'), $this->equalTo(array('bar')))
->will($this->returnValue($statementMock));
$connectionMock->expects($this->once())
- ->method('insert')
+ ->method('insertIfNotExist')
->with($this->equalTo('*PREFIX*appconfig'),
$this->equalTo(
array(
@@ -223,7 +223,8 @@ class Test_Appconfig extends \Test\TestCase {
'configkey' => 'foo',
'configvalue' => 'v1',
)
- ));
+ ), $this->equalTo(['appid', 'configkey']))
+ ->willReturn(1);
$connectionMock->expects($this->never())
->method('update');
@@ -246,7 +247,7 @@ class Test_Appconfig extends \Test\TestCase {
.' WHERE `appid` = ?'), $this->equalTo(array('bar')))
->will($this->returnValue($statementMock));
$connectionMock->expects($this->once())
- ->method('insert')
+ ->method('insertIfNotExist')
->with($this->equalTo('*PREFIX*appconfig'),
$this->equalTo(
array(
@@ -254,7 +255,8 @@ class Test_Appconfig extends \Test\TestCase {
'configkey' => 'foo',
'configvalue' => 'v1',
)
- ));
+ ), $this->equalTo(['appid', 'configkey']))
+ ->willReturn(1);
$connectionMock->expects($this->once())
->method('update')
->with($this->equalTo('*PREFIX*appconfig'),