Issue #2996432 by andypost: Replace all calls to db_truncate, which is deprecated
parent
c16c5d0647
commit
0875ceba15
|
@ -266,6 +266,7 @@ function db_delete($table, array $options = []) {
|
||||||
* @see \Drupal\Core\Database\Connection::defaultOptions()
|
* @see \Drupal\Core\Database\Connection::defaultOptions()
|
||||||
*/
|
*/
|
||||||
function db_truncate($table, array $options = []) {
|
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') {
|
if (empty($options['target']) || $options['target'] == 'replica') {
|
||||||
$options['target'] = 'default';
|
$options['target'] = 'default';
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Drupal\Tests\statistics\Functional;
|
namespace Drupal\Tests\statistics\Functional;
|
||||||
|
|
||||||
|
use Drupal\Core\Database\Database;
|
||||||
use Drupal\Tests\BrowserTestBase;
|
use Drupal\Tests\BrowserTestBase;
|
||||||
use Drupal\node\Entity\Node;
|
use Drupal\node\Entity\Node;
|
||||||
|
|
||||||
|
@ -82,7 +83,7 @@ class StatisticsLoggingTest extends BrowserTestBase {
|
||||||
->save();
|
->save();
|
||||||
|
|
||||||
// Clear the logs.
|
// Clear the logs.
|
||||||
db_truncate('node_counter');
|
Database::getConnection()->truncate('node_counter');
|
||||||
$this->client = \Drupal::httpClient();
|
$this->client = \Drupal::httpClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ namespace Drupal\KernelTests\Core\Database;
|
||||||
use Drupal\Core\Database\Query\Condition;
|
use Drupal\Core\Database\Query\Condition;
|
||||||
use Drupal\Core\Database\Query\Delete;
|
use Drupal\Core\Database\Query\Delete;
|
||||||
use Drupal\Core\Database\Query\Merge;
|
use Drupal\Core\Database\Query\Merge;
|
||||||
|
use Drupal\Core\Database\Query\Truncate;
|
||||||
use Drupal\Core\Database\Query\Update;
|
use Drupal\Core\Database\Query\Update;
|
||||||
use Drupal\Core\Database\Transaction;
|
use Drupal\Core\Database\Transaction;
|
||||||
use Drupal\Core\Database\Database;
|
use Drupal\Core\Database\Database;
|
||||||
|
@ -376,4 +377,13 @@ class DatabaseLegacyTest extends DatabaseTestBase {
|
||||||
$this->assertInstanceOf(Delete::class, db_delete('test'));
|
$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 @@ class DeleteTruncateTest extends DatabaseTestBase {
|
||||||
$num_records_before = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
|
$num_records_before = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
|
||||||
$this->assertTrue($num_records_before > 0, 'The table is not empty.');
|
$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();
|
$num_records_after = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
|
||||||
$this->assertEqual(0, $num_records_after, 'Truncate really deletes everything.');
|
$this->assertEqual(0, $num_records_after, 'Truncate really deletes everything.');
|
||||||
|
|
|
@ -337,7 +337,7 @@ class TransactionTest extends DatabaseTestBase {
|
||||||
* Starts over for a new test.
|
* Starts over for a new test.
|
||||||
*/
|
*/
|
||||||
protected function cleanUp() {
|
protected function cleanUp() {
|
||||||
db_truncate('test')
|
$this->connection->truncate('test')
|
||||||
->execute();
|
->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue