diff --git a/core/includes/database.inc b/core/includes/database.inc index 6891ea92e1..3b6ce37a9b 100644 --- a/core/includes/database.inc +++ b/core/includes/database.inc @@ -181,9 +181,14 @@ function db_insert($table, array $options = []) { * @see \Drupal\Core\Database\Connection::defaultOptions() */ function db_merge($table, array $options = []) { + // @todo Move defaults to connection https://www.drupal.org/node/2947775 if (empty($options['target']) || $options['target'] == 'replica') { $options['target'] = 'default'; } + @trigger_error( + "Deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call merge() on it. For example, \$injected_database->merge(\$table, \$options);", + E_USER_DEPRECATED + ); return Database::getConnection($options['target'])->merge($table, $options); } diff --git a/core/modules/search/search.module b/core/modules/search/search.module index 4b517c1cda..be665e6729 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -211,13 +211,14 @@ function search_cron() { * up to date (even if cron times out or otherwise fails). */ function search_update_totals() { + $connection = \Drupal::database(); // Update word IDF (Inverse Document Frequency) counts for new/changed words. foreach (search_dirty() as $word => $dummy) { // Get total count $total = db_query("SELECT SUM(score) FROM {search_index} WHERE word = :word", [':word' => $word], ['target' => 'replica'])->fetchField(); // Apply Zipf's law to equalize the probability distribution. $total = log10(1 + 1 / (max(1, $total))); - \Drupal::database()->merge('search_total') + $connection->merge('search_total') ->key('word', $word) ->fields(['count' => $total]) ->execute(); @@ -536,6 +537,7 @@ function search_index($type, $sid, $langcode, $text) { // cache tags. search_index_clear($type, $sid, $langcode); + $connection = \Drupal::database(); // Insert cleaned up data into dataset db_insert('search_dataset') ->fields([ @@ -552,7 +554,7 @@ function search_index($type, $sid, $langcode, $text) { // If a word already exists in the database, its score gets increased // appropriately. If not, we create a new record with the appropriate // starting score. - \Drupal::database()->merge('search_index') + $connection->merge('search_index') ->keys([ 'word' => $word, 'sid' => $sid, diff --git a/core/modules/shortcut/src/ShortcutSetStorage.php b/core/modules/shortcut/src/ShortcutSetStorage.php index ce0b442319..86d36a2a3d 100644 --- a/core/modules/shortcut/src/ShortcutSetStorage.php +++ b/core/modules/shortcut/src/ShortcutSetStorage.php @@ -71,6 +71,7 @@ public function deleteAssignedShortcutSets(ShortcutSetInterface $entity) { * {@inheritdoc} */ public function assignUser(ShortcutSetInterface $shortcut_set, $account) { + // @todo Inject database service https://www.drupal.org/node/2947769 \Drupal::database()->merge('shortcut_set_users') ->key('uid', $account->id()) ->fields(['set_name' => $shortcut_set->id()]) diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index b18a232783..434a31f7ac 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -546,8 +546,9 @@ function taxonomy_build_node_index($node) { } // Insert index entries for all the node's terms. if (!empty($tid_all)) { + $connection = \Drupal::database(); foreach ($tid_all as $tid) { - \Drupal::database()->merge('taxonomy_index') + $connection->merge('taxonomy_index') ->key(['nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished()]) ->fields(['sticky' => $sticky, 'created' => $node->getCreatedTime()]) ->execute(); diff --git a/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module index 86c37391f9..f129ad132e 100644 --- a/core/modules/tracker/tracker.module +++ b/core/modules/tracker/tracker.module @@ -259,7 +259,8 @@ function _tracker_add($nid, $uid, $changed) { $changed = max($node->changed, $changed); // Update the node-level data. - \Drupal::database()->merge('tracker_node') + $connection = \Drupal::database(); + $connection->merge('tracker_node') ->key('nid', $nid) ->fields([ 'changed' => $changed, @@ -268,7 +269,7 @@ function _tracker_add($nid, $uid, $changed) { ->execute(); // Create or update the user-level data, first for the user posting. - \Drupal::database()->merge('tracker_user') + $connection->merge('tracker_user') ->keys([ 'nid' => $nid, 'uid' => $uid, diff --git a/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php b/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php index 0b860f92c1..10e0ac852e 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php @@ -18,6 +18,7 @@ class MergeTest extends DatabaseTestBase { public function testMergeInsert() { $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField(); + // @todo Inject database service https://www.drupal.org/node/2947769 $result = \Drupal::database()->merge('test_people') ->key('job', 'Presenter') ->fields([ diff --git a/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php index 22173dae66..3f8e2688d0 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php @@ -3,6 +3,7 @@ namespace Drupal\KernelTests\Core\Database; use Drupal\Core\Database\Database; +use Drupal\Core\Database\Query\Merge; /** * Regression tests cases for the database layer. @@ -59,6 +60,15 @@ public function testDBIndexExists() { $this->assertSame(FALSE, db_index_exists('test', 'nosuchindex'), 'Returns false for nonexistent index.'); } + /** + * Tests the db_merge() function is deprecated. + * + * @expectedDeprecation Deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call merge() on it. For example, $injected_database->merge($table, $options); + */ + public function testDBMergeIsDeprecated() { + $this->assertTrue(db_merge('test') instanceof Merge); + } + /** * Tests the db_set_active() function. * diff --git a/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php b/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php index 274093b7e3..e0107fc143 100644 --- a/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php +++ b/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php @@ -41,9 +41,10 @@ public function testGarbageCollection() { } $this->assertIdentical(count($store->getAll()), 4, 'Four items were written to the storage.'); + $connection = \Drupal::database(); // Manually expire the data. for ($i = 0; $i <= 3; $i++) { - \Drupal::database()->merge('key_value_expire') + $connection->merge('key_value_expire') ->keys([ 'name' => 'key_' . $i, 'collection' => $collection,