Commit 44b3b41a authored by catch's avatar catch

Issue #2848952 by voleger, andypost, mondrake, jeetendrakumar, pk188,...

Issue #2848952 by voleger, andypost, mondrake, jeetendrakumar, pk188, hgunicamp, piggito, JayKandari, daffie, shashikant_chauhan, xjm, cilefen, chiranjeeb, Pavan B S: Replace all calls to db_merge(), which is deprecated
parent 52100ba1
......@@ -181,6 +181,9 @@ function db_insert($table, array $options = []) {
* @see \Drupal\Core\Database\Connection::defaultOptions()
*/
function db_merge($table, array $options = []) {
@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';
}
......
......@@ -1281,6 +1281,7 @@ function hook_ENTITY_TYPE_translation_delete(\Drupal\Core\Entity\EntityInterface
* @see hook_ENTITY_TYPE_predelete()
*/
function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
$connection = \Drupal::database();
// Count references to this entity in a custom table before they are removed
// upon entity deletion.
$id = $entity->id();
......@@ -1293,7 +1294,7 @@ function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
->fetchField();
// Log the count in a table that records this statistic for deleted entities.
db_merge('example_deleted_entity_statistics')
$connection->merge('example_deleted_entity_statistics')
->key(['type' => $type, 'id' => $id])
->fields(['count' => $count])
->execute();
......@@ -1309,6 +1310,7 @@ function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
* @see hook_entity_predelete()
*/
function hook_ENTITY_TYPE_predelete(Drupal\Core\Entity\EntityInterface $entity) {
$connection = \Drupal::database();
// Count references to this entity in a custom table before they are removed
// upon entity deletion.
$id = $entity->id();
......@@ -1321,7 +1323,7 @@ function hook_ENTITY_TYPE_predelete(Drupal\Core\Entity\EntityInterface $entity)
->fetchField();
// Log the count in a table that records this statistic for deleted entities.
db_merge('example_deleted_entity_statistics')
$connection->merge('example_deleted_entity_statistics')
->key(['type' => $type, 'id' => $id])
->fields(['count' => $count])
->execute();
......
......@@ -107,7 +107,7 @@ function history_write($nid, $account = NULL) {
}
if ($account->isAuthenticated()) {
db_merge('history')
\Drupal::database()->merge('history')
->keys([
'uid' => $account->id(),
'nid' => $nid,
......
......@@ -836,7 +836,7 @@ function locale_translation_get_file_history() {
* FALSE on failure. Otherwise SAVED_NEW or SAVED_UPDATED.
*/
function locale_translation_update_file_history($file) {
$status = db_merge('locale_file')
$status = \Drupal::database()->merge('locale_file')
->key([
'project' => $file->project,
'langcode' => $file->langcode,
......
......@@ -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)));
db_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.
db_merge('search_index')
$connection->merge('search_index')
->keys([
'word' => $word,
'sid' => $sid,
......
......@@ -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,7 +87,7 @@ public function deleteAssignedShortcutSets(ShortcutSetInterface $entity) {
* {@inheritdoc}
*/
public function assignUser(ShortcutSetInterface $shortcut_set, $account) {
db_merge('shortcut_set_users')
$this->connection->merge('shortcut_set_users')
->key('uid', $account->id())
->fields(['set_name' => $shortcut_set->id()])
->execute();
......
......@@ -548,8 +548,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) {
db_merge('taxonomy_index')
$connection->merge('taxonomy_index')
->key(['nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished()])
->fields(['sticky' => $sticky, 'created' => $node->getCreatedTime()])
->execute();
......
......@@ -250,6 +250,7 @@ function tracker_comment_delete(CommentInterface $comment) {
* The node updated timestamp or comment timestamp.
*/
function _tracker_add($nid, $uid, $changed) {
$connection = \Drupal::database();
// @todo This should be actually filtering on the desired language and just
// fall back to the default language.
$node = db_query('SELECT nid, status, uid, changed FROM {node_field_data} WHERE nid = :nid AND default_langcode = 1 ORDER BY changed DESC, status DESC', [':nid' => $nid])->fetchObject();
......@@ -259,7 +260,7 @@ function _tracker_add($nid, $uid, $changed) {
$changed = max($node->changed, $changed);
// Update the node-level data.
db_merge('tracker_node')
$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.
db_merge('tracker_user')
$connection->merge('tracker_user')
->keys([
'nid' => $nid,
'uid' => $uid,
......
......@@ -4,7 +4,6 @@
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Transaction;
use Drupal\Core\Database\Database;
/**
......@@ -172,4 +171,22 @@ public function testDbCreateTable() {
$this->assertTrue($this->connection->schema()->tableExists($name));
}
/**
* 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 = (int) $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 = (int) $this->connection->select('test_people')->countQuery()->execute()->fetchField();
$this->assertSame($num_records_before + 1, $num_records_after, 'Merge inserted properly.');
}
}
......@@ -18,7 +18,7 @@ class MergeTest extends DatabaseTestBase {
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,
......@@ -43,7 +43,7 @@ public function testMergeInsert() {
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,
......@@ -71,7 +71,7 @@ public function testMergeUpdate() {
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'])
......@@ -92,7 +92,7 @@ public function testMergeUpdateExcept() {
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,
......@@ -125,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])
......@@ -147,7 +147,7 @@ public function testMergeUpdateExpression() {
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();
......@@ -166,7 +166,7 @@ public function testMergeInsertWithoutUpdate() {
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();
......@@ -178,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();
......@@ -201,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',
......@@ -216,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',
......
......@@ -33,7 +33,8 @@ protected function setUp() {
*/
public function testGarbageCollection() {
$collection = $this->randomMachineName();
$store = new DatabaseStorageExpirable($collection, new PhpSerialize(), Database::getConnection());
$connection = Database::getConnection();
$store = new DatabaseStorageExpirable($collection, new PhpSerialize(), $connection);
// Insert some items and confirm that they're set.
for ($i = 0; $i <= 3; $i++) {
......@@ -43,7 +44,7 @@ public function testGarbageCollection() {
// Manually expire the data.
for ($i = 0; $i <= 3; $i++) {
db_merge('key_value_expire')
$connection->merge('key_value_expire')
->keys([
'name' => 'key_' . $i,
'collection' => $collection,
......
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