Problem/Motivation

Coming from #3120096-135: Support contrib database driver directories in a fixed location in a module.

This happens if a database driver is provided by a module, since this bit introduced by the parent

+++ b/core/lib/Drupal/Core/Database/Database.php
@@ -457,18 +462,130 @@ public static function convertDbUrlToConnectionInfo($url, $root) {
-    // Discover if the URL has a valid driver scheme. Try with custom drivers
-    // first, since those can override/extend the core ones.
-    $connection_class = $custom_connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection";
-    if (!class_exists($connection_class)) {
-      // If the URL is not relative to a custom driver, try with core ones.
-      $connection_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
+    // Determine if the database driver is provided by a module.
+    $module = NULL;
+    $connection_class = NULL;
+    $url_components = parse_url($url);
+    if (isset($url_components['query'])) {
+      parse_str($url_components['query'], $query);
+      if ($query['module']) {
+        $module = $query['module'];
+        // Set up an additional autoloader. We don't use the main autoloader as
+        // this method can be called before Drupal is installed and is never
+        // called during regular runtime.
+        $namespace = "Drupal\\$module\\Driver\\Database\\$driver";
+        $psr4_base_directory = Database::findDriverAutoloadDirectory($namespace, $root, TRUE);
+        $additional_class_loader = new ClassLoader();
+        $additional_class_loader->addPsr4($namespace . '\\', $psr4_base_directory);
+        $additional_class_loader->register(TRUE);
+        $connection_class = $custom_connection_class = $namespace . '\\Connection';
+      }
+    }

when the $root is null will pass that in ::findDriverAutoloadDirectory which in turn will try to create an instance of ExtensionDiscovery - but the constructor of that method requires $root to be a string, non-nullable.

This is not failing on DrupalCI because you need to run the Cache group of test with a module-based contrib db driver.

Proposed resolution

The fix is to ensure that the app root is setup before calling the chain of methods.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

mondrake created an issue. See original summary.

mondrake’s picture

mondrake’s picture

StatusFileSize
new875 bytes
mondrake’s picture

Status: Active » Needs review
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Assigned: Unassigned » mondrake
Status: Needs review » Needs work

#3 is not enough, since it still fails with

1) Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::testEntitySave
Error: Call to undefined function Drupal\Core\drupal_valid_test_ua()
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:378
/home/travis/drupal8/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php:190
/home/travis/drupal8/core/lib/Drupal/Core/Database/Database.php:576
/home/travis/drupal8/core/lib/Drupal/Core/Database/Database.php:477
/home/travis/drupal8/core/tests/Drupal/KernelTests/KernelTestBase.php:439
/home/travis/drupal8/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php:178
/home/travis/drupal8/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php:35
/home/travis/drupal8/vendor/phpunit/phpunit/src/Framework/TestResult.php:691
mondrake’s picture

StatusFileSize
new1022 bytes
mondrake’s picture

Issue summary: View changes
mondrake’s picture

#7 is not good, either:

1) Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::testEntitySave
Error: Class '\Drupal\database_statement_monitoring_test\dbal\Connection' not found
/home/travis/drupal8/core/lib/Drupal/Core/Database/Database.php:374
/home/travis/drupal8/core/lib/Drupal/Core/Database/Database.php:169
/home/travis/drupal8/core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php:40
/home/travis/drupal8/core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php:31
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:1374
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:1583
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:1215
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:897
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:470
/home/travis/drupal8/core/tests/Drupal/KernelTests/KernelTestBase.php:352
/home/travis/drupal8/core/tests/Drupal/KernelTests/KernelTestBase.php:242
/home/travis/drupal8/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php:34
/home/travis/drupal8/vendor/phpunit/phpunit/src/Framework/TestResult.php:691
mondrake’s picture

Status: Needs work » Needs review
StatusFileSize
new1.49 KB
mondrake’s picture

#10 still not good, because even trying to format the error message with the driver name ends up in 'class not found' error.

mondrake’s picture

StatusFileSize
new1.44 KB
alexpott’s picture

This test is done in a really really odd way. I think it deserves a rewrite. The way it is changing the database namespace is entirely unsupported.

mondrake’s picture

#12 works, for me.

