Skip to content

Commit

Permalink
Allow Drupal install with contrib database drivers (#5070)
Browse files Browse the repository at this point in the history
* First

* Second
  • Loading branch information
mondrake committed Feb 12, 2022
1 parent 19b7521 commit d6b3061
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/Sql/SqlBase.php
Expand Up @@ -13,6 +13,26 @@
use Webmozart\PathUtil\Path;
use Consolidation\Config\Util\Interpolator;

/**
* The base implementation for Drush database connections.
*
* MySql, PostgreSql and SQLite implementations are provided by Drush.
* Contrib and custom database drivers can provide their own implementation by
* extending from this class, and naming the class like 'SqlMydriver'. Note that
* the camelcasing is required, as well as it is mandatory that the namespace of
* the extending class be 'Drush\Sql'. In order to avoid autoloadier
* collisions, it is recommended to place the class outside of the 'src'
* directory of the module providing the database driver, then adding a
* 'classmap' entry to the autoload class of the module's composer.json file.
*
* For example, supposing the SqlMydriver class is located in a 'drush'
* module subdirectory:
* @code
* "autoload": {
* "classmap": ["drush/SqlMydriver.php"]
* },
* @endcode
*/
abstract class SqlBase implements ConfigAwareInterface
{
use SqlTableSelectionTrait;
Expand Down Expand Up @@ -86,16 +106,16 @@ public static function create(array $options = []): ?SqlBase

if ($url = $options['db-url']) {
$url = is_array($url) ? $url[$database] : $url;
$db_spec = self::dbSpecFromDbUrl($url);
$db_spec = static::dbSpecFromDbUrl($url);
$db_spec['prefix'] = $options['db-prefix'];
return self::getInstance($db_spec, $options);
return static::getInstance($db_spec, $options);
} elseif (($databases = $options['databases']) && (array_key_exists($database, $databases)) && (array_key_exists($target, $databases[$database]))) {
// @todo 'databases' option is not declared anywhere?
$db_spec = $databases[$database][$target];
return self::getInstance($db_spec, $options);
return static::getInstance($db_spec, $options);
} elseif ($info = Database::getConnectionInfo($database)) {
$db_spec = $info[$target];
return self::getInstance($db_spec, $options);
return static::getInstance($db_spec, $options);
} else {
throw new \Exception(dt('Unable to load Drupal settings. Check your --root, --uri, etc.'));
}
Expand Down Expand Up @@ -587,7 +607,7 @@ public static function dbSpecFromDbUrl($db_url): array
];
$url = (object)array_map('urldecode', $url);
$db_spec = [
'driver' => $url->scheme == 'mysqli' ? 'mysql' : $url->scheme,
'driver' => $url->scheme,
'username' => $url->user,
'password' => $url->pass,
'host' => $url->host,
Expand Down

0 comments on commit d6b3061

Please sign in to comment.