Merge pull request #12417 from nextcloud/bugfix/noid/app-files_external-replace-insertIfNotExist

Fix UniqueConstraintViolationException while insert into files_external config tables
This commit is contained in:
Morris Jobke 2018-11-14 21:05:32 +01:00 committed by GitHub
commit 08bd8373f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 18 deletions

View File

@ -25,6 +25,7 @@
namespace OCA\Files_External\Service; namespace OCA\Files_External\Service;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\Security\ICrypto; use OCP\Security\ICrypto;
@ -300,12 +301,15 @@ class DBConfigService {
if ($key === 'password') { if ($key === 'password') {
$value = $this->encryptValue($value); $value = $this->encryptValue($value);
} }
$count = $this->connection->insertIfNotExist('*PREFIX*external_config', [
'mount_id' => $mountId, try {
'key' => $key, $builder = $this->connection->getQueryBuilder();
'value' => $value $builder->insert('external_config')
], ['mount_id', 'key']); ->setValue('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))
if ($count === 0) { ->setValue('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))
->setValue('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))
->execute();
} catch(UniqueConstraintViolationException $e) {
$builder = $this->connection->getQueryBuilder(); $builder = $this->connection->getQueryBuilder();
$query = $builder->update('external_config') $query = $builder->update('external_config')
->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)) ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))
@ -321,13 +325,14 @@ class DBConfigService {
* @param string $value * @param string $value
*/ */
public function setOption($mountId, $key, $value) { public function setOption($mountId, $key, $value) {
try {
$count = $this->connection->insertIfNotExist('*PREFIX*external_options', [ $builder = $this->connection->getQueryBuilder();
'mount_id' => $mountId, $builder->insert('external_options')
'key' => $key, ->setValue('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))
'value' => json_encode($value) ->setValue('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))
], ['mount_id', 'key']); ->setValue('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR))
if ($count === 0) { ->execute();
} catch(UniqueConstraintViolationException $e) {
$builder = $this->connection->getQueryBuilder(); $builder = $this->connection->getQueryBuilder();
$query = $builder->update('external_options') $query = $builder->update('external_options')
->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR)) ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR))
@ -338,11 +343,16 @@ class DBConfigService {
} }
public function addApplicable($mountId, $type, $value) { public function addApplicable($mountId, $type, $value) {
$this->connection->insertIfNotExist('*PREFIX*external_applicable', [ try {
'mount_id' => $mountId, $builder = $this->connection->getQueryBuilder();
'type' => $type, $builder->insert('external_applicable')
'value' => $value ->setValue('mount_id', $builder->createNamedParameter($mountId))
], ['mount_id', 'type', 'value']); ->setValue('type', $builder->createNamedParameter($type))
->setValue('value', $builder->createNamedParameter($value))
->execute();
} catch(UniqueConstraintViolationException $e) {
// applicable exists already
}
} }
public function removeApplicable($mountId, $type, $value) { public function removeApplicable($mountId, $type, $value) {