See #2848161: [meta] Replace calls to deprecated db_*() wrappers.

Replace all usages of db_query in core, except in code that is testing db_query.

Steps to create scripted patch:

  • Script to do the majority of the work
    # Replace the occurrences in code.
    find ./core/lib -type f -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
    find ./core/tests -type f -not -path "./core/tests/Drupal/KernelTests/Core/Database/*" -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
    find ./core/tests/Drupal/KernelTests/Core/Database -type f -not -path "./core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php" -not -path "./core/tests/Drupal/KernelTests/Core/Database/QueryTest.php" -exec sed -i -r 's/db_query\(/$this->connection->query\(/g;' {} \;
    find ./core/modules -type f -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
    find ./core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php -type f -exec sed -i -r "s/call_user_func_array\('db_query'/call_user_func_array\([\$this->connection, 'query']/g;" {} \;
    find ./core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php -type f -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
    find ./core/includes/form.inc -type f -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
    find ./core/scripts/ -type f -exec sed -i -r 's/db_query\(/\\Drupal::database\()->query\(/g;' {} \;
    # Start using \Drupal::database() or inject service
    find ./core/modules/tracker -type f -exec sed -i -r 's/Database::getConnection\(/\\Drupal::database\(/g;' {} \;
    
  • The files that requires to manually add use Drupal\Core\Database\Database; row. See interdiff at #26
  • Fix the replacements in the comments. See interdiff #27

Comments

adriancid created an issue. See original summary.

erozqba’s picture

StatusFileSize
new157.69 KB
erozqba’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: 2875394-2.patch, failed testing.

erozqba’s picture

Status: Needs work » Needs review
StatusFileSize
new158.05 KB
new2.05 KB
erozqba’s picture

StatusFileSize
new158.03 KB
new1.55 KB

The last submitted patch, 5: 2875394-5.patch, failed testing.

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.

voleger’s picture

Assigned: adriancid » Unassigned
StatusFileSize
new157.24 KB

reroll against 8.6.x

Status: Needs review » Needs work

The last submitted patch, 10: replace_all_db_query_calls-2875394-10-8.6.x.patch, failed testing. View results

voleger’s picture

StatusFileSize
new157.33 KB
new2.51 KB
voleger’s picture

Status: Needs work » Needs review
voleger’s picture

Maybe this patch should be chunked into the smaller patches? Like it was with #2924538: [META] Remove all usages of drupal_set_message and drupal_get_messages

voleger’s picture

Title: Replace all calls to db_query, which is deprecated » [meta] Replace all calls to db_query, which is deprecated
Issue summary: View changes
Status: Needs review » Active

Updated issue summary

voleger’s picture

voleger’s picture

Title: [meta] Replace all calls to db_query, which is deprecated » Replace all calls to db_query, which is deprecated
Issue summary: View changes

Returning to the usual task issue. Regarding https://www.drupal.org/core/scope#examples we should follow good scope.
So as @alexpott had mentioned in the followup issues, to reduce the complexity of maintaining huge patch, it should be scripted.

Initial replacements.

# Replace the occurrences in code.
find ./core/lib -type f -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
find ./core/tests -type f -not -path "./core/tests/Drupal/KernelTests/Core/Database/*" -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
find ./core/tests/Drupal/KernelTests/Core/Database -type f -not -path "./core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php" -exec sed -i -r 's/db_query\(/$this->connection->query\(/g;' {} \;
find ./core/modules -type f -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
voleger’s picture

Status: Active » Needs work
joshmiller’s picture

Working on scripting this patch. Here's what I have so far:

export find_string='db\_query[(]';
export replace_string='\\Drupal\:\:database[(][)]-\>query\(';
export regex='s/'$find_string'/'$replace_string'/g';
git grep -l 'db_query(' | xargs sed -i $regex;

It replaces all instances, which is unfortunate, because that replaces the `function db_query(` in database.inc as well. Once I figure out how to quickly revert that one change, I'll update this issue and close child issues.

EDIT: Whoops, looks like @voleger beat me to it (I worked on this last week, but just now getting around to posting it). His script looks more comprehensive, especially handling the object oriented test, etc.

voleger’s picture

Also, the script should avoid replacing function calls at tests for the function itself like Drupal\KernelTests\Core\Database\QueryTest

voleger’s picture

Next iteration.
Bulk replacements that cover all places where db_query function is used.

