Commit 0875ceba authored by catch's avatar catch

Issue #2996432 by andypost: Replace all calls to db_truncate, which is deprecated

parent c16c5d06
......@@ -266,6 +266,7 @@ function db_delete($table, array $options = []) {
* @see \Drupal\Core\Database\Connection::defaultOptions()
*/
function db_truncate($table, array $options = []) {
@trigger_error('db_truncate() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call truncate() on it. For example, $injected_database->truncate($table, $options). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
if (empty($options['target']) || $options['target'] == 'replica') {
$options['target'] = 'default';
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\statistics\Functional;
use Drupal\Core\Database\Database;
use Drupal\Tests\BrowserTestBase;
use Drupal\node\Entity\Node;
......@@ -82,7 +83,7 @@ protected function setUp() {
->save();
// Clear the logs.
db_truncate('node_counter');
Database::getConnection()->truncate('node_counter');
$this->client = \Drupal::httpClient();
}
......
......@@ -5,6 +5,7 @@
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\Delete;
use Drupal\Core\Database\Query\Merge;
use Drupal\Core\Database\Query\Truncate;
use Drupal\Core\Database\Query\Update;
use Drupal\Core\Database\Transaction;
use Drupal\Core\Database\Database;
......@@ -376,4 +377,13 @@ public function testDbDelete() {
$this->assertInstanceOf(Delete::class, db_delete('test'));
}
/**
* Tests deprecation of the db_truncate() function.
*
* @expectedDeprecation db_truncate() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call truncate() on it. For example, $injected_database->truncate($table, $options). See https://www.drupal.org/node/2993033
*/
public function testDbTruncate() {
$this->assertInstanceOf(Truncate::class, db_truncate('test'));
}
}
......@@ -60,7 +60,7 @@ public function testTruncate() {
$num_records_before = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$this->assertTrue($num_records_before > 0, 'The table is not empty.');
db_truncate('test')->execute();
$this->connection->truncate('test')->execute();
$num_records_after = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
$this->assertEqual(0, $num_records_after, 'Truncate really deletes everything.');
......
......@@ -337,7 +337,7 @@ protected function executeDDLStatement() {
* Starts over for a new test.
*/
protected function cleanUp() {
db_truncate('test')
$this->connection->truncate('test')
->execute();
}
......
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