d7ee5743eb80ee4b5f25aa2c16df3d03d57d44e8
[yaffs-website] / vendor / drush / drush / src / Commands / sql / SqlCommands.php
1 <?php
2 namespace Drush\Commands\sql;
3
4 use Drupal\Core\Database\Database;
5 use Drush\Commands\DrushCommands;
6 use Drush\Drush;
7 use Drush\Exceptions\UserAbortException;
8 use Drush\Sql\SqlBase;
9
10 class SqlCommands extends DrushCommands
11 {
12
13     /**
14      * Print database connection details using print_r().
15      *
16      * @command sql:conf
17      * @aliases sql-conf
18      * @option all Show all database connections, instead of just one.
19      * @option show-passwords Show database password.
20      * @optionset_sql
21      * @bootstrap max configuration
22      * @hidden
23      */
24     public function conf($options = ['format' => 'yaml', 'all' => false, 'show-passwords' => false])
25     {
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']);
32                     }
33                 }
34             }
35         } else {
36             $sql = SqlBase::create($options);
37             $return = $sql->getDbSpec();
38             if (!$options['show-passwords']) {
39                 unset($return['password']);
40             }
41         }
42         return $return;
43     }
44
45     /**
46      * A string for connecting to the DB.
47      *
48      * @command sql:connect
49      * @aliases sql-connect
50      * @option extra Add custom options to the connect string (e.g. --extra=--skip-column-names)
51      * @optionset_sql
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.
57      */
58     public function connect($options = ['extra' => self::REQ])
59     {
60         $sql = SqlBase::create($options);
61         return $sql->connect(false);
62     }
63
64     /**
65      * Create a database.
66      *
67      * @command sql:create
68      * @aliases sql-create
69      * @option db-su Account to use when creating a new database.
70      * @option db-su-pw Password for the db-su account.
71      * @optionset_sql
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
79      */
80     public function create($options = ['db-su' => self::REQ, 'db-su-pw' => self::REQ])
81     {
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]));
89
90             if (!$this->io()->confirm(dt('Do you really want to continue?'))) {
91                 throw new UserAbortException();
92             }
93
94             if (!$sql->createdb(true)) {
95                 throw new \Exception('Unable to create database. Rerun with --debug to see any error message.');
96             }
97         }
98     }
99
100     /**
101      * Drop all tables in a given database.
102      *
103      * @command sql:drop
104      * @aliases sql-drop
105      * @optionset_sql
106      * @bootstrap max configuration
107      * @topics docs:policy
108      */
109     public function drop($options = [])
110     {
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();
115         }
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.');
119         }
120     }
121
122     /**
123      * Open a SQL command-line interface using Drupal's credentials.
124      *
125      * @command sql:cli
126      * @option extra Add custom options to the connect string (e.g. --extra=--skip-column-names)
127      * @optionset_sql
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.
133      * @remote-tty
134      * @bootstrap max configuration
135      */
136     public function cli($options = [])
137     {
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.');
141         }
142     }
143
144     /**
145      * Execute a query against a database.
146      *
147      * @command sql:query
148      * @param $query An SQL query. Ignored if --file is provided.
149      * @optionset_sql
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
165      *
166      */
167     public function query($query = '', $options = ['result-file' => null, 'file' => self::REQ, 'extra' => self::REQ, 'db-prefix' => false])
168     {
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);
173         }
174         if (Drush::simulate()) {
175             if ($query) {
176                 $this->output()->writeln(dt('Simulating sql-query: !q', ['!q' => $query]));
177             } else {
178                 $this->output()->writeln(dt('Simulating sql-import from !f', ['!f' => $options['file']]));
179             }
180         } else {
181             $sql = SqlBase::create($options);
182             $result = $sql->query($query, $filename, $options['result-file']);
183             if (!$result) {
184                 throw new \Exception(dt('Query failed.'));
185             }
186             $this->output()->writeln(implode("\n", drush_shell_exec_output()));
187         }
188         return true;
189     }
190
191     /**
192      * Exports the Drupal DB as SQL using mysqldump or equivalent.
193      *
194      * @command sql:dump
195      * @aliases sql-dump
196      * @optionset_sql
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
213      *
214      * @notes
215      *   createdb is used by sql-sync, since including the DROP TABLE statements interfere with the import when the database is created.
216      */
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])
218     {
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.');
222         }
223     }
224 }