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

8.7.x
Nathaniel Catchpole 2018-09-13 16:14:03 +01:00
parent c16c5d0647
commit 0875ceba15
5 changed files with 15 additions and 3 deletions

View File

@ -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';
}

View File

@ -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 @@ class StatisticsLoggingTest extends BrowserTestBase {
->save();
// Clear the logs.
db_truncate('node_counter');
Database::getConnection()->truncate('node_counter');
$this->client = \Drupal::httpClient();
}

View File

@ -5,6 +5,7 @@ namespace Drupal\KernelTests\Core\Database;
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 @@ class DatabaseLegacyTest extends DatabaseTestBase {
$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'));
}
}

View File

@ -60,7 +60,7 @@ class DeleteTruncateTest extends DatabaseTestBase {
$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.');

View File

@ -337,7 +337,7 @@ class TransactionTest extends DatabaseTestBase {
* Starts over for a new test.
*/
protected function cleanUp() {
db_truncate('test')
$this->connection->truncate('test')
->execute();
}