Skip to content

Commit

Permalink
Remove sqlVersion classes in favor of factory method in SqlBase (#2598)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman committed Feb 8, 2017
1 parent 9bbddb2 commit b23f454
Show file tree
Hide file tree
Showing 19 changed files with 266 additions and 295 deletions.
59 changes: 0 additions & 59 deletions commands/sql/sql.drush.inc

This file was deleted.

2 changes: 1 addition & 1 deletion includes/drush.inc
Expand Up @@ -1413,7 +1413,7 @@ function drush_print_timers() {
}

/**
* Turn drupal_set_message errors into drush_log errors
* Turn drupal_set_message() errors into drush_log errors
*/
function _drush_log_drupal_messages() {
if (function_exists('drupal_get_messages')) {
Expand Down
5 changes: 3 additions & 2 deletions includes/filesystem.inc
Expand Up @@ -4,6 +4,7 @@
* @file
* Filesystem utilities.
*/
use Drush\Sql\SqlBase;
use Webmozart\PathUtil\Path;

/**
Expand Down Expand Up @@ -576,8 +577,8 @@ function drush_preflight_backup_dir($subdir = NULL) {
// Try to use db name as subdir if none was provided.
if (empty($subdir)) {
$subdir = 'unknown';
if ($sql = drush_sql_get_class()) {
$db_spec = $sql->db_spec();
if ($sql = SqlBase::create()) {
$db_spec = $sql->getDbSpec();
$subdir = $db_spec['database'];
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/Drush.php
Expand Up @@ -6,6 +6,7 @@
*/

use League\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use SebastianBergmann\Version;

/**
Expand Down
11 changes: 6 additions & 5 deletions lib/Drush/Boot/DrupalBoot.php
Expand Up @@ -3,6 +3,7 @@
namespace Drush\Boot;

use Drush\Log\LogLevel;
use Drush\Sql\SqlBase;
use Psr\Log\LoggerInterface;
use Drupal\user\Entity\User;

Expand Down Expand Up @@ -476,17 +477,17 @@ function bootstrap_drupal_database_validate() {
// Drupal requires PDO, and Drush requires php 5.6+ which ships with PDO
// but PHP may be compiled with --disable-pdo.
if (!class_exists('\PDO')) {
drush_log(dt('PDO support is required.'), LogLevel::BOOTSTRAP);
$this->logger->log(LogLevel::BOOTSTRAP, dt('PDO support is required.'));
return FALSE;
}
try {
$sql = drush_sql_get_class();
$sql = SqlBase::create();
if (!$sql->query('SELECT 1;')) {
return drush_bootstrap_error('DRUSH_DRUPAL_DB_ERROR');
}
}
catch (\Exception $e) {
drush_log(dt('Unable to validate DB c: @e', array('@e' => $e->getMessage())), 'debug');
$this->logger->log(LogLevel::DEBUG, dt('Unable to validate DB: @e', array('@e' => $e->getMessage())));
return FALSE;
}
return TRUE;
Expand Down Expand Up @@ -518,8 +519,8 @@ function bootstrap_drupal_database_validate() {
*/
function bootstrap_drupal_database_has_table($required_tables) {
try {
$sql = drush_sql_get_class();
$spec = $sql->db_spec();
$sql = SqlBase::create();
$spec = $sql->getDbSpec();
$prefix = isset($spec['prefix']) ? $spec['prefix'] : NULL;
if (!is_array($prefix)) {
$prefix = array('default' => $prefix);
Expand Down
2 changes: 1 addition & 1 deletion lib/Drush/Commands/OptionsCommands.php
Expand Up @@ -33,7 +33,7 @@ public function optionsetSsh() {}
* @option db-url A Drupal 6 style database URL.
* @option target The name of a target within the specified database connection. Defaults to default
*/
public function optionsetSql() {}
public function optionsetSql($options = ['database' => 'default', 'target' => 'default']) {}

/**
* @hook option @optionset_table_selection
Expand Down
17 changes: 9 additions & 8 deletions lib/Drush/Commands/core/SiteInstallCommands.php
Expand Up @@ -5,6 +5,7 @@
use Drush\Commands\DrushCommands;
use Drush\Log\LogLevel;
use Drupal\Core\Config\FileStorage;
use Drush\Sql\SqlBase;

class SiteInstallCommands extends DrushCommands {

Expand Down Expand Up @@ -62,8 +63,8 @@ public function install($profile, $additional = NULL, $options = ['db-url' => NU
$class_loader = drush_drupal_load_autoloader(DRUPAL_ROOT);
$profile = $this->determineProfile($profile, $options, $class_loader);

$sql = drush_sql_get_class();
$db_spec = $sql->db_spec();
$sql = SqlBase::create($options);
$db_spec = $sql->getDbSpec();

$account_pass = $options['account-pass'] ?: drush_generate_password();
$settings = array(
Expand Down Expand Up @@ -190,8 +191,8 @@ public function validate(CommandData $commandData) {
}
}

$sql = drush_sql_get_class();
if (!$sql->db_spec()) {
$sql = SqlBase::create($commandData->input()->getOptions());
if (!$sql->getDbSpec()) {
throw new \Exception(dt('Could not determine database connection parameters. Pass --db-url option.'));
}
}
Expand All @@ -203,8 +204,8 @@ public function validate(CommandData $commandData) {
*
*/
public function pre(CommandData $commandData) {
$sql = drush_sql_get_class();
$db_spec = $sql->db_spec();
$sql = SqlBase::create($commandData->input()->getOptions());
$db_spec = $sql->getDbSpec();

// Make sure URI is set so we get back a proper $alias_record. Needed for quick-drupal.
_drush_bootstrap_selected_uri();
Expand All @@ -227,7 +228,7 @@ public function pre(CommandData $commandData) {
if ($sitesfile_write) {
$msg[] = dt('create a @sitesfile file', array('@sitesfile' => $sitesfile));
}
if ($sql->db_exists()) {
if ($sql->dbExists()) {
$msg[] = dt("DROP all tables in your '@db' database.", array('@db' => $db_spec['database']));
}
else {
Expand Down Expand Up @@ -266,7 +267,7 @@ public function pre(CommandData $commandData) {
define('MAINTENANCE_MODE', 'install');
drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);

if (!$sql->drop_or_create()) {
if (!$sql->dropOrCreate()) {
throw new \Exception(dt('Failed to create database: @error', array('@error' => implode(drush_shell_exec_output()))));
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/Drush/Commands/core/StatusCommands.php
Expand Up @@ -5,6 +5,7 @@
use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Drush\Commands\DrushCommands;
use Drush\Sql\SqlBase;
use Symfony\Component\Console\Input\InputInterface;

use Consolidation\OutputFormatters\StructuredData\AssociativeList;
Expand Down Expand Up @@ -83,8 +84,8 @@ public static function getPropertyList($options) {
if ($site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT')) {
$status_table['uri'] = drush_get_context('DRUSH_URI');
try {
$sql = drush_sql_get_class();
$db_spec = $sql->db_spec();
$sql = SqlBase::create($options);
$db_spec = $sql->getDbSpec();
$status_table['db-driver'] = $db_spec['driver'];
if (!empty($db_spec['unix_socket'])) {
$status_table['db-socket'] = $db_spec['unix_socket'];
Expand Down
3 changes: 2 additions & 1 deletion lib/Drush/Commands/sql/SanitizeUserTableCommands.php
Expand Up @@ -4,6 +4,7 @@
use Consolidation\AnnotatedCommand\CommandData;
use Drupal\Core\Database\Database;
use Drush\Commands\DrushCommands;
use Drush\Sql\SqlBase;
use Symfony\Component\Console\Input\InputInterface;

/**
Expand Down Expand Up @@ -39,7 +40,7 @@ public function sanitize($result, CommandData $commandData) {
if ($this->isEnabled($options['sanitize-email'])) {
if (strpos($options['sanitize-email'], '%') !== FALSE) {
// We need a different sanitization query for MSSQL, Postgres and Mysql.
$sql = drush_sql_get_class();
$sql = SqlBase::create($commandData->input()->getOptions());
$db_driver = $sql->scheme();
if ($db_driver == 'pgsql') {
$email_map = array('%uid' => "' || uid || '", '%mail' => "' || replace(mail, '@', '_') || '", '%name' => "' || replace(name, ' ', '_') || '");
Expand Down
33 changes: 16 additions & 17 deletions lib/Drush/Commands/sql/SqlCommands.php
Expand Up @@ -3,6 +3,7 @@

use Drupal\Core\Database\Database;
use Drush\Commands\DrushCommands;
use Drush\Sql\SqlBase;

class SqlCommands extends DrushCommands {

Expand All @@ -16,7 +17,7 @@ class SqlCommands extends DrushCommands {
* @hidden
*/
public function conf($options = ['format' => 'yaml', 'all' => FALSE, 'show-passwords' => FALSE]) {
drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
$this->further($options);
if ($options['all']) {
$return = Database::getAllConnectionInfo();
foreach ($return as $key1 => $value) {
Expand All @@ -28,8 +29,8 @@ public function conf($options = ['format' => 'yaml', 'all' => FALSE, 'show-passw
}
}
else {
$sql = drush_sql_get_class();
$return = $sql->db_spec();
$sql = SqlBase::create($options);
$return = $sql->getDbSpec();
if (!$options['show-passwords']) {
unset($return['password']);
}
Expand All @@ -50,7 +51,7 @@ public function conf($options = ['format' => 'yaml', 'all' => FALSE, 'show-passw
*/
public function connect($options = ['extra' => '']) {
$this->further($options);
$sql = drush_sql_get_class();
$sql = SqlBase::create($options);
return $sql->connect(FALSE);
}

Expand All @@ -68,10 +69,10 @@ public function connect($options = ['extra' => '']) {
* @usage drush sql-create --db-su=root --db-su-pw=rootpassword --db-url="mysql://drupal_db_user:drupal_db_password@127.0.0.1/drupal_db"
* Create the database as specified in the db-url option.
*/
public function create() {
public function create($options = []) {
$this->further($options);
$sql = drush_sql_get_class();
$db_spec = $sql->db_spec();
$sql = SqlBase::create($options);
$db_spec = $sql->getDbSpec();
// Prompt for confirmation.
if (!drush_get_context('DRUSH_SIMULATE')) {
// @todo odd - maybe for sql-sync.
Expand All @@ -94,10 +95,10 @@ public function create() {
* @optionset_sql
* @topics docs-policy
*/
public function drop() {
public function drop($options = []) {
$this->further($options);
$sql = drush_sql_get_class();
$db_spec = $sql->db_spec();
$sql = SqlBase::create($options);
$db_spec = $sql->getDbSpec();
if (!drush_confirm(dt('Do you really want to drop all tables in the database !db?', array('!db' => $db_spec['database'])))) {
return drush_user_abort();
}
Expand All @@ -118,9 +119,9 @@ public function drop() {
* Open a SQL CLI and skip reading table information.
* @remote-tty
*/
public function cli() {
public function cli($options = []) {
$this->further($options);
$sql = drush_sql_get_class();
$sql = SqlBase::create($options);
drush_shell_proc_open($sql->connect());
}

Expand All @@ -134,8 +135,6 @@ public function cli() {
* @option file Path to a file containing the SQL to be run. Gzip files are accepted.
* @option extra Add custom options to the connect string (e.g. --extra=--skip-column-names)
* @option db-prefix Enable replacement of braces in your query.
* @option db-spec A database specification. Only used with --backend calls.
* @hidden-option db-spec
* @validate-file-exists file
* @aliases sqlq
* @usage drush sql-query "SELECT * FROM users WHERE uid=1"
Expand All @@ -148,7 +147,7 @@ public function cli() {
* Alternate way to import sql statements from a file.
*
*/
public function query($query = '', $options = ['result-file' => NULL, 'file' => NULL, 'extra' => NULL, 'db-prefix' => NULL, 'db-spec' => NULL]) {
public function query($query = '', $options = ['result-file' => NULL, 'file' => NULL, 'extra' => NULL, 'db-prefix' => NULL]) {
$this->further($options);
$filename = $options['file'];
// Enable prefix processing when db-prefix option is used.
Expand All @@ -164,7 +163,7 @@ public function query($query = '', $options = ['result-file' => NULL, 'file' =>
}
}
else {
$sql = drush_sql_get_class($options['db-spec']);
$sql = SqlBase::create($options);
$result = $sql->query($query, $filename, $options['result-file']);
if (!$result) {
throw new \Exception(dt('Query failed.'));
Expand Down Expand Up @@ -197,7 +196,7 @@ public function query($query = '', $options = ['result-file' => NULL, 'file' =>
*/
public function dump($options = ['result-file' => NULL, 'create-db' => NULL, 'data-only' => NULL, 'ordered-dump' => NULL, 'gzip' => NULL, 'extra' => NULL, 'extra-dump' => NULL]) {
$this->further($options);
$sql = drush_sql_get_class();
$sql = SqlBase::create($options);
return $sql->dump($options);
}

Expand Down
20 changes: 0 additions & 20 deletions lib/Drush/Sql/Sql7.php

This file was deleted.

18 changes: 0 additions & 18 deletions lib/Drush/Sql/Sql8.php

This file was deleted.

0 comments on commit b23f454

Please sign in to comment.