Skip to content

Commit 9f6b80d

Browse files
authored
Use Drupal DB during bootstrap instead of CLI (#3800)
1 parent d42c974 commit 9f6b80d

File tree

2 files changed

+21
-72
lines changed

2 files changed

+21
-72
lines changed

src/Boot/DrupalBoot.php

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -169,77 +169,6 @@ public function bootstrapDrupalConfiguration()
169169
*/
170170
public function bootstrapDrupalDatabaseValidate()
171171
{
172-
// Drupal requires PDO, and Drush requires php 5.6+ which ships with PDO
173-
// but PHP may be compiled with --disable-pdo.
174-
if (!class_exists('\PDO')) {
175-
$this->logger->log(LogLevel::BOOTSTRAP, dt('PDO support is required.'));
176-
return false;
177-
}
178-
try {
179-
$sql = SqlBase::create();
180-
// Drush requires a database client program during its Drupal bootstrap.
181-
$command = $sql->command();
182-
if (drush_program_exists($command) === false) {
183-
$this->logger->warning(dt('The command \'!command\' is required for preflight but cannot be found. Please install it and retry.', ['!command' => $command]));
184-
return false;
185-
}
186-
if (!$sql->query('SELECT 1;', null, drush_bit_bucket())) {
187-
$message = dt("Drush was not able to start (bootstrap) the Drupal database.\n");
188-
$message .= dt("Hint: This may occur when Drush is trying to:\n");
189-
$message .= dt(" * bootstrap a site that has not been installed or does not have a configured database. In this case you can select another site with a working database setup by specifying the URI to use with the --uri parameter on the command line. See `drush topic docs-aliases` for details.\n");
190-
$message .= dt(" * connect the database through a socket. The socket file may be wrong or the php-cli may have no access to it in a jailed shell. See http://drupal.org/node/1428638 for details.\n");
191-
$message .= dt('More information may be available by running `drush status`');
192-
$this->logger->log(LogLevel::BOOTSTRAP, $message);
193-
return false;
194-
}
195-
} catch (\Exception $e) {
196-
$this->logger->log(LogLevel::DEBUG, dt('Unable to validate DB: @e', ['@e' => $e->getMessage()]));
197-
return false;
198-
}
199-
return true;
200-
}
201-
202-
/**
203-
* Test to see if the Drupal database has a specified
204-
* table or tables.
205-
*
206-
* This is a bootstrap helper function designed to be called
207-
* from the bootstrapDrupalDatabaseValidate() methods of
208-
* derived DrupalBoot classes. If a database exists, but is
209-
* empty, then the Drupal database bootstrap will fail. To
210-
* prevent this situation, we test for some table that is needed
211-
* in an ordinary bootstrap, and return FALSE from the validate
212-
* function if it does not exist, so that we do not attempt to
213-
* start the database bootstrap.
214-
*
215-
* Note that we must manually do our own prefix testing here,
216-
* because the existing wrappers we have for handling prefixes
217-
* depend on bootstrapping to the "database" phase, and therefore
218-
* are not available to validate this same phase.
219-
*
220-
* @param $required_tables
221-
* Array of table names, or string with one table name
222-
*
223-
* @return TRUE if all required tables exist in the database.
224-
*/
225-
public function bootstrapDrupalDatabaseHasTable($required_tables)
226-
{
227-
228-
$sql = SqlBase::create();
229-
$spec = $sql->getDbSpec();
230-
$prefix = isset($spec['prefix']) ? $spec['prefix'] : null;
231-
if (!is_array($prefix)) {
232-
$prefix = ['default' => $prefix];
233-
}
234-
foreach ((array)$required_tables as $required_table) {
235-
$prefix_key = array_key_exists($required_table, $prefix) ? $required_table : 'default';
236-
$table_name = $prefix[$prefix_key] . $required_table;
237-
if (!$sql->alwaysQuery("SELECT 1 FROM $table_name LIMIT 1;", null, drush_bit_bucket())) {
238-
$this->logger->notice('Missing database table: '. $table_name);
239-
return false;
240-
}
241-
}
242-
return true;
243172
}
244173

245174
/**

src/Boot/DrupalBoot8.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Drush\Boot;
44

55
use Consolidation\AnnotatedCommand\AnnotationData;
6+
use Drupal\Core\Database\Database;
67
use Drush\Log\DrushLog;
78
use Symfony\Component\HttpFoundation\Request;
89
use Symfony\Component\HttpFoundation\Response;
@@ -154,7 +155,26 @@ public function bootstrapDrupalConfigurationValidate()
154155

155156
public function bootstrapDrupalDatabaseValidate()
156157
{
157-
return parent::bootstrapDrupalDatabaseValidate() && $this->bootstrapDrupalDatabaseHasTable('key_value');
158+
// Drupal requires PDO, and Drush requires php 5.6+ which ships with PDO
159+
// but PHP may be compiled with --disable-pdo.
160+
if (!class_exists('\PDO')) {
161+
$this->logger->log(LogLevel::BOOTSTRAP, dt('PDO support is required.'));
162+
return false;
163+
}
164+
165+
try {
166+
// @todo Log queries in addition to logging failure messages?
167+
$connection = Database::getConnection();
168+
$connection->query('SELECT 1;');
169+
} catch (\Exception $e) {
170+
$this->logger->log(LogLevel::BOOTSTRAP, 'Unable to connect to database. More information may be available by running `drush status`. This may occur when Drush is trying to bootstrap a site that has not been installed or does not have a configured database. In this case you can select another site with a working database setup by specifying the URI to use with the --uri parameter on the command line. See `drush topic docs-aliases` for details.');
171+
return false;
172+
}
173+
if (!$connection->schema()->tableExists('key_value')) {
174+
$this->logger->log(LogLevel::BOOTSTRAP, 'key_value table not found. Database may be empty.');
175+
return false;
176+
}
177+
return true;
158178
}
159179

160180
public function bootstrapDrupalDatabase()

0 commit comments

Comments
 (0)