Error with Drush : Bootstrap phase bootstrapDrupalDatabase() failed to validate

Created on 2 June 2023, over 1 year ago
Updated 20 June 2023, over 1 year ago

Problem/Motivation

It applies seemingly to all drush commands.

When I launch drush list -vvv or drush status -vvv I have the folowing message in the console :

[info] key_value table not found. Database may be empty. [0.19 sec, 9.62 MB]
[debug] Bootstrap phase bootstrapDrupalDatabase() failed to validate; continuing at bootstrapDrupalConfiguration() [0.19 sec, 9.62 MB]

Furthermore, a lot of drush commands with drush list are not available, only _global, archive, cache, core, php, pm (but not pm:install or pm:uninstall), site, sql and updatedb.

As expected, the drush config:import does not work :

Command config:import was not found. Drush was unable to query the database. As a result, many commands are unavailable. Re-run your command with --debug to see relevant log messages.

<!--break-->

First, I opened an issue on the Drush repo but they told me it was not on their side. Looking further, I think that the problem is the Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::tableExists() function

Steps to reproduce

Launch the drush list => only part of the commands are available

Launch drush config:import, drush config:export => "Command config:import was not found. Drush was unable to query the database."

My website works well otherwise, the database is well connected and I have no errors in the back-end

Here are my system configuration:

  • sqlsrv module version: 4.3.2
  • Drush version: 11.5.1
  • Drupal version: 9.3.22
  • PHP version: 7.4.3
  • OS : Windows 10
  • Database: SQLServer version 15.00.2000

Proposed resolution

  1. In Drush\Boot\DrupalBoot8::bootstrapDrupalDatabaseValidate(), there is the following test that returns FALSE while the table exists:

           if (!$connection->schema()->tableExists('key_value')) {
                $this->logger->info('key_value table not found. Database may be empty.');
                return false;
            }
    
  2. In Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::tableExists(), I have noticed that the getPrefixIndo() function returned an array with a schema key to NULL. This is why the $connection->schema()->tableExists('key_value') returns FALSE
  3. The getPrefixInfo() is not implemented in Drupal\sqlsrv\Driver\Database\sqlsrv\Schema but in Drupal\Core\Database\Schema::getPrefixInfo(). In the first lines we have :
        $info = [
          'schema' => $this->defaultSchema,
          'prefix' => $this->connection->tablePrefix($table),
        ];
    


    In the Drush command context, $this->defaultSchema is not set and returns NULL

  4. I have noticed that there is a the Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::getDefaultSchema() that makes the job :

        if (!isset($this->defaultSchema)) {
          $result = $this->connection->queryDirect("SELECT SCHEMA_NAME()")->fetchField();
          $this->defaultSchema = $result;
        }
        return $this->defaultSchema;
    
  5. I updated the Drupal\sqlsrv\Driver\Database\sqlsrv\Schema class by adding the getPrefixInfo() method from the Drupal\Core\Database\Schema::getPrefixInfo() with only the replacement of $this->defaultSchema by $this->getDefaultSchema()

Now, the drush commands work perfectly

Remaining tasks

Check if this fix has no side effect

User interface changes

N/A => to check

API changes

N/A => to check

Data model changes

N/A => to check

πŸ› Bug report
Status

Fixed

Version

4.3

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024