Commit 84bde9d6 authored by catch's avatar catch

Issue #2850033 by andypost, jeetendrakumar, Sharique, pk188, piggito, voleger,...

Issue #2850033 by andypost, jeetendrakumar, Sharique, pk188, piggito, voleger, gaurav.kapoor, yogeshmpawar, xjm: Replace all calls to db_delete, which is deprecated
parent bb7c0570
......@@ -237,6 +237,9 @@ function db_update($table, array $options = []) {
* @see \Drupal\Core\Database\Connection::defaultOptions()
*/
function db_delete($table, array $options = []) {
@trigger_error('db_delete 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 delete() on it. For example, $injected_database->delete($table, $options). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
// @todo Move away from here setting default target connection,
// https://www.drupal.org/node/2947775
if (empty($options['target']) || $options['target'] == 'replica') {
$options['target'] = 'default';
}
......
......@@ -610,7 +610,7 @@ public function nextIdDelete() {
// counter.
try {
$max_id = $this->query('SELECT MAX(value) FROM {sequences}')->fetchField();
// We know we are using MySQL here, no need for the slower db_delete().
// We know we are using MySQL here, no need for the slower ::delete().
$this->query('DELETE FROM {sequences} WHERE value < :value', [':value' => $max_id]);
}
// During testing, this function is called from shutdown with the
......
......@@ -1342,7 +1342,7 @@ function hook_ENTITY_TYPE_predelete(Drupal\Core\Entity\EntityInterface $entity)
*/
function hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
// Delete the entity's entry from a fictional table of all entities.
db_delete('example_entity')
\Drupal::database()->delete('example_entity')
->condition('type', $entity->getEntityTypeId())
->condition('id', $entity->id())
->execute();
......@@ -1361,7 +1361,7 @@ function hook_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
*/
function hook_ENTITY_TYPE_delete(Drupal\Core\Entity\EntityInterface $entity) {
// Delete the entity's entry from a fictional table of all entities.
db_delete('example_entity')
\Drupal::database()->delete('example_entity')
->condition('type', $entity->getEntityTypeId())
->condition('id', $entity->id())
->execute();
......
......@@ -2,6 +2,8 @@
namespace Drupal\Tests\aggregator\Functional;
use Drupal\Core\Database\Database;
/**
* Tests OPML import.
*
......@@ -70,6 +72,7 @@ public function validateImportFormFields() {
* Submits form with invalid, empty, and valid OPML files.
*/
protected function submitImportForm() {
$connection = Database::getConnection();
$before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
$form['files[upload]'] = $this->getInvalidOpml();
......@@ -83,7 +86,7 @@ protected function submitImportForm() {
$after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
$this->assertEqual($before, $after, 'No feeds were added during the two last form submissions.');
db_delete('aggregator_feed')->execute();
$connection->delete('aggregator_feed')->execute();
$feeds[0] = $this->getFeedEditArray();
$feeds[1] = $this->getFeedEditArray();
......
......@@ -67,6 +67,7 @@ function dblog_cron() {
// Counting the most recent n rows avoids issues with wid number sequences,
// e.g. auto_increment value > 1 or rows deleted directly from the table.
if ($row_limit > 0) {
$connection = \Drupal::database();
$min_row = db_select('watchdog', 'w')
->fields('w', ['wid'])
->orderBy('wid', 'DESC')
......@@ -75,7 +76,7 @@ function dblog_cron() {
// Delete all table entries older than the nth row, if nth row was found.
if ($min_row) {
db_delete('watchdog')
$connection->delete('watchdog')
->condition('wid', $min_row, '<')
->execute();
}
......
......@@ -4,6 +4,7 @@
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\Database;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;
use Drupal\dblog\Controller\DbLogController;
......@@ -554,7 +555,7 @@ public function testFilter() {
$this->drupalLogin($this->adminUser);
// Clear the log to ensure that only generated entries will be found.
db_delete('watchdog')->execute();
Database::getConnection()->delete('watchdog')->execute();
// Generate 9 random watchdog entries.
$type_names = [];
......
......@@ -393,7 +393,7 @@ function hook_field_info_max_weight($entity_type, $bundle, $context, $context_mo
* The field storage being purged.
*/
function hook_field_purge_field_storage(\Drupal\field\Entity\FieldStorageConfig $field_storage) {
db_delete('my_module_field_storage_info')
\Drupal::database()->delete('my_module_field_storage_info')
->condition('uuid', $field_storage->uuid())
->execute();
}
......@@ -410,7 +410,7 @@ function hook_field_purge_field_storage(\Drupal\field\Entity\FieldStorageConfig
* The field being purged.
*/
function hook_field_purge_field(\Drupal\field\Entity\FieldConfig $field) {
db_delete('my_module_field_info')
\Drupal::database()->delete('my_module_field_info')
->condition('id', $field->id())
->execute();
}
......
......@@ -124,7 +124,7 @@ function history_write($nid, $account = NULL) {
* Implements hook_cron().
*/
function history_cron() {
db_delete('history')
\Drupal::database()->delete('history')
->condition('timestamp', HISTORY_READ_LIMIT, '<')
->execute();
}
......@@ -151,7 +151,7 @@ function history_node_view_alter(array &$build, EntityInterface $node, EntityVie
* Implements hook_ENTITY_TYPE_delete() for node entities.
*/
function history_node_delete(EntityInterface $node) {
db_delete('history')
\Drupal::database()->delete('history')
->condition('nid', $node->id())
->execute();
}
......@@ -162,7 +162,7 @@ function history_node_delete(EntityInterface $node) {
function history_user_cancel($edit, $account, $method) {
switch ($method) {
case 'user_cancel_reassign':
db_delete('history')
\Drupal::database()->delete('history')
->condition('uid', $account->id())
->execute();
break;
......@@ -173,7 +173,7 @@ function history_user_cancel($edit, $account, $method) {
* Implements hook_ENTITY_TYPE_delete() for user entities.
*/
function history_user_delete($account) {
db_delete('history')
\Drupal::database()->delete('history')
->condition('uid', $account->id())
->execute();
}
......
......@@ -863,7 +863,7 @@ function locale_translation_update_file_history($file) {
* Language code(s) to be deleted from the file history.
*/
function locale_translation_file_history_delete($projects = [], $langcodes = []) {
$query = db_delete('locale_file');
$query = \Drupal::database()->delete('locale_file');
if (!empty($projects)) {
$query->condition('project', $projects, 'IN');
}
......
......@@ -56,7 +56,7 @@ function hook_path_update($path) {
* @see \Drupal\Core\Path\AliasStorageInterface::delete()
*/
function hook_path_delete($path) {
db_delete('mytable')
\Drupal::database()->delete('mytable')
->condition('pid', $path['pid'])
->execute();
}
......
......@@ -140,8 +140,9 @@ function search_preprocess_block(&$variables) {
* omitted, all items matching $sid and $type are cleared.
*/
function search_index_clear($type = NULL, $sid = NULL, $langcode = NULL) {
$query_index = db_delete('search_index');
$query_dataset = db_delete('search_dataset');
$connection = \Drupal::database();
$query_index = $connection->delete('search_index');
$query_dataset = $connection->delete('search_dataset');
if ($type) {
$query_index->condition('type', $type);
$query_dataset->condition('type', $type);
......@@ -232,7 +233,7 @@ function search_update_totals() {
$or->condition('word', $word->realword);
}
if (count($or) > 0) {
db_delete('search_total')
$connection->delete('search_total')
->condition($or)
->execute();
}
......
......@@ -78,7 +78,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
public function deleteAssignedShortcutSets(ShortcutSetInterface $entity) {
// First, delete any user assignments for this set, so that each of these
// users will go back to using whatever default set applies.
db_delete('shortcut_set_users')
$this->connection->delete('shortcut_set_users')
->condition('set_name', $entity->id())
->execute();
}
......@@ -98,7 +98,7 @@ public function assignUser(ShortcutSetInterface $shortcut_set, $account) {
* {@inheritdoc}
*/
public function unassignUser($account) {
$deleted = db_delete('shortcut_set_users')
$deleted = $this->connection->delete('shortcut_set_users')
->condition('uid', $account->id())
->execute();
return (bool) $deleted;
......
......@@ -588,7 +588,7 @@ function taxonomy_node_predelete(EntityInterface $node) {
*/
function taxonomy_delete_node_index(EntityInterface $node) {
if (\Drupal::config('taxonomy.settings')->get('maintain_index_table')) {
db_delete('taxonomy_index')->condition('nid', $node->id())->execute();
\Drupal::database()->delete('taxonomy_index')->condition('nid', $node->id())->execute();
}
}
......@@ -598,7 +598,7 @@ function taxonomy_delete_node_index(EntityInterface $node) {
function taxonomy_taxonomy_term_delete(Term $term) {
if (\Drupal::config('taxonomy.settings')->get('maintain_index_table')) {
// Clean up the {taxonomy_index} table when terms are deleted.
db_delete('taxonomy_index')->condition('tid', $term->id())->execute();
\Drupal::database()->delete('taxonomy_index')->condition('tid', $term->id())->execute();
}
}
......
......@@ -5,6 +5,7 @@
use Drupal\comment\CommentInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Database\Database;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldStorageConfig;
......@@ -373,9 +374,10 @@ public function testTrackerCronIndexing() {
\Drupal::state()->set('tracker.index_nid', 4);
// Clear the current tracker tables and rebuild them.
db_delete('tracker_node')
$connection = Database::getConnection();
$connection->delete('tracker_node')
->execute();
db_delete('tracker_user')
$connection->delete('tracker_user')
->execute();
tracker_cron();
......
......@@ -55,16 +55,17 @@ function tracker_cron() {
->execute();
$nodes = Node::loadMultiple($nids);
$connection = \Drupal::database();
foreach ($nodes as $nid => $node) {
// Calculate the changed timestamp for this node.
$changed = _tracker_calculate_changed($node);
// Remove existing data for this node.
db_delete('tracker_node')
$connection->delete('tracker_node')
->condition('nid', $nid)
->execute();
db_delete('tracker_user')
$connection->delete('tracker_user')
->condition('nid', $nid)
->execute();
......@@ -199,10 +200,11 @@ function tracker_node_update(NodeInterface $node, $arg = 0) {
* Deletes tracking information for a node.
*/
function tracker_node_predelete(EntityInterface $node, $arg = 0) {
db_delete('tracker_node')
$connection = \Drupal::database();
$connection->delete('tracker_node')
->condition('nid', $node->id())
->execute();
db_delete('tracker_user')
$connection->delete('tracker_user')
->condition('nid', $node->id())
->execute();
}
......@@ -345,7 +347,7 @@ function _tracker_remove($nid, $uid = NULL, $changed = NULL) {
// If we haven't found a reason to keep the user's subscription, delete it.
if (!$keep_subscription) {
db_delete('tracker_user')
$connection->delete('tracker_user')
->condition('nid', $nid)
->condition('uid', $uid)
->execute();
......@@ -383,10 +385,10 @@ function _tracker_remove($nid, $uid = NULL, $changed = NULL) {
}
else {
// If the node doesn't exist, remove everything.
db_delete('tracker_node')
$connection->delete('tracker_node')
->condition('nid', $nid)
->execute();
db_delete('tracker_user')
$connection->delete('tracker_user')
->condition('nid', $nid)
->execute();
}
......
......@@ -39,7 +39,7 @@ protected function update($name, $data) {
}
protected function delete($name) {
db_delete('config')->condition('name', $name)->execute();
Database::getConnection()->delete('config')->condition('name', $name)->execute();
}
}
......@@ -3,6 +3,7 @@
namespace Drupal\KernelTests\Core\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\Delete;
use Drupal\Core\Database\Query\Update;
use Drupal\Core\Database\Transaction;
use Drupal\Core\Database\Database;
......@@ -300,4 +301,13 @@ public function testDbUpdate() {
$this->assertInstanceOf(Update::class, db_update('test'));
}
/**
* Tests deprecation of the db_delete() function.
*
* @expectedDeprecation db_delete 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 delete() on it. For example, $injected_database->delete($table, $options). See https://www.drupal.org/node/2993033
*/
public function testDbDelete() {
$this->assertInstanceOf(Delete::class, db_delete('test'));
}
}
......@@ -27,7 +27,7 @@ public function testSubselectDelete() {
$subquery = db_select('test', 't')
->fields('t', ['id'])
->condition('t.id', [$pid_to_delete], 'IN');
$delete = db_delete('test_task')
$delete = $this->connection->delete('test_task')
->condition('task', 'sleep')
->condition('pid', $subquery, 'IN');
......@@ -44,7 +44,7 @@ public function testSubselectDelete() {
public function testSimpleDelete() {
$num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
$num_deleted = db_delete('test')
$num_deleted = $this->connection->delete('test')
->condition('id', 1)
->execute();
$this->assertIdentical($num_deleted, 1, 'Deleted 1 record.');
......@@ -150,7 +150,7 @@ public function testTruncateTransactionRollback() {
public function testSpecialColumnDelete() {
$num_records_before = db_query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
$num_deleted = db_delete('test_special_columns')
$num_deleted = $this->connection->delete('test_special_columns')
->condition('id', 1)
->execute();
$this->assertIdentical($num_deleted, 1, 'Deleted 1 special column record.');
......
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