mondrake’s picture

Assigned: mondrake » Unassigned
alexpott’s picture

Another option here is to check for core drivers. In order to use this test contrib drivers already have to extend the test so this all feels very complex.

daffie’s picture

Status: Needs review » Needs work

I think that we should not do the patch from comment #12. As far as I can see the test will be skipped for any contrib driver. And for me this is wrong. This is testing a major bug fix with a single test class. For me any contrib driver should pass this test. A contrib driver should extend this test. Add there own version of the helper module "database_statement_monitoring_test" and override the method getDatabaseConnectionInfo().
Or we can rewrite this test as @alexpott is suggesting.

alexpott’s picture

Given #17 can we design the test so that it passes for core and fails for a contrib driver unless it finds the necessary class. That way we achieve the desired outcome of making it easier for contrib drivers to run this test and know that they should have it passing.

mondrake’s picture

StatusFileSize
new1.19 KB

Rerolled.

daffie’s picture

@mondrake: In #3124354-11: Remove unnecessary classes from the database drivers @alexpott made a suggestion how to improve this test. Can you take a look at it and see if it works for your contrib driver?

mondrake’s picture

@daffie maybe, yes. #20 is just there to have a patch to load in local testing. Once #3124354: Remove unnecessary classes from the database drivers will be green I'll try to switch to that patch.

mondrake’s picture

#3124354: Remove unnecessary classes from the database drivers got committed, but that did not solve the issue here.

daffie’s picture

I think that this issue needs #3129043: Move core database drivers to modules of their own to land. The patch makes a code change to EndOfTransactionQueriesTest:

diff --git a/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php b/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php
index 7ff31796d4..1a0e4f6a7d 100644
--- a/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php
@@ -31,12 +31,12 @@ class EndOfTransactionQueriesTest extends KernelTestBase {
    * {@inheritdoc}
    */
   protected function setUp(): void {
+    parent::setUp();
+
     if (!class_exists($this->getDatabaseConnectionInfo()['default']['namespace'] . '\Connection')) {
       $this->markTestSkipped(sprintf('No logging override exists for the %s database driver. Create it, subclass this test class and override ::getDatabaseConnectionInfo().', $this->getDatabaseConnectionInfo()['default']['driver']));
     }
 
-    parent::setUp();
-
     $this->installSchema('system', 'sequences');
     $this->installEntitySchema('entity_test');
     $this->installEntitySchema('user');

For the test to work for contrib database drivers, the contrib drivers need to add their own fake database_statement_monitoring_test driver in the module part of the driver. See #3128699: Testing issue for pgsql_fallback.

mondrake’s picture

Status: Needs work » Postponed

Thanks for feedback @daffie. Postponing then.

daffie’s picture

Status: Postponed » Closed (duplicate)
mondrake’s picture

Status: Closed (duplicate) » Needs work

Thanks @daffie @Beakerboy, unfortunately #3152003: EndOfTransactionQueriesTest does not include bootstrap.inc early enough for contrib database drivers did not fix the issue here, nor I expect #3151118: Include bootstrap.inc using composer will.

The problem is with parent::setUp(); really regardless of where it is positioned, because if the db driver has not registered a \Drupal\database_statement_monitoring_test\{driver}\Connection class, we get the following failure:

1) Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::testEntitySave
Error: Class '\Drupal\database_statement_monitoring_test\dbal\Connection' not found
/home/travis/drupal8/core/lib/Drupal/Core/Database/Database.php:380
/home/travis/drupal8/core/lib/Drupal/Core/Database/Database.php:169
/home/travis/drupal8/core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php:40
/home/travis/drupal8/core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php:31
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:1374
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:1583
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:1215
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:897
/home/travis/drupal8/core/lib/Drupal/Core/DrupalKernel.php:470
/home/travis/drupal8/core/tests/Drupal/KernelTests/KernelTestBase.php:351
/home/travis/drupal8/core/tests/Drupal/KernelTests/KernelTestBase.php:241
/home/travis/drupal8/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php:34
/home/travis/drupal8/vendor/phpunit/phpunit/src/Framework/TestResult.php:691

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mondrake’s picture

Status: Needs work » Closed (outdated)