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.

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

Issue fork sqlsrv-3364384

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ncac created an issue. See original summary.

pstewart’s picture

Status: Active » Needs work

I've taken a quick look at this in my D10 test environment and am not seeing the problem, but I'm guessing this maybe that with drush + D9.3 tableExists is getting hit before something else that calls into getDefaultSchema which causes the error, while with drush + D10 it seems that getDefaultSchema is getting hit before tableExists so the bug doesn't manifest (@ncac I'm guessing it's a similar reason why you're seeing the problem in drush but not on the site itself). The problem has almost certainly materialised due to the change in #3259155 which now does check the schema explicitly in tableExists (although I realise now that the solution there is incomplete as we should be doing the same check in fieldExists, that's a separate problem).

Implementing getPrefixInfo is definitely the way to go here, however I think we can simplify a bit by just calling getDefaultSchema to ensure the defaultSchema variable is set, then delegating back to the parent.

I'll take a closer look next week when I have some more time to look at Drupal stuff.

arne_hortell’s picture

About 15 hours ago my development site failed to connect to database, i had dev-4.3-x 4288276 which have been working flawlessly a few weeks. After upgrading to dev-4.4-x 3362d7f it works perfectly again.
Thanks pstewart for your efforts.

beakerboy’s picture

My memory could be wrong, but I think at one point drush had its own implementation of each database driver instead of using the Drupal classes. If this is the case, drush would need to be patched to support sqlsrv.

beakerboy’s picture

It looks like I already have a project for this:
https://www.drupal.org/project/drush_sqlsrv

ncac’s picture

@Beakerboy: In the context of this issue, it seems to be the database driver defined in my settings.php

[
  'driver' => 'sqlsrv',
  'namespace' => 'Drupal\\sqlsrv\\Driver\\Database\\sqlsrv',
  'autoload' => 'modules/contrib/sqlsrv/src/Driver/Database/sqlsrv'
]

which is used in Drush\Boot\DrupalBoot8::bootstrapDrupalDatabaseValidate() invoking Drupal\Core\Database\Database::getConnection()

arne_hortell’s picture

It turned out that website works great but drush is totally handicapped and cant connect to database.
Everything worked on friday...
Any ideas?

arne_hortell’s picture

https://www.drupal.org/files/issues/2023-06-02/diff.txt

The above fix from ncac solved my problem, now all works again

arne_hortell’s picture

StatusFileSize
new1.41 KB
pstewart’s picture

@arne_hortell can you confirm which Drupal and Drush versions you were running when you experienced the Drush errors?

pstewart’s picture

Status: Needs work » Needs review

I've pushed a change to the issue fork to simplify the fix, which seemsto be working ok in a D9 test environment. Please can those affected try the issue fork patch to test?

arne_hortell’s picture

@pstewart Drupal 9.5.8 on Apache, drush 11.5.1

arne_hortell’s picture

@pstewart patch 84.diff (your file) seems to work great

arne_hortell’s picture

StatusFileSize
new1.42 KB

For whatever reason, patch 3364384 doesnt work with my composer so i made a new patch according to
https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa...

pstewart’s picture

@arne_hortell your patch from #15 appears to be the same as @ncac's original patch apart from some trailing newlines, did the revised patch from the merge request actual work for you in the end?

I have now managed to recreate this error on my D10 development environment by explicitly removing the schema key from the database settings in settings.php, then clearing all caches from the web interface. Drush then failed as described above. I then applied the diff from MR84 and confirmed that it resolved the problem.

@ncac if you're able to confirm the current diff from MR84 is working then that would be great, however I'm pretty satisfied this fix is the way to go so if I don't hear back I'll go ahead and commit this and make a new release.

arne_hortell’s picture

It turned out there was another issue, totally unrelated that made the #15.
Now all is fine and im looking forward to you r new release @pstewart

ncac’s picture

I can confirm that, on my side, every Drush command work with the issue fork patch. Thank you @pstewart

pstewart’s picture

Status: Needs review » Reviewed & tested by the community

OK, let's get this committed and tag a new release

  • pstewart committed ede86907 on 4.3.x
    Issue #3364384 by ncac, arne_hortell, pstewart: Error with Drush :...
pstewart’s picture

Status: Reviewed & tested by the community » Fixed
solideogloria’s picture

Per #3366370: Error with Drush : Bootstrap phase bootstrapDrupalDatabase() failed to validate (4.4.x branch), shouldn't this also get committed on the 4.x branch?

Edit: Despite it not showing a commit, it looks like the beta was forked after the commit. So never mind.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.