Support from Acquia helps fund testing for Drupal Acquia logo

Comments

voleger created an issue. See original summary.

voleger’s picture

Title: Replace all calls to db_query [core/test/Database] » Replace all calls to db_query [core/test/../Database]
Assigned: voleger » Unassigned
Status: Active » Needs review
FileSize
60.9 KB
voleger’s picture

Reformatted changed lines. Tried to increase readability.

voleger’s picture

The last submitted patch, 3: replace_all_db_query_calls-2939387-03-8.6.x.patch, failed testing. View results

voleger’s picture

Issue summary: View changes

grep -inr -e "db_query" core/tests/Drupal/KernelTests/Core/Database

apaderno’s picture

Should not tests avoid using \Drupal and use $this->container instead?

voleger’s picture

https://www.drupal.org/core/scope#examples
In this scope, the goal is just replacements. Any other changes should be done in the followup issues.

voleger’s picture

Hm, almost all tests extend DatabaseTestBase so it's not so hard to replace that calls.
And this patch contains core/tests/Drupal/KernelTests/Core/Database/* files only.
@kiamlaluno thank's for the suggestion.

apaderno’s picture

Status: Needs review » Needs work
-    $result = db_query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', [
+    $result = $this->database
+      ->query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', [
       ':a1' => 'The age of ',
       ':a2' => ' is ',
       ':a3' => '.',

The indentation is wrong. It should be the following one.

-    $result = db_query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', [
+    $result = $this->database
+      ->query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', [
+        ':a1' => 'The age of ',
+        ':a2' => ' is ',
+        ':a3' => '.',

For the rest the patch is fine.

If somebody else doesn't find other mistakes, the patch for me is ready to RTBC.

voleger’s picture

apaderno’s picture

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

Needs reroll

voleger’s picture

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

Status: Needs review » Needs work

The last submitted patch, 16: replace_all_db_query_calls-2939387-16-8.6.x.patch, failed testing. View results

voleger’s picture

voleger’s picture

Title: Replace all calls to db_query [core/test/../Database] » [PP-1] Replace all calls to db_query [core/test/../Database]
Status: Needs work » Postponed
mondrake’s picture

Title: [PP-1] Replace all calls to db_query [core/test/../Database] » Replace all calls to db_query [core/test/../Database]
Status: Postponed » Needs review

Blocker was committed, un-postponing

mondrake’s picture

  1. +++ b/core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php
    @@ -125,7 +129,7 @@ public function testLikeBackslash() {
       public function testGetFullQualifiedTableName() {
    -    $database = \Drupal::database();
    +    $database = $this->connection;
         $num_matches = $database->select($database->getFullQualifiedTableName('test'), 't')
    

    I understand this should minimize impact, but it seems strange to initialise a local $database variable here. Can't we just use $this->connection?

  2. +++ b/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php
    @@ -17,11 +17,20 @@ class LoggingTest extends DatabaseTestBase {
     
    -    db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
    -    db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'])->fetchCol();
    +    call_user_func_array(
    +      [$this->connection, 'query'],
    +      ['SELECT name FROM {test} WHERE age > :age', [':age' => 25]]
    +    )->fetchCol();
    +    call_user_func_array(
    +      [$this->connection, 'query'],
    +      ['SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo']]
    +    )->fetchCol();
    

    I don't think this is correct in this case - I'd just use $this->connection->query instead. The call_user_func_array is correct a few lines below because in that case we want to test a call that does not have a file in the backtrace.

  3. +++ b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php
    index e239098715..775af1d3de 100644
    --- a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
    
    --- a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
    +++ b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
    
    +++ b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
    +++ b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
    @@ -21,6 +21,22 @@ class SchemaTest extends KernelTestBase {
    
    @@ -21,6 +21,22 @@ class SchemaTest extends KernelTestBase {
        */
       protected $counter;
     
    +  /**
    +   * Database service.
    +   *
    +   * @var \Drupal\Core\Database\Connection
    +   */
    +  protected $database;
    +
    +  /**
    +   * {@inheritdoc}
    +   */
    +  protected function setUp() {
    +    parent::setUp();
    +    $this->database = $this->container->get('database');
    +  }
    +
    +
    

    Uhm is there a reason we extend from KernelTestBase here and not from DatabaseTestBase? In that case that would not be needed and we could just use $this->connection... here too.

Status: Needs review » Needs work

The last submitted patch, 20: replace_all_db_query_calls-2939387-20-8.6.x.patch, failed testing. View results

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

Status: Needs work » Closed (duplicate)

So we will work on the scripted patch.
Follow #2875394: Replace all calls to db_query, which is deprecated