Use insertIfNotExists to avoid problems with parallel calls

This commit is contained in:
Joas Schilling 2015-05-11 12:37:14 +02:00
parent 39497b9c3a
commit dfed287dc0
2 changed files with 22 additions and 15 deletions

View File

@ -42,13 +42,14 @@
namespace OC; 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 * This class provides an easy way for apps to store config values in the
* database. * database.
*/ */
class AppConfig implements \OCP\IAppConfig { class AppConfig implements IAppConfig {
/** /**
* @var \OC\DB\Connection $conn * @var \OC\DB\Connection $conn
*/ */
@ -64,7 +65,7 @@ class AppConfig implements \OCP\IAppConfig {
private $apps = null; private $apps = null;
/** /**
* @param \OC\DB\Connection $conn * @param Connection $conn
*/ */
public function __construct(Connection $conn) { public function __construct(Connection $conn) {
$this->conn = $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 $app app
* @param string $key key * @param string $key key
* @param string $value value * @param string $value value
* * @return void
* Sets a value. If the key did not exist before it will be created.
*/ */
public function setValue($app, $key, $value) { public function setValue($app, $key, $value) {
$inserted = false;
// Does the key exist? no: insert, yes: update. // Does the key exist? no: insert, yes: update.
if (!$this->hasKey($app, $key)) { if (!$this->hasKey($app, $key)) {
$data = array( $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [
'appid' => $app, 'appid' => $app,
'configkey' => $key, 'configkey' => $key,
'configvalue' => $value, 'configvalue' => $value,
); ], [
$this->conn->insert('*PREFIX*appconfig', $data); 'appid',
} else { 'configkey',
]);
}
if (!$inserted) {
$oldValue = $this->getValue($app, $key); $oldValue = $this->getValue($app, $key);
if($oldValue === strval($value)) { if($oldValue === strval($value)) {
return true; return;
} }
$data = array( $data = array(
'configvalue' => $value, 'configvalue' => $value,

View File

@ -215,7 +215,7 @@ class Test_Appconfig extends \Test\TestCase {
.' WHERE `appid` = ?'), $this->equalTo(array('bar'))) .' WHERE `appid` = ?'), $this->equalTo(array('bar')))
->will($this->returnValue($statementMock)); ->will($this->returnValue($statementMock));
$connectionMock->expects($this->once()) $connectionMock->expects($this->once())
->method('insert') ->method('insertIfNotExist')
->with($this->equalTo('*PREFIX*appconfig'), ->with($this->equalTo('*PREFIX*appconfig'),
$this->equalTo( $this->equalTo(
array( array(
@ -223,7 +223,8 @@ class Test_Appconfig extends \Test\TestCase {
'configkey' => 'foo', 'configkey' => 'foo',
'configvalue' => 'v1', 'configvalue' => 'v1',
) )
)); ), $this->equalTo(['appid', 'configkey']))
->willReturn(1);
$connectionMock->expects($this->never()) $connectionMock->expects($this->never())
->method('update'); ->method('update');
@ -246,7 +247,7 @@ class Test_Appconfig extends \Test\TestCase {
.' WHERE `appid` = ?'), $this->equalTo(array('bar'))) .' WHERE `appid` = ?'), $this->equalTo(array('bar')))
->will($this->returnValue($statementMock)); ->will($this->returnValue($statementMock));
$connectionMock->expects($this->once()) $connectionMock->expects($this->once())
->method('insert') ->method('insertIfNotExist')
->with($this->equalTo('*PREFIX*appconfig'), ->with($this->equalTo('*PREFIX*appconfig'),
$this->equalTo( $this->equalTo(
array( array(
@ -254,7 +255,8 @@ class Test_Appconfig extends \Test\TestCase {
'configkey' => 'foo', 'configkey' => 'foo',
'configvalue' => 'v1', 'configvalue' => 'v1',
) )
)); ), $this->equalTo(['appid', 'configkey']))
->willReturn(1);
$connectionMock->expects($this->once()) $connectionMock->expects($this->once())
->method('update') ->method('update')
->with($this->equalTo('*PREFIX*appconfig'), ->with($this->equalTo('*PREFIX*appconfig'),