Sqlmysql.php

  1. 8.0.x lib/Drush/Sql/Sqlmysql.php
  2. 7.x lib/Drush/Sql/Sqlmysql.php
  3. master lib/Drush/Sql/Sqlmysql.php

Namespace

Drush\Sql

Classes

Namesort descending Description
Sqlmysql

File

lib/Drush/Sql/Sqlmysql.php
View source
  1. <?php
  2. namespace Drush\Sql;
  3. class Sqlmysql extends SqlBase {
  4. public function command() {
  5. return 'mysql';
  6. }
  7. public function creds($hide_password = TRUE) {
  8. if ($hide_password) {
  9. // EMPTY password is not the same as NO password, and is valid.
  10. $contents = <<<EOT
  11. #This file was written by Drush's Sqlmysql.inc.
  12. [client]
  13. user="{$this->db_spec['username']}"
  14. password="{$this->db_spec['password']}"
  15. EOT;
  16. $file = drush_save_data_to_temp_file($contents);
  17. $parameters['defaults-extra-file'] = $file;
  18. }
  19. else {
  20. // User is required. Drupal calls it 'username'. MySQL calls it 'user'.
  21. $parameters['user'] = $this->db_spec['username'];
  22. // EMPTY password is not the same as NO password, and is valid.
  23. if (isset($this->db_spec['password'])) {
  24. $parameters['password'] = $this->db_spec['password'];
  25. }
  26. }
  27. // Some drush commands (e.g. site-install) want to connect to the
  28. // server, but not the database. Connect to the built-in database.
  29. $parameters['database'] = empty($this->db_spec['database']) ? 'information_schema' : $this->db_spec['database'];
  30. // Default to unix socket if configured.
  31. if (!empty($this->db_spec['unix_socket'])) {
  32. $parameters['socket'] = $this->db_spec['unix_socket'];
  33. }
  34. // EMPTY host is not the same as NO host, and is valid (see unix_socket).
  35. elseif (isset($this->db_spec['host'])) {
  36. $parameters['host'] = $this->db_spec['host'];
  37. }
  38. if (!empty($this->db_spec['port'])) {
  39. $parameters['port'] = $this->db_spec['port'];
  40. }
  41. return $this->params_to_options($parameters);
  42. }
  43. public function silent() {
  44. return '--silent';
  45. }
  46. public function createdb_sql($dbname, $quoted = FALSE) {
  47. if ($quoted) {
  48. $dbname = '`' . $dbname . '`';
  49. }
  50. $sql[] = sprintf('DROP DATABASE IF EXISTS %s;', $dbname);
  51. $sql[] = sprintf('CREATE DATABASE %s /*!40100 DEFAULT CHARACTER SET utf8 */;', $dbname);
  52. $db_superuser = drush_get_option('db-su');
  53. if (isset($db_superuser)) {
  54. $sql[] = sprintf('GRANT ALL PRIVILEGES ON %s.* TO \'%s\'@\'%s\'', $dbname, $this->db_spec['username'], $this->db_spec['host']);
  55. $sql[] = sprintf("IDENTIFIED BY '%s';", $this->db_spec['password']);
  56. $sql[] = 'FLUSH PRIVILEGES;';
  57. }
  58. return implode(' ', $sql);
  59. }
  60. public function db_exists() {
  61. $current = drush_get_context('DRUSH_SIMULATE');
  62. drush_set_context('DRUSH_SIMULATE', FALSE);
  63. // Suppress output. We only care about return value.
  64. $return = $this->query("SELECT 1;", NULL, drush_bit_bucket());
  65. drush_set_context('DRUSH_SIMULATE', $current);
  66. return $return;
  67. }
  68. public function listTables() {
  69. $current = drush_get_context('DRUSH_SIMULATE');
  70. drush_set_context('DRUSH_SIMULATE', FALSE);
  71. $return = $this->query('SHOW TABLES;');
  72. $tables = drush_shell_exec_output();
  73. drush_set_context('DRUSH_SIMULATE', $current);
  74. return $tables;
  75. }
  76. public function dumpCmd($table_selection) {
  77. $parens = FALSE;
  78. $skip_tables = $table_selection['skip'];
  79. $structure_tables = $table_selection['structure'];
  80. $tables = $table_selection['tables'];
  81. $ignores = array();
  82. $skip_tables = array_merge($structure_tables, $skip_tables);
  83. $data_only = drush_get_option('data-only');
  84. // The ordered-dump option is only supported by MySQL for now.
  85. // @todo add documention once a hook for drush_get_option_help() is available.
  86. // @see drush_get_option_help() in drush.inc
  87. $ordered_dump = drush_get_option('ordered-dump');
  88. $exec = 'mysqldump ';
  89. // mysqldump wants 'databasename' instead of 'database=databasename' for no good reason.
  90. $only_db_name = str_replace('--database=', ' ', $this->creds());
  91. $exec .= $only_db_name;
  92. // We had --skip-add-locks here for a while to help people with insufficient permissions,
  93. // but removed it because it slows down the import a lot. See http://drupal.org/node/1283978
  94. $extra = ' --no-autocommit --single-transaction --opt -Q';
  95. if (isset($data_only)) {
  96. $extra .= ' --no-create-info';
  97. }
  98. if (isset($ordered_dump)) {
  99. $extra .= ' --skip-extended-insert --order-by-primary';
  100. }
  101. $exec .= $extra;
  102. if (!empty($tables)) {
  103. $exec .= ' ' . implode(' ', $tables);
  104. }
  105. else {
  106. // Append the ignore-table options.
  107. foreach ($skip_tables as $table) {
  108. $ignores[] = '--ignore-table=' . $this->db_spec['database'] . '.' . $table;
  109. $parens = TRUE;
  110. }
  111. $exec .= ' '. implode(' ', $ignores);
  112. // Run mysqldump again and append output if we need some structure only tables.
  113. if (!empty($structure_tables)) {
  114. $exec .= " && mysqldump " . $only_db_name . " --no-data $extra " . implode(' ', $structure_tables);
  115. $parens = TRUE;
  116. }
  117. }
  118. return $parens ? "($exec)" : $exec;
  119. }
  120. }