diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index 791bf7a..c96afb3 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -1419,6 +1419,33 @@ public function quote($string, $parameter_type = \PDO::PARAM_STR) { } /** + * Checks if there the database is empty. + * + * @return bool + * Returns TRUE if there is an empty database for the connection. + */ + public function databaseEmpty() { + // Check for the presence of a database table provided in the hook_schema() + // implementation of a required core module. That should always be present + // if Drupal is already installed. + try { + return !$this->connection->schema()->tableExists('sessions'); + } + catch (\Exception $e) { + // If we still have an exception at this point, we need to be careful + // since we should not redirect if the exception represents an error on an + // already-installed site (for example, if the database server went down). + // However a database error with code 1049 means "missing database", and + // it should be safe to redirect in that case. + return ($e instanceof \PDOException || $e instanceof DatabaseException) && $e->getCode() == 1049; + } + + // Default to FALSE to minimize the chance of overwriting an installed + // database. + return FALSE; + } + + /** * Prevents the database connection from being serialized. */ public function __sleep() { diff --git a/core/lib/Drupal/Core/Installer/CheckInstalledTrait.php b/core/lib/Drupal/Core/Installer/CheckInstalledTrait.php index d00d6d2..f5fa91d 100644 --- a/core/lib/Drupal/Core/Installer/CheckInstalledTrait.php +++ b/core/lib/Drupal/Core/Installer/CheckInstalledTrait.php @@ -28,13 +28,11 @@ * Drupal is not installed yet, or FALSE otherwise. */ protected function shouldRedirectToInstaller(\Exception $exception, Connection $connection = NULL) { - $exception_to_check = $exception; - // To avoid unnecessary queries, only act if the exception is one that is // expected to occur when Drupal has not yet been installed. This includes // NotFoundHttpException because an uninstalled site won't have route // information available yet and therefore can return 404 errors. - if (!($exception_to_check instanceof \PDOException || $exception_to_check instanceof DatabaseException || $exception_to_check instanceof NotFoundHttpException)) { + if (!($exception instanceof \PDOException || $exception instanceof DatabaseException || $exception instanceof NotFoundHttpException)) { return FALSE; } @@ -54,27 +52,10 @@ protected function shouldRedirectToInstaller(\Exception $exception, Connection $ return TRUE; } - // Check for the presence of a database table provided in the hook_schema() - // implementation of a required core module. That should always be present - // if Drupal is already installed. - if (isset($connection)) { - try { - return !$connection->schema()->tableExists('sessions'); - } - catch (\Exception $e) { - // If the query failed, use the exception it failed with for the rest - // of the checks in this function; it will be more reliable since - // unlike the original exception, we know exactly where it came from. - $exception_to_check = $e; - } + // Redirect if the database is empty. + if ($connection) { + return $connection->databaseEmpty(); } - - // If we still have an exception at this point, we need to be careful since - // we should not redirect if the exception represents an error on an - // already-installed site (for example, if the database server went down). - // However a database error with code 1049 means "missing database", and it - // should be safe to redirect in that case. - return ($exception_to_check instanceof \PDOException || $exception_to_check instanceof DatabaseException) && $exception_to_check->getCode() == 1049; } }