Sqlpgsql.php

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

Namespace

Drush\Sql

Constants

Namesort descending Description
PSQL_SHOW_TABLES

Classes

Namesort descending Description
Sqlpgsql

File

lib/Drush/Sql/Sqlpgsql.php
View source
  1. <?php
  2. namespace Drush\Sql;
  3. define('PSQL_SHOW_TABLES', "SELECT tablename FROM pg_tables WHERE schemaname='public';");
  4. class Sqlpgsql extends SqlBase {
  5. public $query_extra = "--no-align --field-separator='\t' --pset tuples_only=on";
  6. public $query_file = "--file";
  7. private $password_file = NULL;
  8. private function password_file() {
  9. if (!isset($password_file) && isset($this->db_spec['password'])) {
  10. $pgpass_parts = array(
  11. empty($this->db_spec['host']) ? 'localhost' : $this->db_spec['host'],
  12. empty($this->db_spec['port']) ? '5432' : $this->db_spec['port'],
  13. // Database
  14. '*',
  15. $this->db_spec['username'],
  16. $this->db_spec['password']
  17. );
  18. // Escape colon and backslash characters in entries.
  19. // @see http://www.postgresql.org/docs/9.1/static/libpq-pgpass.html
  20. array_walk($pgpass_parts, function (&$part) {
  21. // The order of the replacements is important so that backslashes are
  22. // not replaced twice.
  23. $part = str_replace(array('\\', ':'), array('\\\\', '\:'), $part);
  24. });
  25. $pgpass_contents = implode(':', $pgpass_parts);
  26. $password_file = drush_save_data_to_temp_file($pgpass_contents);
  27. chmod($password_file, 0600);
  28. }
  29. return $password_file;
  30. }
  31. public function command() {
  32. $environment = "";
  33. $pw_file = $this->password_file();
  34. if (isset($pw_file)) {
  35. $environment = "PGPASSFILE={$pw_file} ";
  36. }
  37. return "{$environment}psql -q";
  38. }
  39. /*
  40. * @param $hide_password
  41. * Not used in postgres. Use .pgpass file instead. See http://drupal.org/node/438828.
  42. */
  43. public function creds($hide_password = TRUE) {
  44. // Some drush commands (e.g. site-install) want to connect to the
  45. // server, but not the database. Connect to the built-in database.
  46. $parameters['dbname'] = empty($this->db_spec['database']) ? 'template1' : $this->db_spec['database'];
  47. // Host and port are optional but have defaults.
  48. $parameters['host'] = empty($this->db_spec['host']) ? 'localhost' : $this->db_spec['host'];
  49. $parameters['port'] = empty($this->db_spec['port']) ? '5432' : $this->db_spec['port'];
  50. // Username is required.
  51. $parameters['username'] = $this->db_spec['username'];
  52. // Don't set the password.
  53. // @see http://drupal.org/node/438828
  54. return $this->params_to_options($parameters);
  55. }
  56. public function createdb_sql($dbname, $quoted = FALSE) {
  57. if ($quoted) {
  58. $dbname = '`' . $dbname . '`';
  59. }
  60. $sql[] = sprintf('drop database if exists %s;', $dbname);
  61. $sql[] = sprintf("create database %s ENCODING 'UTF8';", $dbname);
  62. return implode(' ', $sql);
  63. }
  64. public function db_exists() {
  65. $database = $this->db_spec['database'];
  66. // Get a new class instance that has no 'database'.
  67. $db_spec_no_db = $this->db_spec;
  68. unset($db_spec_no_db['database']);
  69. $sql_no_db = drush_sql_get_class($db_spec_no_db);
  70. $query = "SELECT 1 AS result FROM pg_database WHERE datname='$database'";
  71. drush_shell_exec($sql_no_db->connect() . ' -t -c %s', $query);
  72. $output = drush_shell_exec_output();
  73. return (bool)$output[0];
  74. }
  75. public function query_format($query) {
  76. if (strtolower($query) == 'show tables;') {
  77. return PSQL_SHOW_TABLES;
  78. }
  79. return $query;
  80. }
  81. public function listTables() {
  82. $return = $this->query(PSQL_SHOW_TABLES);
  83. $tables = drush_shell_exec_output();
  84. if (!empty($tables)) {
  85. return $tables;
  86. }
  87. return array();
  88. }
  89. public function dumpCmd($table_selection) {
  90. $parens = FALSE;
  91. $skip_tables = $table_selection['skip'];
  92. $structure_tables = $table_selection['structure'];
  93. $tables = $table_selection['tables'];
  94. $ignores = array();
  95. $skip_tables = array_merge($structure_tables, $skip_tables);
  96. $data_only = drush_get_option('data-only');
  97. $create_db = drush_get_option('create-db');
  98. $exec = 'pg_dump ';
  99. // Unlike psql, pg_dump does not take a '--dbname=' before the database name.
  100. $extra = str_replace('--dbname=', ' ', $this->creds());
  101. if (isset($data_only)) {
  102. $extra .= ' --data-only';
  103. }
  104. $exec .= $extra;
  105. $exec .= (!isset($create_db) && !isset($data_only) ? ' --clean' : '');
  106. if (!empty($tables)) {
  107. foreach ($tables as $table) {
  108. $exec .= " --table=$table";
  109. }
  110. }
  111. else {
  112. foreach ($skip_tables as $table) {
  113. $ignores[] = "--exclude-table=$table";
  114. }
  115. $exec .= ' '. implode(' ', $ignores);
  116. // Run pg_dump again and append output if we need some structure only tables.
  117. if (!empty($structure_tables)) {
  118. $parens = TRUE;
  119. $schemaonlies = array();
  120. foreach ($structure_tables as $table) {
  121. $schemaonlies[] = "--table=$table";
  122. }
  123. $exec .= " && pg_dump --schema-only " . implode(' ', $schemaonlies) . $extra;
  124. $exec .= (!isset($create_db) && !isset($data_only) ? ' --clean' : '');
  125. }
  126. }
  127. return $parens ? "($exec)" : $exec;
  128. }
  129. }