Follow-up to #2848161: [meta] Replace calls to deprecated db_*() wrappers

Problem/Motivation

#1894396: Mark db_*() wrappers in database.inc as @deprecated for 9.x deprecated the db_* family of functions. Update documentation recommending them.

Proposed resolution

This can help find them git grep -E "^\s*(\/\/|\*).*db_\w+"

Remaining tasks

CommentFileSizeAuthor
#49 interdiff-2849745-47-49.txt615 bytesvoleger
#49 2849745-49.patch21.69 KBvoleger
#47 interdiff-2849745-42-47.txt1.26 KBvoleger
#47 2849745-47.patch21.67 KBvoleger
#42 interdiff-2849745-39-42.txt1.86 KBvoleger
#42 2849745-42.patch21.62 KBvoleger
#39 interdiff-2849745-37-39.txt13.3 KBvoleger
#39 2849745-39.patch22.25 KBvoleger
#39 2849745-39-just_reroll.patch27.94 KBvoleger
#37 2849745-37-interdiff.txt13.22 KBBerdir
#37 2849745-37.patch29.34 KBBerdir
#32 2849745-32.patch18.29 KBvacho
#21 interdiff-2849745-19-21.txt3.28 KByogeshmpawar
#21 replace_documentation-2849745-21.patch21.66 KByogeshmpawar
#19 interdiff-2849745-17-19.txt1.75 KByogeshmpawar
#19 replace_documentation-2849745-19.patch22.59 KByogeshmpawar
#17 replace_documentation-2849745-17.patch23.24 KByogeshmpawar
#9 drupal-replace-all-recommendation-of-db_*()-2849745-9.patch23.24 KBMunavijayalakshmi
#6 drupal-replace-all-recommendation-of-db_*()-2849745-6.patch21.09 KBgaurav.kapoor
#4 drupal-replace-calls-to-db*()-in-documentation-2849745-4.patch5.4 KBgaurav.kapoor
#2 drupal-replace-documentation-recommending-db_*()-2849745-2.patch1.43 KBgaurav.kapoor
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cilefen created an issue. See original summary.

gaurav.kapoor’s picture

For the start i am uploading this patch , i know it needs more work. Is this the right way to do it??

cilefen’s picture

Issue summary: View changes

Use \Drupal.

I added a search script to help find these to the issue summary.

gaurav.kapoor’s picture

Did some more , tell me if i am wrong.

cilefen’s picture

Don't change CHANGELOG.TXT but it seems like you are going in the right direction.

gaurav.kapoor’s picture

xjm’s picture

Issue tags: +DrupalCampNJ2017
Munavijayalakshmi’s picture

