diff -u b/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php --- b/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -562,32 +562,8 @@ } } catch (\PDOException $e) { - if ($this->isTableMissingException($e) && !empty($options['create_missing_table'])) { - if ($options['return'] == Database::RETURN_STATEMENT) { - // This is a SELECT on a missing table. Creating the table and - // re-runing the query will result in an empty result so return that - // early instead. - return new StatementEmpty(); - } - if (is_string($options['create_missing_table'])) { - $table = $options['create_missing_table']; - foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT) as $bt) { - if (isset($bt['object']) && $bt['object'] instanceof SchemaProviderInterface && ($schema = $bt['object']->getSchema($table))) { - if ($this->schema()->ensureTableExists($table, $schema)) { - // Theoretically the table must exist at this point so this - // should not matter but still, databases. This makes sure - // no infinite recursion happens. - $options['create_missing_table'] = FALSE; - return $this->query($query, $args, $options); - } - } - } - } - elseif ($options['create_missing_table'] === TRUE) { - return NULL; - } - } - if ($options['throw_exception']) { + $handled = $this->handleMissingTable($e, $query, $args, $options); + if (!isset($handled) && $options['throw_exception']) { // Wrap the exception in another exception, because PHP does not allow // overriding Exception::getMessage(). Its message is the extra database // debug information. @@ -608,6 +584,43 @@ } /** + * Handle a missing table exception. + * + * @param $e + * @param $query + * @param $args + * @param $options + * @return bool|StatementInterface + */ + protected function handleMissingTable($e, $query, $args, $options) { + if ($this->isTableMissingException($e) && !empty($options['create_missing_table'])) { + if ($options['return'] == Database::RETURN_STATEMENT) { + // This is a SELECT on a missing table. Creating the table and + // re-runing the query will result in an empty result so return that + // early instead. + return new StatementEmpty(); + } + if (is_string($options['create_missing_table'])) { + $table = $options['create_missing_table']; + foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT) as $bt) { + if (isset($bt['object']) && $bt['object'] instanceof SchemaProviderInterface && ($schema = $bt['object']->getSchema($table))) { + if ($this->schema()->ensureTableExists($table, $schema)) { + // Theoretically the table must exist at this point so this + // should not matter but still, databases. This makes sure + // no infinite recursion happens. + $options['create_missing_table'] = FALSE; + return $this->query($query, $args, $options); + } + } + } + } + elseif ($options['create_missing_table'] === TRUE) { + return FALSE; + } + } + } + + /** * Expands out shorthand placeholders. * * Drupal supports an alternate syntax for doing arrays of values. We diff -u b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php --- b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php @@ -143,7 +143,8 @@ } } catch (\PDOException $e) { - if ($options['throw_exception']) { + $handled = $this->handleMissingTable($e, $query, $args, $options); + if (!isset($handled) && $options['throw_exception']) { // Match all SQLSTATE 23xxx errors. if (substr($e->getCode(), -6, -3) == '23') { $e = new IntegrityConstraintViolationException($e->getMessage(), $e->getCode(), $e);