# Replace the occurrences in code.
find ./core/lib -type f -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
find ./core/tests -type f -not -path "./core/tests/Drupal/KernelTests/Core/Database/*" -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
find ./core/tests/Drupal/KernelTests/Core/Database -type f -not -path "./core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php" -not -path "./core/tests/Drupal/KernelTests/Core/Database/QueryTest.php" -exec sed -i -r 's/db_query\(/$this->connection->query\(/g;' {} \;
find ./core/modules -type f -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
find ./core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php -type f -exec sed -i -r "s/call_user_func_array\('db_query'/call_user_func_array\([\$this->connection, 'query']/g;" {} \;
find ./core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php -type f -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
find ./core/includes/form.inc -type f -exec sed -i -r 's/db_query\(/Database::getConnection\()->query\(/g;' {} \;
find ./core/scripts/ -type f -exec sed -i -r 's/db_query\(/\\Drupal::database\()->query\(/g;' {} \;
# Start using \Drupal::database() or inject service
find ./core/modules/tracker -type f -exec sed -i -r 's/Database::getConnection\(/\\Drupal::database\(/g;' {} \;

The next step should be to find out files where Database class not used in current namespace and then fix related errors.
That step also should be scripted.

Also I guess we shouldn't remove db_query function name from the next array in core/lib/Drupal/Core/Utility/Error.php:50 file:

      // The first element in the stack is the call, the second element gives us
      // the caller. We skip calls that occurred in one of the classes of the
      // database layer or in one of its global functions.
      $db_functions = ['db_query', 'db_query_range'];

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.

voleger’s picture

Let's see which files require additional attention.
Here's scripted patch from #22 script.
No interdiff. Patch was scripted.

voleger’s picture

Try manually to add usages of required Database class in files

voleger’s picture

StatusFileSize
new24 KB

Reupload interdiff

voleger’s picture

Fixed comments

voleger’s picture

Issue summary: View changes
voleger’s picture

Status: Needs work » Needs review

Need some review of #27 and ideas on how to fix the Migration and Logger tests.

The last submitted patch, 25: core-db_query_replace_scripted-2875394-25.patch, failed testing. View results

andypost’s picture

I think better to get in db_delete & db_merge then patch will be smaller because in some places there will be $connection variable

voleger’s picture

Title: Replace all calls to db_query, which is deprecated » [PP-3] Replace all calls to db_query, which is deprecated
Status: Needs review » Postponed
mondrake’s picture

There is a lot of calls to db_query('SELECT COUNT(*) ...');.

Find for example just those in test code with find . -type f -name '*Test.php' -exec grep -Ei "QUERY.*COUNT.*\(\*\)" {} \+.

We might convert those to $injected_database->select(...)->countQuery() and then the conversion here would be more digestible...

mondrake’s picture

voleger’s picture

Title: [PP-3] Replace all calls to db_query, which is deprecated » [PP-2] Replace all calls to db_query, which is deprecated
voleger’s picture

voleger’s picture

voleger’s picture

Title: Replace all calls to db_query, which is deprecated » [PP-2] Replace all calls to db_query, which is deprecated

Also, make it postponed on #2994904: Convert query('SELECT ... FROM {xxx}') to select('xxx')->... in tests that will convert some query into select calls

voleger’s picture

mondrake’s picture

Title: [PP-2] Replace all calls to db_query, which is deprecated » [PP-4] Replace all calls to db_query, which is deprecated
Related issues: +#3001216: Use the database.replica service where appropriate, +#3000931: Connection::query() does not support 'target' option
mondrake’s picture

mondrake’s picture

Title: [PP-4] Replace all calls to db_query, which is deprecated » [PP-3] Replace all calls to db_query, which is deprecated
mondrake’s picture

Title: [PP-3] Replace all calls to db_query, which is deprecated » [PP-2] Replace all calls to db_query, which is deprecated
Issue summary: View changes
mondrake’s picture

I do not think this is blocked on #2994904: Convert query('SELECT ... FROM {xxx}') to select('xxx')->... in tests - it would help for sure but can do without.

voleger’s picture

Title: [PP-2] Replace all calls to db_query, which is deprecated » Replace all calls to db_query, which is deprecated
Status: Postponed » Needs review
StatusFileSize
new163.98 KB

I agree those issues are not the blockers no more.

Scripting of the patch will not help because it also become outdated and it's complicated to script adding missing namespaces, loop construction situations, ::getConnection calls optimization, connections to the replica database.

Added rerolled patch.
Skipped D6 and D7 scripts, because they should be executed against related codebase version.
Skipped documentation updates for db_* functions, because they are deprecated and usually linked to deprecated db_* functions that are also deprecated.

Status: Needs review » Needs work

The last submitted patch, 47: db_query-2875394-47.patch, failed testing. View results

andypost’s picture

Yep, as other conversions landed let's get this one next before "count" issue in bikeshed

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new2.16 KB
new164.01 KB

I'm not sure about replacement in LoggingTest. Trying not pass test property in the callable array.

Status: Needs review » Needs work

The last submitted patch, 50: db_query-2875394-50.patch, failed testing. View results

mondrake’s picture

Title: Replace all calls to db_query, which is deprecated » [PP-1] Replace all calls to db_query, which is deprecated
Issue summary: View changes
Status: Needs work » Postponed

