Problem/Motivation
The problem with database queries is that when they get more complicated, the sql string can differ depending on the database. Dynamic queries let the Drupal database driver generate the sql string and therefore has more flexibility in the resulting sql string. A static query is just an sql string and therefore has no flexibility in making small adjustments for a specific database. The static queries that we use in core work for all the by core supported databases (MySQL, PostgreSQL and SQLite). They may work or not for other databases. When we change them to dynamic queries, they should work for a lot more/all databases. 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/modules/{every module}/tests" and its sub-directories. Any query that is specific for only one database should not be changed.
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 |
---|---|---|---|
#27 | 3160267-8.9.x-26.patch | 49.39 KB | alexpott |
#27 | 24-26-interdiff.txt | 817 bytes | alexpott |
#24 | 3160267-23.patch | 49.4 KB | alexpott |
#21 | interdiff-19-21.txt | 2.28 KB | jungle |
#21 | 3160267-21.patch | 52.99 KB | jungle |
Comments
Comment #2
daffie CreditAttribution: daffie commentedComment #3
daffie CreditAttribution: daffie commentedThe same thing that we did in #3152398: Change static queries to dynamic queries in core/tests/Drupal, should be done in this issue.
Comment #4
nijolawrence CreditAttribution: nijolawrence as a volunteer commented80 occurrences of query() and 7 occurrences of queryRange() found.
I am working on it.
Comment #5
partyka CreditAttribution: partyka at Argonne National Laboratory commentednijolawrence -- that's a lot! do you have a list! maybe we could divide and conquer this issue
Comment #6
nijolawrence CreditAttribution: nijolawrence as a volunteer commented@partyka - I am sorry I didnot notice your message.
queryRange() - All 7 occurrences fixed
query() - All 67 occurrences fixed
The rest of 13 happened to be 9 Views query() and 4 xpath query().
Comment #7
partyka CreditAttribution: partyka at Argonne National Laboratory commentedNo worries, since there’s a lot, how did you determine this list?
Comment #9
nijolawrence CreditAttribution: nijolawrence as a volunteer commentedFixed errors.
I have reverted one of the static queries in core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php
Since this is trying to trigger a PDO exception should this be improved?
Comment #10
nijolawrence CreditAttribution: nijolawrence as a volunteer commented@partyka I have done a grep with '>query(' and '>queryRange('.
Please let me know if I missed something.
Comment #11
daffie CreditAttribution: daffie commented@nijolawrence: Tank you for your patch. It looks good and you did all the queries that needed change. I have only one remark. After that it is RTBC for me.
We need the change in core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php. Only that is not enough. Because we change the static query to a dynamic query we also must change:
Comment #12
nijolawrence CreditAttribution: nijolawrence as a volunteer commented@daffie Thanks for your feedback. I am working on making the change
Comment #13
nijolawrence CreditAttribution: nijolawrence as a volunteer commentedUpdated the patch with the change mentioned by @daffie. Also fixed the coding standard messages mentioned in the Automated Test.
Comment #14
daffie CreditAttribution: daffie commentedAll code changes look good to me.
All query() and queryRange() in the module tests have been replaced.
For me it is RTBC.
Comment #15
alexpottThis triggers a spelling mistake error. If you can this to cdp then it won't - we ignore words less than 4 characters which is fine for most aliases.
This change, whilst it looks correct, is out-of-scope
Comment #16
nijolawrence CreditAttribution: nijolawrence as a volunteer commented@alexpott Thanks for reviewing my changes.
1. I will make changes and upload a new patch.
2. Regarding this. The change in the static query in core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php was causing the tests to fail.
For fixing this I made the changes you mentioned. Please let me know how you want me to move forward with this. Should I revert changes to this file as well?
Comment #17
daffie CreditAttribution: daffie commented@nijolawrence: Just fix #15.1 and do not do #15.2, but add an explanation why the change is necessary.
Comment #18
alexpottRe #15.2 ah I see we're dealing with a difference in how quotes are handled. Fun. So yeah we only need to cope with #15.1
Comment #19
nijolawrence CreditAttribution: nijolawrence as a volunteer commented@alexpott @daffie
Fixed #15.1.
Added comment in front of #15.2.
Please review and let me know if any other changes are required.
Comment #20
daffie CreditAttribution: daffie commentedThe point of @alexpott has been addressed.
Back to RTBC.
Comment #21
jungleWrapped too early. Addressing and stay RTBC.
Comment #22
alexpottCommitted 3312c7b and pushed to 9.1.x. Thanks!
Comment #24
alexpottHere's the backport.
The following files were in conflict:
#2983395: user module's flood controls should do better logging added db queries to 9.1.x
Comment #25
alexpottCommitted bba4be9 and pushed to 9.0.x. Thanks!
Comment #27
alexpottQueries are not quoted in the same way in D8.
Comment #28
alexpottCommitted f06027d and pushed to 8.9.x. Thanks!