6 use Drush\Preflight\PreflightArgs;
9 class SqlMysql extends SqlBase
12 public $queryExtra = '-A';
14 public function command()
19 public function creds($hide_password = true)
21 $dbSpec = $this->getDbSpec();
23 // EMPTY password is not the same as NO password, and is valid.
25 #This file was written by Drush's Sqlmysql.php.
27 user="{$dbSpec['username']}"
28 password="{$dbSpec['password']}"
31 $file = drush_save_data_to_temp_file($contents);
32 $parameters['defaults-file'] = $file;
34 // User is required. Drupal calls it 'username'. MySQL calls it 'user'.
35 $parameters['user'] = $dbSpec['username'];
36 // EMPTY password is not the same as NO password, and is valid.
37 if (isset($dbSpec['password'])) {
38 $parameters['password'] = $dbSpec['password'];
42 // Some Drush commands (e.g. site-install) want to connect to the
43 // server, but not the database. Connect to the built-in database.
44 $parameters['database'] = empty($dbSpec['database']) ? 'information_schema' : $dbSpec['database'];
46 // Default to unix socket if configured.
47 if (!empty($dbSpec['unix_socket'])) {
48 $parameters['socket'] = $dbSpec['unix_socket'];
49 } // EMPTY host is not the same as NO host, and is valid (see unix_socket).
50 elseif (isset($dbSpec['host'])) {
51 $parameters['host'] = $dbSpec['host'];
54 if (!empty($dbSpec['port'])) {
55 $parameters['port'] = $dbSpec['port'];
58 if (!empty($dbSpec['pdo']['unix_socket'])) {
59 $parameters['socket'] = $dbSpec['pdo']['unix_socket'];
62 if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CA])) {
63 $parameters['ssl-ca'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CA];
66 if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CAPATH])) {
67 $parameters['ssl-capath'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CAPATH];
70 if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CERT])) {
71 $parameters['ssl-cert'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CERT];
74 if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CIPHER])) {
75 $parameters['ssl-cipher'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CIPHER];
78 if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_KEY])) {
79 $parameters['ssl-key'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_KEY];
82 return $this->paramsToOptions($parameters);
85 public function silent()
90 public function createdbSql($dbname, $quoted = false)
92 $dbSpec = $this->getDbSpec();
94 $dbname = '`' . $dbname . '`';
96 $sql[] = sprintf('DROP DATABASE IF EXISTS %s;', $dbname);
97 $sql[] = sprintf('CREATE DATABASE %s /*!40100 DEFAULT CHARACTER SET utf8 */;', $dbname);
98 $db_superuser = $this->getConfig()->get('sql.db-su');
99 if (isset($db_superuser)) {
100 // - For a localhost database, create a localhost user. This is important for security.
101 // localhost is special and only allows local Unix socket file connections.
102 // - If the database is on a remote server, create a wildcard user with %.
103 // We can't easily know what IP address or hostname would represent our server.
104 $domain = ($dbSpec['host'] == 'localhost') ? 'localhost' : '%';
105 $sql[] = sprintf('GRANT ALL PRIVILEGES ON %s.* TO \'%s\'@\'%s\'', $dbname, $dbSpec['username'], $domain);
106 $sql[] = sprintf("IDENTIFIED BY '%s';", $dbSpec['password']);
107 $sql[] = 'FLUSH PRIVILEGES;';
109 return implode(' ', $sql);
115 public function dbExists()
117 // Suppress output. We only care about return value.
118 return $this->alwaysQuery("SELECT 1;", null, drush_bit_bucket());
121 public function listTables()
123 $this->alwaysQuery('SHOW TABLES;');
124 return drush_shell_exec_output();
127 public function dumpCmd($table_selection)
129 $dbSpec = $this->getDbSpec();
131 $skip_tables = $table_selection['skip'];
132 $structure_tables = $table_selection['structure'];
133 $tables = $table_selection['tables'];
136 $skip_tables = array_merge($structure_tables, $skip_tables);
137 $data_only = $this->getOption('data-only');
138 // The ordered-dump option is only supported by MySQL for now.
139 $ordered_dump = $this->getOption('ordered-dump');
141 $exec = 'mysqldump ';
142 // mysqldump wants 'databasename' instead of 'database=databasename' for no good reason.
143 $only_db_name = str_replace('--database=', ' ', $this->creds());
144 $exec .= $only_db_name;
146 // We had --skip-add-locks here for a while to help people with insufficient permissions,
147 // but removed it because it slows down the import a lot. See http://drupal.org/node/1283978
148 $extra = ' --no-autocommit --single-transaction --opt -Q';
150 $extra .= ' --no-create-info';
153 $extra .= ' --skip-extended-insert --order-by-primary';
155 if ($option = $this->getOption('extra-dump')) {
156 $extra .= " $option";
160 if (!empty($tables)) {
161 $exec .= ' ' . implode(' ', $tables);
163 // Append the ignore-table options.
164 foreach ($skip_tables as $table) {
165 $ignores[] = '--ignore-table=' . $dbSpec['database'] . '.' . $table;
168 $exec .= ' '. implode(' ', $ignores);
170 // Run mysqldump again and append output if we need some structure only tables.
171 if (!empty($structure_tables)) {
172 $exec .= " && mysqldump " . $only_db_name . " --no-data $extra " . implode(' ', $structure_tables);
176 return $parens ? "($exec)" : $exec;