We need #2999962: Unify Database/Log::findCaller and Utility/Error::getLastCaller before we can address the LoggingTest issue.

voleger’s picture

As logger already works not properly, is it possible to fix an issue related to the LoggingTest in the followup issue? It will unblock a huge amount of replacements.

mondrake’s picture

As discussed in #2994904: Convert query('SELECT ... FROM {xxx}') to select('xxx')->... in tests, we probably need to have separate issues to convert db_query usages on entity related tables to use the Entity Query API, since neither converting to Connection::query nor to Connection::select seems the right thing to do in these cases.

voleger’s picture

Title: [PP-1] Replace all calls to db_query, which is deprecated » [PP-2] Replace all calls to db_query, which is deprecated
Issue summary: View changes

Since amounts replacements in this issue much more than replacements in #3012599: Replace all db calls to aggregator_feed and aggregator_item tables with Entity APIs and this issue already blocked by #2999962: Unify Database/Log::findCaller and Utility/Error::getLastCaller, I suggest postponing this issue also on #3012599: Replace all db calls to aggregator_feed and aggregator_item tables with Entity APIs as there are no blockers.

voleger’s picture

Issue summary: View changes

Oops wrong issue

voleger’s picture

Title: [PP-2] Replace all calls to db_query, which is deprecated » [PP-1] Replace all calls to db_query, which is deprecated
Issue summary: View changes
voleger’s picture

voleger’s picture

StatusFileSize
new143.58 KB
mondrake’s picture

IMHO, we should also decide on the fate of #2994904: Convert query('SELECT ... FROM {xxx}') to select('xxx')->... in tests before unpostponing - with that in, this patch would be 100kb+ smaller and much easier to review.

berdir’s picture

@voleger: I think the #2999962: Unify Database/Log::findCaller and Utility/Error::getLastCaller is tricky to resolve, is this really a hard blocker, can't we just keep those duplicated pieces of code and update them separately?

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

berdir’s picture

Title: [PP-1] Replace all calls to db_query, which is deprecated » Replace all calls to db_query, which is deprecated
Status: Postponed » Needs review
StatusFileSize
new3.17 KB
new142.66 KB

I'm still not sure about blocking issue and it just feels hard to review for me.

Why don't we just fix it like this? Took me a while to understand it, but the trick as much as I understand it really is to find the last call of the backtrace that's still *inside* the database system and then report the next, but the line/file is actually on this part.

We already check for the db_* function not being the next, so as far as I see, we just need to change the namespace check to the same. That means it behave consistently, whether or not the call goes through db_query(). Also added explicit tests for that.

voleger’s picture

StatusFileSize
new140.42 KB
new6.31 KB

Nice. +1 for the fix.
Cleanup of unused use definitions. Updated deprecation messages to fix CS issues.
Fixed typo and comments CS issue.

thalles’s picture

Looks good!


+++ b/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php
@@ -13,19 +13,30 @@ class LoggingTest extends DatabaseTestBase {
+    db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();

But why not use the $this->connection->query?

voleger’s picture

Status: Needs review » Reviewed & tested by the community

This line will test the logging of procedural call of the db_query function as we introduce the fix for the logging service. BTW this test in the legacy group, so that line will be removed from drupal:9.0.0.

thalles’s picture

Thanks @voleger!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 66: db_query-2875394-66.patch, failed testing. View results

berdir’s picture

Status: Needs work » Reviewed & tested by the community

Still applies and test is still green, looks like another random test fail in MediaLibraryTest?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 66: db_query-2875394-66.patch, failed testing. View results

yogeshmpawar’s picture

Status: Needs work » Reviewed & tested by the community

All looks good so setting back to RTBC! Tests failure is not related (see #3055648: Frequent random fail in \Drupal\Tests\media_library\FunctionalJavascript\MediaLibraryTest).

voleger’s picture

Issue summary: View changes

Updated IS

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 66: db_query-2875394-66.patch, failed testing. View results

voleger’s picture

Status: Needs work » Reviewed & tested by the community
voleger’s picture

Issue tags: +dckyiv2019
catch’s picture

Status: Reviewed & tested by the community » Needs work

Needs a re-roll.

berdir’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new140.43 KB
new1.91 KB

Two conflicts in EntityApiTest. Adding a patch diff to show that there are only context changes compared to the last patch and setting back to RTBC.

voleger’s picture

Rerolled
Updated changes in db_query function definition. See #3000931: Connection::query() does not support 'target' option

voleger’s picture

StatusFileSize
new140.56 KB

And actual rerolled patch

larowlan’s picture

adding review credit for mondrake for co-ordination and management tasks

  • larowlan committed 275c864 on 8.8.x
    Issue #2875394 by voleger, erozqba, Berdir, mondrake: Replace all calls...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed 275c864 and pushed to 8.8.

🎉

Status: Fixed » Closed (fixed)

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