Status: Active » Needs work
  1. +++ b/core/includes/database.inc
    @@ -22,11 +22,11 @@
    + * If the caller or other modules need to change the query, use \Drupal::database()->select()
    

    Line exceeding 80 characters

  2. +++ b/core/includes/database.inc
    @@ -22,11 +22,11 @@
    + * be handled via \Drupal::database()->insert(), \Drupal::database()->update() and \Drupal::database()->delete() respectively.
    

    Line exceeding 80 characters

  3. +++ b/core/includes/database.inc
    @@ -389,18 +389,18 @@ function db_escape_field($field) {
    + * You must use a query builder like \Drupal::database()->select() in order to use \Drupal::database()->like() on
    + * all supported database systems. Using \Drupal::database()->like() with \Drupal::database()->query() or
    

    Line exceeding 80 characters

  4. +++ b/core/includes/database.inc
    @@ -534,7 +534,7 @@ function db_xor() {
    + * Internal API function call.  The \Drupal::database()->and(), \Drupal::database()->or(), and \Drupal::database()->xor()
    

    Line exceeding 80 characters & There are 2 spaces.

  5. +++ b/core/includes/database.inc
    @@ -960,9 +960,9 @@ function db_drop_index($table, $name) {
    + * \Drupal::database()->drop_{primary_key,unique_key,index}() before calling \Drupal::database()->change_field().
    

    Line exceeding 80 characters

  6. +++ b/core/includes/database.inc
    @@ -990,12 +990,12 @@ function db_drop_index($table, $name) {
    + * \Drupal::database()->add_{primary_key,unique_key,index}() for this purpose because the ALTER
    

    Line exceeding 80 characters

  7. +++ b/core/includes/database.inc
    @@ -990,12 +990,12 @@ function db_drop_index($table, $name) {
    + * You could use \Drupal::database()->add_{primary_key,unique_key,index}() in all cases unless you
    

    Line exceeding 80 characters

  8. +++ b/core/includes/form.inc
    @@ -606,15 +606,15 @@ function template_preprocess_form_element_label(&$variables) {
    + *     $context['sandbox']['max'] = \Drupal::database()->query('SELECT COUNT(DISTINCT id) FROM {example}')->fetchField();
    

    Line exceeding 80 characters

  9. +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
    @@ -286,7 +286,7 @@ public function nextIdDelete() {
    +      // We know we are using MySQL here, no need for the slower \Drupal::database()->delete().
    

    Line exceeding 80 characters

  10. +++ b/core/lib/Drupal/Core/Database/Query/SelectInterface.php
    @@ -343,8 +343,8 @@ public function leftJoin($table, $alias = NULL, $condition = NULL, $arguments =
    +   *   \Drupal::database()->query('B')->leftJoin('A'). This functionality has been deprecated
    

    Line exceeding 80 characters

  11. +++ b/core/lib/Drupal/Core/Database/Query/TableSortExtender.php
    @@ -38,7 +38,7 @@ public function orderByHeader(array $header) {
    +      // Based on code from \Drupal::database()->escapeTable(), but this can also contain a dot.
    

    Line exceeding 80 characters

  12. +++ b/core/lib/Drupal/Core/Database/Schema.php
    @@ -312,7 +312,7 @@ public function fieldExists($table, $column) {
    +   *   or index including it in this array. See \Drupal::database()->changeField() for more
    
    @@ -518,9 +518,9 @@ public function fieldExists($table, $column) {
    +   * db_drop_{primary_key,unique_key,index}() before calling \Drupal::database()->changeField().
    

    Line exceeding 80 characters

  13. +++ b/core/lib/Drupal/Core/Database/database.api.php
    @@ -28,7 +28,7 @@
    + * abstraction layer provides the functions \Drupal::database()->query() and \Drupal::database()->queryRange(),
    

    Line exceeding 80 characters

  14. +++ b/core/lib/Drupal/Core/Database/database.api.php
    @@ -37,7 +37,7 @@
    + * As a note, \Drupal::database()->query() and similar functions are wrappers on connection object
    

    Line exceeding 80 characters

  15. +++ b/core/lib/Drupal/Core/Database/database.api.php
    @@ -53,7 +53,7 @@
    + *   use \Drupal::database()->queryRange() instead of \Drupal::database()->query().
    

    Line exceeding 80 characters

  16. +++ b/core/lib/Drupal/Core/Database/database.api.php
    @@ -89,7 +89,7 @@
    + * As a note, \Drupal::database()->select() and similar functions are wrappers on connection
    

    Line exceeding 80 characters

  17. +++ b/core/lib/Drupal/Core/Database/database.api.php
    @@ -123,12 +123,12 @@
    + * consistently across databases; you should never use \Drupal::database()->query() to run
    + * an INSERT, UPDATE, or DELETE query. Instead, use functions \Drupal::database()->insert(),
    + * \Drupal::database()->update(), and \Drupal::database()->delete() to obtain a base query on your table, and then
    

    Line exceeding 80 characters

  18. +++ b/core/lib/Drupal/Core/Database/database.api.php
    @@ -123,12 +123,12 @@
    + * As a note, \Drupal::database()->insert() and similar functions are wrappers on connection
    

    Line exceeding 80 characters

  19. +++ b/core/lib/Drupal/Core/Database/database.api.php
    @@ -197,18 +197,18 @@
    + * The examples here all use functions like \Drupal::database()->select() and \Drupal::database()->query(), which
    ...
    + * case, you can look at the code for \Drupal::database()->select() and the other functions to see
    

    Line exceeding 80 characters

Munavijayalakshmi’s picture

Status: Needs review » Needs work

The last submitted patch, 9: drupal-replace-all-recommendation-of-db_*()-2849745-9.patch, failed testing.

cilefen’s picture

himanshu-dixit’s picture

Status: Needs work » Needs review

The last submitted patch, 6: drupal-replace-all-recommendation-of-db_*()-2849745-6.patch, failed testing.

cilefen’s picture

Status: Needs review » Needs work

The last submitted patch, 9: drupal-replace-all-recommendation-of-db_*()-2849745-9.patch, failed testing.

daffie’s picture

Issue tags: +Needs reroll
yogeshmpawar’s picture

Status: Needs work » Needs review
FileSize
23.24 KB

Uploading same patch with another naming conventions. may be it will solve "patch validation error."

daffie’s picture

@Yogesh Pawar: Thanks for the reroll.

The patch looks good, but I do have some remarks:

  1. +++ b/core/includes/form.inc
    @@ -606,15 +606,15 @@ function template_preprocess_form_element_label(&$variables) {
    - *   $result = db_select('example')
    - *     ->fields('example', array('id'))
    - *     ->condition('id', $context['sandbox']['current_id'], '>')
    - *     ->orderBy('id')
    - *     ->range(0, $limit)
    - *     ->execute();
    + *   $result = \Drupal::database()->select('example');
    + *   $result->fields('example', array('id'));
    + *   $result->condition('id', $context['sandbox']['current_id'], '>');
    + *   $result->orderBy('id');
    + *   $result->range(0, $limit);
    + *   $result->execute();
    

    You have only to change $result = db_select('example') to $result = \Drupal::database()->select('example'). Remember to remove the semicolon. The rest of the change is not necessary. There are multiple instances of this problem.

  2. +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
    @@ -30,12 +30,11 @@ public function orderRandom() {
    -   *   $query
    -   *     ->distinct()
    -   *     ->fields('e')
    -   *     ->orderBy('timestamp');
    +   *   $query->distinct();
    +   *   $query->fields('e');
    +   *   $query->orderBy('timestamp');
    

    This change is not necessary.

yogeshmpawar’s picture

Status: Needs work » Needs review
FileSize
22.59 KB
1.75 KB

@daffie- thanks for your suggestions.

daffie’s picture

Status: Needs review » Needs work

All documentation changes look good to me. I have only one remark:

+++ b/core/includes/database.inc
@@ -389,18 +390,19 @@ function db_escape_field($field) {
- * $result = db_select('person', 'p')
- *   ->fields('p')
- *   ->condition('name', db_like($prefix) . '%', 'LIKE')
- *   ->execute()
- *   ->fetchAll();
+ * $result = \Drupal::database()->select('person', 'p');
+ * $result->fields('p');
+ * $result->condition('name', db_like($prefix) . '%', 'LIKE');
+ * $result->execute();
+ * $result->fetchAll();

+++ b/core/lib/Drupal/Core/Database/Connection.php
@@ -523,11 +523,11 @@ public function makeComment($comments) {
-   * db_update('example')
-   *  ->condition('id', $id)
-   *  ->fields(array('field2' => 10))
-   *  ->comment('Exploit * / DROP TABLE node; --')
-   *  ->execute()
+   * $query = \Drupal::database()->update('example')
+   * $query->condition('id', $id)
+   * $query->fields(array('field2' => 10))
+   * $query->comment('Exploit * / DROP TABLE node; --')
+   * $query->execute()

+++ b/core/lib/Drupal/Core/Database/Query/ConditionInterface.php
@@ -30,8 +30,8 @@
-   * db_select('users')
-   *  ->condition('name', db_like($name), 'LIKE')
+   * $query = \Drupal::database()->select('users');
+   * $query->condition('name', \Drupal::database()->escapeLike($name), 'LIKE');

+++ b/core/lib/Drupal/Core/Database/database.api.php
@@ -89,21 +91,21 @@
- * $result = db_select('example', 'e')
- *   ->fields('e', array('id', 'title', 'created'))
- *   ->condition('e.uid', $uid)
- *   ->orderBy('e.created', 'DESC')
- *   ->range(0, 10)
- *   ->execute();
+ * $result = \Drupal::database()->select('example', 'e');
+ * $result->fields('e', array('id', 'title', 'created'));
+ * $result->condition('e.uid', $uid);
+ * $result->orderBy('e.created', 'DESC');
+ * $result->range(0, 10);
+ * $result->execute();

These need to change in the same way as comment #18.1.

The problem with this patch is that it is impossible to check if we have changed all that must be changed.

yogeshmpawar’s picture

Assigned: Munavijayalakshmi » Unassigned
Status: Needs work » Needs review
FileSize
21.66 KB
3.28 KB

Thanks @daffie, made changes as per remark, also added a interdiff.

daffie’s picture

Issue tags: -Needs reroll

The patch looks good only one nitpick left.

+++ b/core/lib/Drupal/Core/Database/Schema.php
@@ -534,8 +535,8 @@ public function fieldExists($table, $column) {
+   * \Drupal::database()-> dropPrimaryKey('foo');

Nitpick: A space between the arrow and dropPrimaryKey.

The problem with this patch is that it is impossible to check if we have changed all that must be changed. Maybe we should set this issue to postponed untill all other sub-issues of #2319859: [META] Deprecate contents of database.inc are fixed.

cilefen’s picture

Status: Needs review » Postponed

How is it impossible?

daffie’s picture

@cilefen: I do a string search for function that must be changed. And for this issue that are a lot of functions to search for. Each search gives a lot of false positives that are not documentation. If we postponed this issue until all code changes are made, we will have no more false positives. If you know a better way to checking if all instances are changed, please let me know!

cilefen’s picture

I was just asking. The reason this big issue exists is that it was becoming untenable to write patches for documentation when some doc lines mention several db_ functions in a single line, although the original plan had been to change each function with its documentation in single issues. That idea is not set in stone and suggestions on getting this done are welcome! It's complicated, as you know.

Also, note that #2848161: [meta] Replace calls to deprecated db_*() wrappers exists to collect the db_ usages so we don't clutter up #2319859: [META] Deprecate contents of database.inc although you could re-child all the issues there if you are ambitious.

cilefen’s picture

Oh, I see https://www.drupal.org/node/2319859#comment-11966287. I'm still open to suggestions on the docs part: whether it is easier to do the docs by function with the usages, or this big bang issue.

daffie’s picture

@cilefen: What we also can do patch in this issue with most of the documentation changes and followup after all the code changes are done if we forgot something in documentation. You are the committer and you may decide.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Mile23’s picture

Issue tags: +Needs reroll

I've been working on #2634356: Replace deprecated db calls with injected connection in dbtng_example for Examples and it sure would be nice if database.api.php weren't wrong. :-)

Let's split out database.api.php as a special case: #2987399: Update database.api.php to remove deprecated db_*() functions.

vacho’s picture

Patch rerolled

savkaviktor16@gmail.com’s picture

Issue tags: -Needs reroll

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

voleger’s picture

If we fix the issue #2947946: Create change record for all deprecated db_* functions
we can unblock and close the issue #2848161: [meta] Replace calls to deprecated db_*() wrappers
so that's will unblock current issue.
Also, there is a discussion regarding how correctly get connection to the database in the various code context. See #2991337: Document the recommended ways to obtain the database connection object

voleger’s picture

Status: Postponed » Needs work

#2848161: [meta] Replace calls to deprecated db_*() wrappers Needs review as all required child issues were resolved.
So we can unblock this issue.

Berdir’s picture

Status: Needs work » Needs review
FileSize
29.34 KB
13.22 KB

Rerolled, a few cases were already updated. I did look for other instances of db_* functions mentioned in in comments where they shouldn't be and updated them mostly test descriptions and assertion messages.

Berdir’s picture

Status: Needs review » Needs work
+++ b/core/includes/database.inc
@@ -382,14 +383,15 @@ function db_escape_field($field) {
+ * \Drupal::database()->query_range() is not supported.

@@ -808,7 +810,7 @@ function db_drop_table($table) {
  *   without the 'fields' element. If you are adding a type 'serial' field, you
  *   MUST specify at least one key or index including it in this array. See
- *   \Drupal\Core\Database\Schema::changeField() for more explanation why.
+ *   \Drupal::database()->change_field() for more explanation why.
  *

@@ -817,7 +819,7 @@ function db_drop_table($table) {
  * @see \Drupal\Core\Database\Schema::addField()
- * @see \Drupal\Core\Database\Schema::changeField()
+ * @see \Drupal::database()->change_field()
  */

@@ -1050,10 +1052,10 @@ function db_drop_index($table, $name) {
  * That means that you have to drop all affected keys and indexes with
- * db_drop_{primary_key,unique_key,index}() before calling
- * \Drupal\Core\Database\Schema::changeField().
+ * \Drupal::database()->drop_{primary_key,unique_key,index}() before calling
+ * \Drupal::database()->change_field().
  * To recreate the keys and indices, pass the key definitions as the optional

@@ -1067,9 +1069,8 @@ function db_drop_index($table, $name) {
  * @code
- * $schema = \Drupal::database()->schema();
- * $schema->dropPrimaryKey('foo');
- * $schema->changeField('foo', 'bar', 'bar',
+ * \Drupal::database()->drop_primary_key('foo');
+ * \Drupal::database()->change_field('foo', 'bar', 'bar',
  *   array('type' => 'serial', 'not null' => TRUE),
  *   array('primary key' => array('bar')));

Quite a few replacements seem strange and actually backwards, neither are do these methods use _ nor are they on the connection object, often the old version was actually correct?

voleger’s picture

Rerolled patch.
Some changes were reverted.
Found a few lines for replacement.

The last submitted patch, 39: 2849745-39-just_reroll.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Berdir’s picture

Status: Needs review » Needs work

Looks much better now, reviewed with word diff, found just these 3 nitpicks.

  1. +++ b/core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php
    @@ -38,7 +38,7 @@ public function testTemporaryQuery() {
     
    -    // Now try to run two db_query_temporary() in the same request.
    +    // Now try to run two $connection->queryTemporary() in the same request.
         $table_name_test = $connection->queryTemporary('SELECT name FROM {test}', []);
    

    This is also an example where it would possibly make more sense to just write a proper english sentence: .. run two temporary queries in the same request.

  2. +++ b/core/modules/views/src/Plugin/views/display/DefaultDisplay.php
    @@ -51,7 +51,7 @@ public function isDefaultDisplay() {
        * information about the query. After execute, look in $view->result
    -   * for the array of objects returned from db_query.
    +   * for the array of objects returned from ::query.
        *
    

    above we have an example that does self::processField(), this just does ::query and it's also missing (), we should probably make that a bit more consistent (not 100% sure that self or so is actually valid in a docblock.

  3. +++ b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php
    @@ -35,7 +35,7 @@ public function testRegression_310447() {
       /**
    -   * Tests the db_table_exists() function.
    +   * Tests the Schema::tableExists() function.
        */
    

    method?

voleger’s picture

Status: Needs work » Needs review
FileSize
21.62 KB
1.86 KB

Addressed #41

Status: Needs review » Needs work

The last submitted patch, 42: 2849745-42.patch, failed testing. View results

voleger’s picture

Status: Needs work » Needs review

Unrelated test fails. Ready for review.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The patch looks good to me.
The 3 nitpicks from @Berdir are addressed.
The patch is RTBC for me.

catch’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/includes/database.inc
    @@ -21,11 +21,12 @@
    + * \Drupal::database()->select() instead.
      *
      * Do not use this function for INSERT, UPDATE, or DELETE queries. Those should
    - * be handled via db_insert(), db_update() and db_delete() respectively.
    + * be handled via \Drupal::database()->insert(), \Drupal::database()->update()
    + * and \Drupal::database()->delete() respectively.
      *
    

    A bit out of scope but this could also point to \Drupal::database()->merge()

  2. +++ b/core/includes/database.inc
    @@ -382,14 +383,15 @@ function db_escape_field($field) {
    + * use \Drupal::database()->like() on all supported database systems. Using
    

    \Drupal::database()->like() isn't a method, should probably be \Drupal::database()->escapeLike(). Needs work for that.

voleger’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
21.67 KB
1.26 KB

Addressed #46
Back to RTBC

catch’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/includes/database.inc
@@ -21,11 +21,12 @@
+ * \Drupal::database()->merge()and \Drupal::database()->delete() respectively.

This is fine now except 'respectively' doesn't work because there's not a 1-1 match, so we should just remove that word.

voleger’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
21.69 KB
615 bytes

Addressed #48

  • catch committed 383b06f on 8.8.x
    Issue #2849745 by voleger, yogeshmpawar, gaurav.kapoor, Berdir,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 383b06f and pushed to 8.8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.