6 use Drush\Preflight\PreflightArgs;
9 class SqlMysql extends SqlBase
12 public function command()
17 public function creds($hide_password = true)
19 $dbSpec = $this->getDbSpec();
21 // EMPTY password is not the same as NO password, and is valid.
23 #This file was written by Drush's Sqlmysql.php.
25 user="{$dbSpec['username']}"
26 password="{$dbSpec['password']}"
29 $file = drush_save_data_to_temp_file($contents);
30 $parameters['defaults-file'] = $file;
32 // User is required. Drupal calls it 'username'. MySQL calls it 'user'.
33 $parameters['user'] = $dbSpec['username'];
34 // EMPTY password is not the same as NO password, and is valid.
35 if (isset($dbSpec['password'])) {
36 $parameters['password'] = $dbSpec['password'];
40 // Some Drush commands (e.g. site-install) want to connect to the
41 // server, but not the database. Connect to the built-in database.
42 $parameters['database'] = empty($dbSpec['database']) ? 'information_schema' : $dbSpec['database'];
44 // Default to unix socket if configured.
45 if (!empty($dbSpec['unix_socket'])) {
46 $parameters['socket'] = $dbSpec['unix_socket'];
47 } // EMPTY host is not the same as NO host, and is valid (see unix_socket).
48 elseif (isset($dbSpec['host'])) {
49 $parameters['host'] = $dbSpec['host'];
52 if (!empty($dbSpec['port'])) {
53 $parameters['port'] = $dbSpec['port'];
56 if (!empty($dbSpec['pdo']['unix_socket'])) {
57 $parameters['socket'] = $dbSpec['pdo']['unix_socket'];
60 if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CA])) {
61 $parameters['ssl-ca'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CA];
64 if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CAPATH])) {
65 $parameters['ssl-capath'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CAPATH];
68 if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CERT])) {
69 $parameters['ssl-cert'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CERT];
72 if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CIPHER])) {
73 $parameters['ssl-cipher'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CIPHER];
76 if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_KEY])) {
77 $parameters['ssl-key'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_KEY];
80 return $this->paramsToOptions($parameters);
83 public function silent()
88 public function createdbSql($dbname, $quoted = false)
90 $dbSpec = $this->getDbSpec();
92 $dbname = '`' . $dbname . '`';
94 $sql[] = sprintf('DROP DATABASE IF EXISTS %s;', $dbname);
95 $sql[] = sprintf('CREATE DATABASE %s /*!40100 DEFAULT CHARACTER SET utf8 */;', $dbname);
96 $db_superuser = $this->getConfig()->get('sql.db-su');
97 if (isset($db_superuser)) {
98 // - For a localhost database, create a localhost user. This is important for security.
99 // localhost is special and only allows local Unix socket file connections.
100 // - If the database is on a remote server, create a wildcard user with %.
101 // We can't easily know what IP address or hostname would represent our server.
102 $domain = ($dbSpec['host'] == 'localhost') ? 'localhost' : '%';
103 $sql[] = sprintf('GRANT ALL PRIVILEGES ON %s.* TO \'%s\'@\'%s\'', $dbname, $dbSpec['username'], $domain);
104 $sql[] = sprintf("IDENTIFIED BY '%s';", $dbSpec['password']);
105 $sql[] = 'FLUSH PRIVILEGES;';
107 return implode(' ', $sql);
113 public function dbExists()
115 // Suppress output. We only care about return value.
116 return $this->alwaysQuery("SELECT 1;", null, drush_bit_bucket());
119 public function listTables()
121 $this->alwaysQuery('SHOW TABLES;');
122 return drush_shell_exec_output();
125 public function dumpCmd($table_selection)
127 $dbSpec = $this->getDbSpec();
129 $skip_tables = $table_selection['skip'];
130 $structure_tables = $table_selection['structure'];
131 $tables = $table_selection['tables'];
134 $skip_tables = array_merge($structure_tables, $skip_tables);
135 $data_only = $this->getOption('data-only');
136 // The ordered-dump option is only supported by MySQL for now.
137 $ordered_dump = $this->getOption('ordered-dump');
139 $exec = 'mysqldump ';
140 // mysqldump wants 'databasename' instead of 'database=databasename' for no good reason.
141 $only_db_name = str_replace('--database=', ' ', $this->creds());
142 $exec .= $only_db_name;
144 // We had --skip-add-locks here for a while to help people with insufficient permissions,
145 // but removed it because it slows down the import a lot. See http://drupal.org/node/1283978
146 $extra = ' --no-autocommit --single-transaction --opt -Q';
148 $extra .= ' --no-create-info';
151 $extra .= ' --skip-extended-insert --order-by-primary';
153 if ($option = $this->getOption('extra-dump', $this->queryExtra)) {
154 $extra .= " $option";
158 if (!empty($tables)) {
159 $exec .= ' ' . implode(' ', $tables);
161 // Append the ignore-table options.
162 foreach ($skip_tables as $table) {
163 $ignores[] = '--ignore-table=' . $dbSpec['database'] . '.' . $table;
166 $exec .= ' '. implode(' ', $ignores);
168 // Run mysqldump again and append output if we need some structure only tables.
169 if (!empty($structure_tables)) {
170 $exec .= " && mysqldump " . $only_db_name . " --no-data $extra " . implode(' ', $structure_tables);
174 return $parens ? "($exec)" : $exec;