Skip to content

Commit

Permalink
Catch UniqueConstraintViolationException inside insertIfNotExist
Browse files Browse the repository at this point in the history
This is the most common case for the usage of this method.

See also #12369 and the linked tickets.

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
  • Loading branch information
MorrisJobke committed Nov 9, 2018
1 parent da57aaf commit 230e93f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
16 changes: 14 additions & 2 deletions lib/private/DB/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

namespace OC\DB;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;

/**
* This handles the way we use to write queries, into something that can be
* handled by the database abstraction layer.
Expand Down Expand Up @@ -79,7 +81,9 @@ public function unlockTable() {
}

/**
* Insert a row if the matching row does not exists.
* Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
* it is needed that there is also a unique constraint on the values. Then this method will
* catch the exception and return 0.
*
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
* @param array $input data that should be inserted into the table (column name => value)
Expand Down Expand Up @@ -111,6 +115,14 @@ public function insertIfNotExist($table, $input, array $compare = null) {
$query = substr($query, 0, -5);
$query .= ' HAVING COUNT(*) = 0';

return $this->conn->executeUpdate($query, $inserts);
try {
return $this->conn->executeUpdate($query, $inserts);
} catch (UniqueConstraintViolationException $e) {
// if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it
// it's fine to ignore this then
//
// more discussions about this can be found at https://github.com/nextcloud/server/pull/12315
return 0;
}
}
}
16 changes: 14 additions & 2 deletions lib/private/DB/AdapterSqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

namespace OC\DB;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;

class AdapterSqlite extends Adapter {

/**
Expand All @@ -50,7 +52,9 @@ public function fixupStatement($statement) {
}

/**
* Insert a row if the matching row does not exists.
* Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
* it is needed that there is also a unique constraint on the values. Then this method will
* catch the exception and return 0.
*
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
* @param array $input data that should be inserted into the table (column name => value)
Expand Down Expand Up @@ -82,6 +86,14 @@ public function insertIfNotExist($table, $input, array $compare = null) {
$query = substr($query, 0, -5);
$query .= ')';

return $this->conn->executeUpdate($query, $inserts);
try {
return $this->conn->executeUpdate($query, $inserts);
} catch (UniqueConstraintViolationException $e) {
// if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it
// it's fine to ignore this then
//
// more discussions about this can be found at https://github.com/nextcloud/server/pull/12315
return 0;
}
}
}
4 changes: 3 additions & 1 deletion lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ public function realLastInsertId($seqName = null) {
}

/**
* Insert a row if the matching row does not exists.
* Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
* it is needed that there is also a unique constraint on the values. Then this method will
* catch the exception and return 0.
*
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
* @param array $input data that should be inserted into the table (column name => value)
Expand Down
4 changes: 3 additions & 1 deletion lib/public/IDBConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public function executeUpdate($query, array $params = array(), array $types = ar
public function lastInsertId($table = null);

/**
* Insert a row if the matching row does not exists.
* Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
* it is needed that there is also a unique constraint on the values. Then this method will
* catch the exception and return 0.
*
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
* @param array $input data that should be inserted into the table (column name => value)
Expand Down

0 comments on commit 230e93f

Please sign in to comment.