diff -u b/core/includes/database.inc b/core/includes/database.inc --- b/core/includes/database.inc +++ b/core/includes/database.inc @@ -155,7 +155,7 @@ * @see \Drupal\Core\Database\Connection::defaultOptions() */ function db_insert($table, array $options = []) { - @trigger_error('db_insert() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\Database::getConnection()->insert() instead. See https://www.drupal.org/node/2989742', E_USER_DEPRECATED); + @trigger_error('db_insert() 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 call insert() on it. For example, $injected_database->insert($table, $options); See https://www.drupal.org/node/2993033', E_USER_DEPRECATED); if (empty($options['target']) || $options['target'] == 'replica') { $options['target'] = 'default'; } diff -u b/core/modules/contact/tests/drupal-7.contact.database.php b/core/modules/contact/tests/drupal-7.contact.database.php --- b/core/modules/contact/tests/drupal-7.contact.database.php +++ b/core/modules/contact/tests/drupal-7.contact.database.php @@ -17,7 +17,7 @@ ->execute(); // Add a custom contact category. -\Drupal::database()->insert('contact')->fields([ +$connection->insert('contact')->fields([ 'category', 'recipients', 'reply', diff -u b/core/modules/file/tests/src/Kernel/UsageTest.php b/core/modules/file/tests/src/Kernel/UsageTest.php --- b/core/modules/file/tests/src/Kernel/UsageTest.php +++ b/core/modules/file/tests/src/Kernel/UsageTest.php @@ -22,7 +22,8 @@ */ public function testGetUsage() { $file = $this->createFile(); - \Drupal::database()->insert('file_usage') + $connection = \Drupal::database(); + $connection->insert('file_usage') ->fields([ 'fid' => $file->id(), 'module' => 'testing', @@ -31,7 +32,7 @@ 'count' => 1, ]) ->execute(); - \Drupal::database()->insert('file_usage') + $connection->insert('file_usage') ->fields([ 'fid' => $file->id(), 'module' => 'testing', @@ -105,7 +106,8 @@ $file = $this->createFile(); $file->setPermanent(); $file_usage = $this->container->get('file.usage'); - \Drupal::database()->insert('file_usage') + $connection = Database::getConnection(); + $connection->insert('file_usage') ->fields([ 'fid' => $file->id(), 'module' => 'testing', diff -u b/core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php b/core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php --- b/core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php +++ b/core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php @@ -67,14 +67,15 @@ $account->save(); \Drupal::currentUser()->setAccount($account); - \Drupal::database()->insert('history') + $connection = \Drupal::database(); + $connection->insert('history') ->fields([ 'uid' => $account->id(), 'nid' => $nodes[0]->id(), 'timestamp' => REQUEST_TIME - 100, ])->execute(); - \Drupal::database()->insert('history') + $connection->insert('history') ->fields([ 'uid' => $account->id(), 'nid' => $nodes[1]->id(), diff -u b/core/modules/locale/tests/src/Functional/LocaleUpdateBase.php b/core/modules/locale/tests/src/Functional/LocaleUpdateBase.php --- b/core/modules/locale/tests/src/Functional/LocaleUpdateBase.php +++ b/core/modules/locale/tests/src/Functional/LocaleUpdateBase.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\locale\Functional; +use Drupal\Core\Database\Database; use Drupal\Core\StreamWrapper\PublicStream; use Drupal\file\Entity\File; use Drupal\Tests\BrowserTestBase; @@ -278,9 +279,10 @@ 'filename' => 'custom_module_one.de.po', 'version' => '', ]; + $connection = Database::getConnection(); foreach ($data as $file) { $file = array_merge($default, $file); - \Drupal::database()->insert('locale_file')->fields($file)->execute(); + $connection->insert('locale_file')->fields($file)->execute(); } } diff -u b/core/modules/node/node.install b/core/modules/node/node.install --- b/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -134,7 +134,7 @@ } // Populate the node access table. - \Drupal::database()->insert('node_access') + Database::getConnection()->insert('node_access') ->fields([ 'nid' => 0, 'gid' => 0, diff -u b/core/modules/node/tests/src/Functional/NodeAccessGrantsCacheContextTest.php b/core/modules/node/tests/src/Functional/NodeAccessGrantsCacheContextTest.php --- b/core/modules/node/tests/src/Functional/NodeAccessGrantsCacheContextTest.php +++ b/core/modules/node/tests/src/Functional/NodeAccessGrantsCacheContextTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\node\Functional; +use Drupal\Core\Database\Database; + /** * Tests the node access grants cache context service. * @@ -92,7 +94,7 @@ 'grant_update' => 0, 'grant_delete' => 0, ]; - \Drupal::database()->insert('node_access')->fields($record)->execute(); + Database::getConnection()->insert('node_access')->fields($record)->execute(); // Put user accessUser (uid 0) in the realm. \Drupal::state()->set('node_access_test.no_access_uid', 0); diff -u b/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php b/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php --- b/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php +++ b/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\node\Functional; +use Drupal\Core\Database\Database; + /** * Tests that node access queries are properly altered by the node module. * @@ -151,7 +153,8 @@ 'grant_update' => 0, 'grant_delete' => 0, ]; - \Drupal::database()->insert('node_access')->fields($record)->execute(); + $connection = Database::getConnection(); + $connection->insert('node_access')->fields($record)->execute(); // Test that the noAccessUser still doesn't have the 'view' // privilege after adding the node_access record. diff -u b/core/modules/search/search.module b/core/modules/search/search.module --- b/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -539,7 +539,7 @@ $connection = \Drupal::database(); // Insert cleaned up data into dataset - \Drupal::database()->insert('search_dataset') + $connection->insert('search_dataset') ->fields([ 'sid' => $sid, 'langcode' => $langcode, diff -u b/core/modules/search/tests/src/Functional/SearchRankingTest.php b/core/modules/search/tests/src/Functional/SearchRankingTest.php --- b/core/modules/search/tests/src/Functional/SearchRankingTest.php +++ b/core/modules/search/tests/src/Functional/SearchRankingTest.php @@ -4,6 +4,7 @@ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\comment\Tests\CommentTestTrait; +use Drupal\Core\Database\Database; use Drupal\Core\Url; use Drupal\filter\Entity\FilterFormat; use Drupal\search\Entity\SearchPage; @@ -104,7 +105,7 @@ // to the Statistics module. So instead go ahead and manually update the // counter for this node. $nid = $nodes['views'][1]->id(); - \Drupal::database()->insert('node_counter') + Database::getConnection()->insert('node_counter') ->fields(['totalcount' => 5, 'daycount' => 5, 'timestamp' => REQUEST_TIME, 'nid' => $nid]) ->execute(); diff -u b/core/modules/system/tests/modules/module_test/module_test.install b/core/modules/system/tests/modules/module_test/module_test.install --- b/core/modules/system/tests/modules/module_test/module_test.install +++ b/core/modules/system/tests/modules/module_test/module_test.install @@ -5,6 +5,8 @@ * Install, update and uninstall functions for the module_test module. */ +use Drupal\Core\Database\Database; + /** * Implements hook_schema(). */ @@ -29,7 +31,7 @@ */ function module_test_install() { $schema = drupal_get_module_schema('module_test', 'module_test'); - \Drupal::database()->insert('module_test') + Database::getConnection()->insert('module_test') ->fields([ 'data' => $schema['fields']['data']['type'], ]) diff -u b/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module --- b/core/modules/tracker/tracker.module +++ b/core/modules/tracker/tracker.module @@ -55,6 +55,7 @@ ->execute(); $nodes = Node::loadMultiple($nids); + $connection = \Drupal::database(); foreach ($nodes as $nid => $node) { // Calculate the changed timestamp for this node. @@ -69,7 +70,7 @@ ->execute(); // Insert the node-level data. - \Drupal::database()->insert('tracker_node') + $connection->insert('tracker_node') ->fields([ 'nid' => $nid, 'published' => (int) $node->isPublished(), @@ -78,7 +79,7 @@ ->execute(); // Insert the user-level data for the node's author. - \Drupal::database()->insert('tracker_user') + $connection->insert('tracker_user') ->fields([ 'nid' => $nid, 'published' => (int) $node->isPublished(), @@ -101,7 +102,7 @@ ->groupBy('uid') ->execute(); if ($result) { - $query = \Drupal::database()->insert('tracker_user'); + $query = $connection->insert('tracker_user'); foreach ($result as $row) { $query->fields([ 'uid' => $row['uid'], diff -u b/core/modules/views/tests/src/Functional/ViewTestBase.php b/core/modules/views/tests/src/Functional/ViewTestBase.php --- b/core/modules/views/tests/src/Functional/ViewTestBase.php +++ b/core/modules/views/tests/src/Functional/ViewTestBase.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\views\Functional; use Behat\Mink\Exception\ElementNotFoundException; +use Drupal\Core\Database\Database; use Drupal\Core\Database\Query\SelectInterface; use Drupal\Tests\BrowserTestBase; use Drupal\views\Tests\ViewResultAssertionTrait; @@ -55,7 +56,7 @@ // Load the test dataset. $data_set = $this->dataSet(); - $query = \Drupal::database()->insert('views_test_data') + $query = Database::getConnection()->insert('views_test_data') ->fields(array_keys($data_set[0])); foreach ($data_set as $record) { $query->values($record); diff -u b/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php b/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php --- b/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php +++ b/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php @@ -31,7 +31,7 @@ } protected function insert($name, $data) { - \Drupal::database()->insert('config')->fields(['name' => $name, 'data' => $data])->execute(); + Database::getConnection()->insert('config')->fields(['name' => $name, 'data' => $data])->execute(); } protected function update($name, $data) { diff -u b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php --- b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php @@ -2,6 +2,8 @@ namespace Drupal\KernelTests\Core\Database; +use Drupal\Core\Database\Database; + /** * Regression tests cases for the database layer. * @@ -70,7 +72,7 @@ * @expectedDeprecation db_set_active() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\Database::setActiveConnection() instead. See https://www.drupal.org/node/2944084. */ public function testDBIsActive() { - $get_active_db = $this->connection->getKey(); + $get_active_db = Database::getConnection()->getKey(); $this->assert(db_set_active($get_active_db), 'Database connection is active'); } @@ -87,17 +89,2 @@ - /** - * Tests the db_insert() function. - * - * @group legacy - * - * @expectedDeprecation db_insert() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\Database::getConnection()->insert() instead. See https://www.drupal.org/node/2989742 - */ - public function testDbInsert() { - $this->assertNotNull( - db_insert('test') - ->fields(['name' => 'Tester', 'age' => 20]) - ->execute() - ); - } - } diff -u b/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php b/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php --- b/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php @@ -123,7 +123,8 @@ // Generate values and insert them directly in the storage tables. $values = []; - $query = Database::getConnection()->insert($this->revisionTable)->fields($columns); + $connection = Database::getConnection(); + $query = $connection->insert($this->revisionTable)->fields($columns); foreach ($revision_ids as $revision_id) { // Put one value too many. for ($delta = 0; $delta <= $this->fieldCardinality; $delta++) { @@ -133,7 +134,7 @@ } $query->execute(); } - $query = Database::getConnection()->insert($this->table)->fields($columns); + $query = $connection->insert($this->table)->fields($columns); foreach ($values[$revision_id] as $delta => $value) { $query->values([$bundle, 0, $entity->id(), $revision_id, $delta, $entity->language()->getId(), $value]); } @@ -167,8 +168,8 @@ // loaded. $unavailable_langcode = 'xx'; $values = [$bundle, 0, $entity->id(), $entity->getRevisionId(), 0, $unavailable_langcode, mt_rand(1, 127)]; - Database::getConnection()->insert($this->table)->fields($columns)->values($values)->execute(); - Database::getConnection()->insert($this->revisionTable)->fields($columns)->values($values)->execute(); + $connection->insert($this->table)->fields($columns)->values($values)->execute(); + $connection->insert($this->revisionTable)->fields($columns)->values($values)->execute(); $entity = $storage->load($entity->id()); $this->assertFalse(array_key_exists($unavailable_langcode, $entity->{$this->fieldName})); } only in patch2: unchanged: --- a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php @@ -3,6 +3,7 @@ namespace Drupal\KernelTests\Core\Database; use Drupal\Core\Database\Query\Condition; +use Drupal\Core\Database\Query\Insert; use Drupal\Core\Database\Query\Update; use Drupal\Core\Database\Transaction; use Drupal\Core\Database\Database; @@ -216,4 +217,13 @@ public function testDbUpdate() { $this->assertInstanceOf(Update::class, db_update('test')); } + /** + * Tests the db_insert() function. + * + * @expectedDeprecation db_insert() 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 call insert() on it. For example, $injected_database->insert($table, $options); See https://www.drupal.org/node/2993033 + */ + public function testDbInsert() { + $this->assertInstanceOf(Insert::class, db_insert('test')); + } + }