2 namespace Drush\Commands\sql;
4 use Drupal\Core\Database\Database;
5 use Drush\Commands\DrushCommands;
7 use Drush\Exceptions\UserAbortException;
10 class SqlCommands extends DrushCommands
14 * Print database connection details using print_r().
18 * @option all Show all database connections, instead of just one.
19 * @option show-passwords Show database password.
21 * @bootstrap max configuration
24 public function conf($options = ['format' => 'yaml', 'all' => false, 'show-passwords' => false])
26 if ($options['all']) {
27 $return = Database::getAllConnectionInfo();
28 foreach ($return as $key1 => $value) {
29 foreach ($value as $key2 => $spec) {
30 if (!$options['show-passwords']) {
31 unset($return[$key1][$key2]['password']);
36 $sql = SqlBase::create($options);
37 $return = $sql->getDbSpec();
38 if (!$options['show-passwords']) {
39 unset($return['password']);
46 * A string for connecting to the DB.
48 * @command sql:connect
49 * @aliases sql-connect
50 * @option extra Add custom options to the connect string (e.g. --extra=--skip-column-names)
52 * @bootstrap max configuration
53 * @usage `drush sql-connect` < example.sql
54 * Bash: Import SQL statements from a file into the current database.
55 * @usage eval (drush sql-connect) < example.sql
56 * Fish: Import SQL statements from a file into the current database.
58 public function connect($options = ['extra' => self::REQ])
60 $sql = SqlBase::create($options);
61 return $sql->connect(false);
69 * @option db-su Account to use when creating a new database.
70 * @option db-su-pw Password for the db-su account.
72 * @usage drush sql:create
73 * Create the database for the current site.
74 * @usage drush @site.test sql-create
75 * Create the database as specified for @site.test.
76 * @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"
77 * Create the database as specified in the db-url option.
78 * @bootstrap max configuration
80 public function create($options = ['db-su' => self::REQ, 'db-su-pw' => self::REQ])
82 $sql = SqlBase::create($options);
83 $db_spec = $sql->getDbSpec();
84 // Prompt for confirmation.
85 if (!Drush::simulate()) {
86 // @todo odd - maybe for sql-sync.
87 $txt_destination = (isset($db_spec['remote-host']) ? $db_spec['remote-host'] . '/' : '') . $db_spec['database'];
88 $this->output()->writeln(dt("Creating database !target. Any existing database will be dropped!", ['!target' => $txt_destination]));
90 if (!$this->io()->confirm(dt('Do you really want to continue?'))) {
91 throw new UserAbortException();
94 if (!$sql->createdb(true)) {
95 throw new \Exception('Unable to create database. Rerun with --debug to see any error message.');
101 * Drop all tables in a given database.
106 * @bootstrap max configuration
107 * @topics docs:policy
109 public function drop($options = [])
111 $sql = SqlBase::create($options);
112 $db_spec = $sql->getDbSpec();
113 if (!$this->io()->confirm(dt('Do you really want to drop all tables in the database !db?', ['!db' => $db_spec['database']]))) {
114 throw new UserAbortException();
116 $tables = $sql->listTables();
117 if (!$sql->drop($tables)) {
118 throw new \Exception('Unable to drop database. Rerun with --debug to see any error message.');
123 * Open a SQL command-line interface using Drupal's credentials.
126 * @option extra Add custom options to the connect string (e.g. --extra=--skip-column-names)
128 * @aliases sqlc,sql-cli
129 * @usage drush sql:cli
130 * Open a SQL command-line interface using Drupal's credentials.
131 * @usage drush sql:cli --extra=-A
132 * Open a SQL CLI and skip reading table information.
134 * @bootstrap max configuration
136 public function cli($options = [])
138 $sql = SqlBase::create($options);
139 if (drush_shell_proc_open($sql->connect())) {
140 throw new \Exception('Unable to open database shell. Rerun with --debug to see any error message.');
145 * Execute a query against a database.
148 * @param $query An SQL query. Ignored if --file is provided.
150 * @option result-file Save to a file. The file should be relative to Drupal root.
151 * @option file Path to a file containing the SQL to be run. Gzip files are accepted.
152 * @option extra Add custom options to the connect string (e.g. --extra=--skip-column-names)
153 * @option db-prefix Enable replacement of braces in your query.
154 * @validate-file-exists file
155 * @aliases sqlq,sql-query
156 * @usage drush sql:query "SELECT * FROM users WHERE uid=1"
157 * Browse user record. Table prefixes, if used, must be added to table names by hand.
158 * @usage drush sql:query --db-prefix "SELECT * FROM {users}"
159 * Browse user record. Table prefixes are honored. Caution: All curly-braces will be stripped.
160 * @usage `drush sql-connect` < example.sql
161 * Import sql statements from a file into the current database.
162 * @usage drush sql:query --file=example.sql
163 * Alternate way to import sql statements from a file.
164 * @bootstrap max configuration
167 public function query($query = '', $options = ['result-file' => null, 'file' => self::REQ, 'extra' => self::REQ, 'db-prefix' => false])
169 $filename = $options['file'];
170 // Enable prefix processing when db-prefix option is used.
171 if ($options['db-prefix']) {
172 Drush::bootstrapManager()->bootstrapMax(DRUSH_BOOTSTRAP_DRUPAL_DATABASE);
174 if (Drush::simulate()) {
176 $this->output()->writeln(dt('Simulating sql-query: !q', ['!q' => $query]));
178 $this->output()->writeln(dt('Simulating sql-import from !f', ['!f' => $options['file']]));
181 $sql = SqlBase::create($options);
182 $result = $sql->query($query, $filename, $options['result-file']);
184 throw new \Exception(dt('Query failed.'));
186 $this->output()->writeln(implode("\n", drush_shell_exec_output()));
192 * Exports the Drupal DB as SQL using mysqldump or equivalent.
197 * @optionset_table_selection
198 * @option result-file Save to a file. The file should be relative to Drupal root. If --result-file is provided with the value 'auto', a date-based filename will be created under ~/drush-backups directory.
199 * @option create-db Omit DROP TABLE statements. Used by Postgres and Oracle only.
200 * @option data-only Dump data without statements to create any of the schema.
201 * @option ordered-dump Order by primary key and add line breaks for efficient diffs. Slows down the dump. Mysql only.
202 * @option gzip Compress the dump using the gzip program which must be in your $PATH.
203 * @option extra Add custom arguments/options when connecting to database (used internally to list tables).
204 * @option extra-dump Add custom arguments/options to the dumping the database (e.g. mysqldump command).
205 * @usage drush sql:dump --result-file=../18.sql
206 * Save SQL dump to the directory above Drupal root.
207 * @usage drush sql:dump --skip-tables-key=common
208 * Skip standard tables. @see example.drush.yml
209 * @usage drush sql:dump --extra-dump=--no-data
210 * Pass extra option to mysqldump command.
211 * @hidden-options create-db
212 * @bootstrap max configuration
215 * createdb is used by sql-sync, since including the DROP TABLE statements interfere with the import when the database is created.
217 public function dump($options = ['result-file' => self::REQ, 'create-db' => false, 'data-only' => false, 'ordered-dump' => false, 'gzip' => false, 'extra' => self::REQ, 'extra-dump' => self::REQ])
219 $sql = SqlBase::create($options);
220 if ($sql->dump() === false) {
221 throw new \Exception('Unable to dump database. Rerun with --debug to see any error message.');