diff --git a/core/includes/database.inc b/core/includes/database.inc index dda9e73e74..c7ece971f2 100644 --- a/core/includes/database.inc +++ b/core/includes/database.inc @@ -181,14 +181,12 @@ 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 + @trigger_error('db_merge() 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 merge() on it. For example, $injected_database->merge($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'; } - @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/shortcut/src/ShortcutSetStorage.php b/core/modules/shortcut/src/ShortcutSetStorage.php index f2d5ea5c0d..7869523312 100644 --- a/core/modules/shortcut/src/ShortcutSetStorage.php +++ b/core/modules/shortcut/src/ShortcutSetStorage.php @@ -6,6 +6,7 @@ use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\Entity\ConfigEntityStorage; +use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\LanguageManagerInterface; @@ -24,6 +25,13 @@ class ShortcutSetStorage extends ConfigEntityStorage implements ShortcutSetStora */ protected $moduleHandler; + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + /** * Constructs a ShortcutSetStorageController object. * @@ -39,11 +47,14 @@ class ShortcutSetStorage extends ConfigEntityStorage implements ShortcutSetStora * The language manager. * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache * The memory cache. + * @param \Drupal\Core\Database\Connection $connection + * The database connection. */ - public function __construct(EntityTypeInterface $entity_info, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache) { + public function __construct(EntityTypeInterface $entity_info, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, Connection $connection) { parent::__construct($entity_info, $config_factory, $uuid_service, $language_manager, $memory_cache); $this->moduleHandler = $module_handler; + $this->connection = $connection; } /** @@ -56,7 +67,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('uuid'), $container->get('module_handler'), $container->get('language_manager'), - $container->get('entity.memory_cache') + $container->get('entity.memory_cache'), + $container->get('database') ); } @@ -75,8 +87,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') + $this->connection->merge('shortcut_set_users') ->key('uid', $account->id()) ->fields(['set_name' => $shortcut_set->id()]) ->execute(); diff --git a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php index 20103a2f91..6eb4b0fbec 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php @@ -114,4 +114,22 @@ public function testDbFieldNames() { $this->assertSame(['test_field'], db_field_names(['test_field'])); } + /** + * Tests deprecation of the db_merge() function. + * + * @expectedDeprecation db_merge() 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 merge() on it. For example, $injected_database->merge($table, $options). See https://www.drupal.org/node/2993033 + */ + public function testDbMerge() { + $num_records_before = $this->connection->select('test_people')->countQuery()->execute()->fetchField(); + $result = db_merge('test_people') + ->key('job', 'Presenter') + ->fields([ + 'age' => 31, + 'name' => 'Tiffany', + ]) + ->execute(); + $num_records_after = $this->connection->select('test_people')->countQuery()->execute()->fetchField(); + $this->assertSame($num_records_before + 1, $num_records_after, 'Merge inserted properly.'); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php b/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php index b8e1868201..1fa8327d79 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php @@ -9,22 +9,16 @@ * Tests the MERGE query builder. * * @group Database - * @group legacy */ class MergeTest extends DatabaseTestBase { /** * Confirms that we can merge-insert a record successfully. - * - * @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 testMergeInsert() { $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField(); - $result = db_merge('test_people') + $result = $this->connection->merge('test_people') ->key('job', 'Presenter') ->fields([ 'age' => 31, @@ -45,16 +39,11 @@ public function testMergeInsert() { /** * Confirms that we can merge-update a record successfully. - * - * @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 testMergeUpdate() { $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField(); - $result = db_merge('test_people') + $result = $this->connection->merge('test_people') ->key('job', 'Speaker') ->fields([ 'age' => 31, @@ -78,16 +67,11 @@ public function testMergeUpdate() { * * This test varies from the previous test because it manually defines which * fields are inserted, and which fields are updated. - * - * @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 testMergeUpdateExcept() { $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField(); - db_merge('test_people') + $this->connection->merge('test_people') ->key('job', 'Speaker') ->insertFields(['age' => 31]) ->updateFields(['name' => 'Tiffany']) @@ -104,16 +88,11 @@ public function testMergeUpdateExcept() { /** * Confirms that we can merge-update a record, with alternate replacement. - * - * @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 testMergeUpdateExplicit() { $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField(); - db_merge('test_people') + $this->connection->merge('test_people') ->key('job', 'Speaker') ->insertFields([ 'age' => 31, @@ -135,11 +114,6 @@ public function testMergeUpdateExplicit() { /** * Confirms that we can merge-update a record successfully, with expressions. - * - * @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 testMergeUpdateExpression() { $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField(); @@ -151,7 +125,7 @@ public function testMergeUpdateExpression() { // Note that we are also double-setting age here, once as a literal and // once as an expression. This test will only pass if the expression wins, // which is what is supposed to happen. - db_merge('test_people') + $this->connection->merge('test_people') ->key('job', 'Speaker') ->fields(['name' => 'Tiffany']) ->insertFields(['age' => 31]) @@ -169,16 +143,11 @@ public function testMergeUpdateExpression() { /** * Tests that we can merge-insert without any update fields. - * - * @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 testMergeInsertWithoutUpdate() { $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField(); - db_merge('test_people') + $this->connection->merge('test_people') ->key('job', 'Presenter') ->execute(); @@ -193,16 +162,11 @@ public function testMergeInsertWithoutUpdate() { /** * Confirms that we can merge-update without any update fields. - * - * @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 testMergeUpdateWithoutUpdate() { $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField(); - db_merge('test_people') + $this->connection->merge('test_people') ->key('job', 'Speaker') ->execute(); @@ -214,7 +178,7 @@ public function testMergeUpdateWithoutUpdate() { $this->assertEqual($person->age, 30, 'Age skipped correctly.'); $this->assertEqual($person->job, 'Speaker', 'Job skipped correctly.'); - db_merge('test_people') + $this->connection->merge('test_people') ->key('job', 'Speaker') ->insertFields(['age' => 31]) ->execute(); @@ -230,11 +194,6 @@ public function testMergeUpdateWithoutUpdate() { /** * Tests that an invalid merge query throws an exception. - * - * @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 testInvalidMerge() { try { @@ -242,7 +201,7 @@ public function testInvalidMerge() { // Normally it would throw an exception but we are suppressing it with // the throw_exception option. $options['throw_exception'] = FALSE; - db_merge('test_people', $options) + $this->connection->merge('test_people', $options) ->fields([ 'age' => 31, 'name' => 'Tiffany', @@ -257,7 +216,7 @@ public function testInvalidMerge() { try { // This query will fail because there is no key field specified. - db_merge('test_people') + $this->connection->merge('test_people') ->fields([ 'age' => 31, 'name' => 'Tiffany', diff --git a/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php index 466b44af43..4dee52d5c7 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php @@ -2,9 +2,6 @@ namespace Drupal\KernelTests\Core\Database; -use Drupal\Core\Database\Database; -use Drupal\Core\Database\Query\Merge; - /** * Regression tests cases for the database layer. * @@ -60,34 +57,4 @@ public function testDBIndexExists() { $this->assertSame(FALSE, db_index_exists('test', 'nosuchindex'), 'Returns false for nonexistent index.'); } - /** - * Tests the db_merge() function is deprecated. - * - * @group legacy - * - * @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. - * - * @group legacy - * - * @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 = Database::getConnection()->getKey(); - $this->assert(db_set_active($get_active_db), 'Database connection is active'); - } - }