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
- In
Drush\Boot\DrupalBoot8::bootstrapDrupalDatabaseValidate(), there is the following test that returnsFALSEwhile the table exists:
if (!$connection->schema()->tableExists('key_value')) { $this->logger->info('key_value table not found. Database may be empty.'); return false; } - In
Drupal\sqlsrv\Driver\Database\sqlsrv\Schema::tableExists(), I have noticed that thegetPrefixIndo()function returned an array with aschemakey toNULL. This is why the$connection->schema()->tableExists('key_value')returnsFALSE - The
getPrefixInfo()is not implemented inDrupal\sqlsrv\Driver\Database\sqlsrv\Schemabut inDrupal\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->defaultSchemais not set and returnsNULL - 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; - I updated the
Drupal\sqlsrv\Driver\Database\sqlsrv\Schemaclass by adding thegetPrefixInfo()method from theDrupal\Core\Database\Schema::getPrefixInfo()with only the replacement of$this->defaultSchemaby$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
| Comment | File | Size | Author |
|---|
Issue fork sqlsrv-3364384
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
Comment #2
pstewart commentedI'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
tableExistsis getting hit before something else that calls intogetDefaultSchemawhich causes the error, while with drush + D10 it seems thatgetDefaultSchemais getting hit beforetableExistsso 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 intableExists(although I realise now that the solution there is incomplete as we should be doing the same check infieldExists, that's a separate problem).Implementing
getPrefixInfois definitely the way to go here, however I think we can simplify a bit by just callinggetDefaultSchemato ensure thedefaultSchemavariable 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.
Comment #3
arne_hortell commentedAbout 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.
Comment #4
beakerboyMy 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.
Comment #5
beakerboyIt looks like I already have a project for this:
https://www.drupal.org/project/drush_sqlsrv
Comment #6
ncac commented@Beakerboy: In the context of this issue, it seems to be the database driver defined in my settings.php
which is used in
Drush\Boot\DrupalBoot8::bootstrapDrupalDatabaseValidate()invokingDrupal\Core\Database\Database::getConnection()Comment #7
arne_hortell commentedIt turned out that website works great but drush is totally handicapped and cant connect to database.
Everything worked on friday...
Any ideas?
Comment #8
arne_hortell commentedhttps://www.drupal.org/files/issues/2023-06-02/diff.txt
The above fix from ncac solved my problem, now all works again
Comment #9
arne_hortell commentedComment #10
pstewart commented@arne_hortell can you confirm which Drupal and Drush versions you were running when you experienced the Drush errors?
Comment #12
pstewart commentedI'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?
Comment #13
arne_hortell commented@pstewart Drupal 9.5.8 on Apache, drush 11.5.1
Comment #14
arne_hortell commented@pstewart patch 84.diff (your file) seems to work great
Comment #15
arne_hortell commentedFor 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...
Comment #16
pstewart commented@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
schemakey from the database settings insettings.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.
Comment #17
arne_hortell commentedIt 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
Comment #18
ncac commentedI can confirm that, on my side, every Drush command work with the issue fork patch. Thank you @pstewart
Comment #19
pstewart commentedOK, let's get this committed and tag a new release
Comment #21
pstewart commentedComment #22
solideogloria commentedPer #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.