Commit 4535a814 authored by catch's avatar catch

Issue #1839998 by wiifm, mcm.guaba, Josh Waihi: Fixed TruncateQuery...

Issue #1839998 by wiifm, mcm.guaba, Josh Waihi: Fixed TruncateQuery implemented as 'DELETE FROM' in MySQL and SQLite, but not PostgreSQL, causing nefarious table locking.
parent 6c07071c
......@@ -9,18 +9,4 @@
use Drupal\Core\Database\Query\Truncate as QueryTruncate;
class Truncate extends QueryTruncate {
public function __toString() {
// TRUNCATE is actually a DDL statement on MySQL, and DDL statements are
// not transactional, and result in an implicit COMMIT. When we are in a
// transaction, fallback to the slower, but transactional, DELETE.
if ($this->connection->inTransaction()) {
// Create a comment string to prepend to the query.
$comments = $this->connection->makeComment($this->comments);
return $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '}';
}
else {
return parent::__toString();
}
}
}
class Truncate extends QueryTruncate { }
......@@ -22,4 +22,4 @@ public function __toString() {
return $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
}
}
\ No newline at end of file
}
......@@ -73,6 +73,16 @@ public function __toString() {
// Create a sanitized comment string to prepend to the query.
$comments = $this->connection->makeComment($this->comments);
return $comments . 'TRUNCATE {' . $this->connection->escapeTable($this->table) . '} ';
// In most cases, TRUNCATE is not a transaction safe statement as it is a
// DDL statement which results in an implicit COMMIT. When we are in a
// transaction, fallback to the slower, but transactional, DELETE.
// PostgreSQL also locks the entire table for a TRUNCATE strongly reducing
// the concurrency with other transactions.
if ($this->connection->inTransaction()) {
return $comments . 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '}';
}
else {
return $comments . 'TRUNCATE {' . $this->connection->escapeTable($this->table) . '} ';
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment