Problem/Motivation
We should be using static queries as little as possible, because they can result into problems for a some databases. A static query may work on a number of databases, but not on an other one. Dynamic queries are a little bit slower then static queries and for use in tests that is not a problem.
Proposed resolution
Change the static queries in the directory "core/tests/Drupal/FunctionalTests" and its sub-directories. Also do the same when in the directory "core/tests/Drupal/KernelTests" and its sub-directories, but not in the directory "core/tests/Drupal/KernelTests/Core/Database".
What needs to be changed:
From:
$connection->query('SELECT nid FROM {node} WHERE nid = :id', [':id' => 1']);
To:
$connection->select('node')
->fields('node', ['nid'])
->condition('nid', 1)
->execute();
Remaining tasks
TBD
User interface changes
None
API changes
None
Data model changes
None
Release notes snippet
TBD
Comment | File | Size | Author |
---|---|---|---|
#44 | interdiff_36_44.txt | 1.66 KB | ultrabob |
#44 | 3152398-44.patch | 13.24 KB | ultrabob |
#21 | 3152398-21.patch | 14.34 KB | rajandro |
Comments
Comment #2
rik-dev CreditAttribution: rik-dev at DevBranch commentedComment #3
mondrakePersonally, absolutely +1 on the motivation here, but please anyone working on this be aware this was attempted in #2994904: Convert query('SELECT ... FROM {xxx}') to select('xxx')->... in tests, and finally rejected. Wish you better luck :)
Comment #4
rik-dev CreditAttribution: rik-dev at DevBranch commentedComment #5
adityasingh CreditAttribution: adityasingh as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedComment #6
daffie CreditAttribution: daffie commentedThe parent issue is a "bug report", therefore this issue is one too.
Comment #7
adityasingh CreditAttribution: adityasingh as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedUnassigning from myself because i am stuck in other issues.
Comment #8
siddhant.bhosale CreditAttribution: siddhant.bhosale as a volunteer and at QED42 commentedComment #9
siddhant.bhosale CreditAttribution: siddhant.bhosale as a volunteer and at QED42 commentedI have replaced the static queries with dynamic queries from
core/tests/Drupal/FunctionalTests and its sub-folder
,core/tests/Drupal/KernelTests and its sub-folders
. Please review.Comment #10
adityasingh CreditAttribution: adityasingh as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedHi @siddhant.bhosale thanks for patch
Patch looks good for me and cleanly applied.
Comment #11
siddhant.bhosale CreditAttribution: siddhant.bhosale as a volunteer and at QED42 commented@adityasingh Thanks for reviewing the patch. Can you please change the status to RTBC ?
Comment #12
daffie CreditAttribution: daffie commentedPatch look good, only I have some remarks:
You do not need to say 'c.name'. Just 'name' is enough.
This change is out of scope and wrong. It is an empty docblock.
Can we make change a multiline change for better readability. Also 'kvp.collection' can be changed to just 'collection'.
The part "sql_m." an be removed from each line.
- core/tests/Drupal/FunctionalTests/Installer/InstallerDatabaseErrorMessagesTest.php;
- core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php;
- core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php;
- core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php.
Comment #13
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedWorking on this.
Comment #14
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedHere I have tried to address comment #12.
It still needs work to remove the static query from:
-core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php
Comment #15
rajandro CreditAttribution: rajandro as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedI am working on this
Comment #16
daffie CreditAttribution: daffie commentedPatch looks good:
Can the
->fields()
part and the->condition()
part have their own line.Can we change 'tr.name' to just 'name' in the condition method.
Comment #17
Hardik_Patel_12 CreditAttribution: Hardik_Patel_12 at QED42 for Drupal India Association commentedKindly review a patch , covered points #16.1 , #16.2 and #16.4 as suggested by @daffie. #16.3 is still needs work.
Comment #18
Hardik_Patel_12 CreditAttribution: Hardik_Patel_12 at QED42 for Drupal India Association commentedKindly find proper interdiff , last one was diff of diff kindly avoid that.
Comment #19
daffie CreditAttribution: daffie commentedCan the 'tr.name' be changed to just 'name'.
Comment #20
Hardik_Patel_12 CreditAttribution: Hardik_Patel_12 at QED42 for Drupal India Association commentedI don't know somehow i have missed this , changing now 'tr.name' to just 'name'.
Comment #21
rajandro CreditAttribution: rajandro as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedHi @Hardik_Patel_12, I was working on the same issue as you can notice on #15! However, I have checked your patch as well and this is looking fine to me. I am adding the missing point #16.3 and moving this it NR.
Comment #22
daffie CreditAttribution: daffie commentedAll my points are addressed.
All code changes look good to me.
For me it is RTBC.
Comment #23
Hardik_Patel_12 CreditAttribution: Hardik_Patel_12 at QED42 for Drupal India Association commented@rajandro , this was my bad but it was unintentional. Will make sure in future that if someone is working on some issue then not to pick that issue.
Comment #25
daffie CreditAttribution: daffie commentedBack to RTBC.
Comment #31
alexpottCommitted 65656ac660 and pushed to 9.1.x. Thanks!
I'll backport to 9.0.x and maybe 8.9.x if the tests are green to keep tests aligned.
Re #3 I think the statement in the issue summary that using query strings like this makes it harder for contrib db drivers it true so yeah let's do this now and credit people who worked on #2994904: Convert query('SELECT ... FROM {xxx}') to select('xxx')->... in tests
Out-of-scope changes. While these changes are correct they are out-of-scope for changing static queries to dynamic queries. I've reverted them from the committed patch. Please don't make such changes in future patches it increases the amount a reviewer has to review.
Comment #34
alexpottI had to revert this issue because it broke sqlite testing...
Comment #35
alexpottComment #36
alexpottComment #37
alexpottThis should make the following change to \Drupal\KernelTests\KernelTestBaseTest::tearDown
i.e. swap the if around. We can use a static query here because we're hardcoding the db type. And the if this way around makes it more obvious.
Comment #38
daffie CreditAttribution: daffie commentedThis is something a novice can do. Make the change to the method tearDown() in the file "core/tests/Drupal/KernelTests/KernelTestBaseTest.php" as suggested by @alexpott. The query does not need to be changed to a dynamic one, because it is only run for SQLite.
Comment #39
pratik_kambleComment #40
pratik_kambleComment #41
andypostWould be great to elaborate in IS why change to alterable query makes things less fragile.
In my experience dynamic query (especially to entity tables) are evil because some unpredictable alters are fire and every query needs to skip access when running tests.
Comment #42
ultrabob CreditAttribution: ultrabob commentedI can't address #41, but I'll work on #37
Comment #43
daffie CreditAttribution: daffie commentedI am not sure what you would like @andypost. We are changing the queries to dynamic so that they work for contrib database drivers. Some static queries do not work for all contrib database drivers. With dynamic queries that is not a problem.
Queries on entity tables should as much as possible be changed to EntityQueries. We have an issue for that. See #3014948: [META] Replace all direct db calls to entity tables with Entity API.
Comment #44
ultrabob CreditAttribution: ultrabob commentedI've made the changes suggested by @alexpott in #37.
Comment #45
daffie CreditAttribution: daffie commentedThe change looks good to me.
It is the change that was requested by @alexpott.
Back to RTBC.
Comment #46
alexpottCommitted fe38a9c and pushed to 9.1.x. Thanks!