sql.drush.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. <?php
  2. /**
  3. * @file Drush sql commands
  4. */
  5. /**
  6. * Implementation of hook_drush_help().
  7. */
  8. function sql_drush_help($section) {
  9. switch ($section) {
  10. case 'meta:sql:title':
  11. return dt('SQL commands');
  12. case 'meta:sql:summary':
  13. return dt('Examine and modify your Drupal database.');
  14. }
  15. }
  16. /**
  17. * Implementation of hook_drush_command().
  18. */
  19. function sql_drush_command() {
  20. $options['database'] = 'The DB connection key if using multiple connections in settings.php.';
  21. if (drush_drupal_major_version() >= 7) {
  22. $options['target'] = 'The name of a target within the specified database.';
  23. }
  24. $items['sql-drop'] = array(
  25. 'description' => 'Drop all tables in a given database.',
  26. 'arguments' => array(
  27. ),
  28. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
  29. 'options' => array(
  30. 'yes' => 'Skip confirmation and proceed.',
  31. 'result-file' => 'Save to a file. The file should be relative to Drupal root. Recommended.',
  32. ) + $options,
  33. 'topics' => array('docs-policy'),
  34. );
  35. $items['sql-conf'] = array(
  36. 'description' => 'Print database connection details using print_r().',
  37. 'hidden' => TRUE,
  38. 'arguments' => array(
  39. 'all' => 'Show all database connections, instead of just one.',
  40. 'show-passwords' => 'Show database password.',
  41. ),
  42. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
  43. 'options' => $options,
  44. );
  45. $items['sql-connect'] = array(
  46. 'description' => 'A string for connecting to the DB.',
  47. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
  48. 'options' => $options,
  49. 'examples' => array(
  50. '`drush sql-connect` < example.sql' => 'Import sql statements from a file into the current database.',
  51. ),
  52. );
  53. $items['sql-dump'] = array(
  54. 'callback' => 'drush_sql_dump_execute',
  55. 'description' => 'Exports the Drupal DB as SQL using mysqldump or equivalent.',
  56. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
  57. 'examples' => array(
  58. 'drush sql-dump --result-file=../18.sql' => 'Save SQL dump to the directory above Drupal root.',
  59. 'drush sql-dump --skip-tables-key=common' => 'Skip standard tables. @see example.drushrc.com',
  60. ),
  61. 'options' => array(
  62. 'result-file' => 'Save to a file. The file should be relative to Drupal root. If --result-file is provided with no value, then date based filename will be created under ~/drush-backups directory.',
  63. 'skip-tables-key' => 'A key in the $skip_tables array. @see example.drushrc.php. Optional.',
  64. 'structure-tables-key' => 'A key in the $structure_tables array. @see example.drushrc.php. Optional.',
  65. 'tables-key' => 'A key in the $tables array. Optional.',
  66. 'tables-list' => 'A comma-separated list of tables to transfer. Optional.',
  67. 'ordered-dump' => 'Use this option to output ordered INSERT statements in the sql-dump.Useful when backups are managed in a Version Control System. Optional.',
  68. 'create-db' => 'Wipe existing tables.',
  69. 'data-only' => 'Omit CREATE TABLE statements. Postgres only.',
  70. 'ordered-dump' => 'Order by primary key and add line breaks for efficient diff in revision control. Also, faster rsync. Slows down the dump. Mysql only.',
  71. 'gzip' => 'Compress the dump using the gzip program which must be in your $PATH.',
  72. ) + $options,
  73. );
  74. $items['sql-query'] = array(
  75. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
  76. 'description' => 'Execute a query against the site database.',
  77. 'examples' => array(
  78. 'drush sql-query "SELECT * FROM {users} WHERE uid=1"' => 'Browse user record. Table prefixes are honored.',
  79. '`drush sql-connect` < example.sql' => 'Import sql statements from a file into the current database.',
  80. 'drush sql-query --input-file=example.sql' => 'Alternate way to import sql statements from a file.',
  81. ),
  82. 'arguments' => array(
  83. 'query' => 'An SQL query. Ignored if \'file\' is provided.',
  84. ),
  85. 'options' => array(
  86. 'result-file' => 'Save to a file. The file should be relative to Drupal root. Optional.',
  87. 'input-file' => 'Path to a file containing the SQL to be run.',
  88. 'extra' => 'Add custom options to the mysql command.',
  89. ) + $options,
  90. 'aliases' => array('sqlq'),
  91. );
  92. $items['sql-sync'] = array(
  93. 'description' => 'Copy and import source database to target database. Transfers via rsync.',
  94. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  95. 'drush dependencies' => array('core'), // core-rsync.
  96. 'examples' => array(
  97. 'drush sql-sync @dev @prod' => 'Copy the DB defined in sites/dev to the DB in sites/prod.',
  98. ),
  99. 'arguments' => array(
  100. 'from' => 'Name of subdirectory within /sites or a site-alias.',
  101. 'to' => 'Name of subdirectory within /sites or a site-alias.',
  102. ),
  103. 'options' => array(
  104. 'skip-tables-key' => 'A key in the $skip_tables array. @see example.drushrc.php. Optional.',
  105. 'structure-tables-key' => 'A key in the $structure_tables array. @see example.drushrc.php. Optional.',
  106. 'tables-key' => 'A key in the $tables array. Optional.',
  107. 'tables-list' => 'A comma-separated list of tables to transfer. Optional.',
  108. 'cache' => 'Skip dump if result file exists and is less than "cache" hours old. Optional; default is 24 hours.',
  109. 'no-cache' => 'Do not cache the sql-dump file.',
  110. 'no-dump' => 'Do not dump the sql database; always use an existing dump file.',
  111. 'source-db-url' => 'Database specification for source system to dump from.',
  112. 'source-remote-port' => 'Override sql database port number in source-db-url. Optional.',
  113. 'source-remote-host' => 'Remote machine to run sql-dump file on. Optional; default is local machine.',
  114. 'source-dump' => 'Path to dump file. Optional; default is to create a temporary file.',
  115. 'target-database' => 'A key in the $db_url (D6) or $databases (D7+) array which provides the data.',
  116. 'source-target' => 'Oy. A key within the --target_database identifying a particular server in the database group.',
  117. 'target-db-url' => '',
  118. 'target-remote-port' => '',
  119. 'target-remote-host' => '',
  120. 'target-dump' => '',
  121. 'target-database' => 'A key in the $db_url (D6) or $databases (D7+) array which shall receive the data.',
  122. 'target-target' => 'Oy. A key within the --target_database identifying a particular server in the database group.',
  123. 'temp' => 'Use a temporary file to hold dump files. Implies --no-cache.',
  124. 'dump-dir' => 'Directory to store sql dump files in when --source-dump or --target-dump are not used. Takes precedence over --temp.',
  125. 'create-db' => 'Create a new database before importing the database dump on the target machine.',
  126. 'db-su' => 'Account to use when creating a new database. Optional.',
  127. 'db-su-pw' => 'Password for the "db-su" account. Optional.',
  128. 'no-ordered-dump' => 'Do not pass --ordered-dump to sql-dump. sql-sync orders the dumpfile by default in order to increase the efficiency of rsync.',
  129. 'sanitize' => 'Obscure email addresses and reset passwords in the user table post-sync. Optional.',
  130. ),
  131. 'sub-options' => array(
  132. 'sanitize' => array(
  133. 'sanitize-password' => 'The password to assign to all accounts in the sanitization operation, or "no" to keep passwords unchanged. Default is "password".',
  134. 'sanitize-email' => 'The username for test email addresses in the sanitization operation, or "no" to keep email addresses unchanged. May contain replacement patterns %uid, %mail or %login. Default is "user+%uid@localhost".',
  135. 'confirm-sanitizations' => 'Prompt yes/no after importing the database, but before running the sanitizations',
  136. ),
  137. ),
  138. 'topics' => array('docs-aliases', 'docs-policy'),
  139. );
  140. if (drush_drupal_major_version() >= 7) {
  141. $items['sql-sync']['options'] += array(
  142. 'source-target' => 'The name of a target within the SOURCE database.',
  143. 'destination-target' => 'The name of a target within the specified DESTINATION database.',
  144. );
  145. }
  146. $items['sql-cli'] = array(
  147. 'description' => "Open a SQL command-line interface using Drupal's credentials.",
  148. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
  149. 'options' => $options,
  150. 'aliases' => array('sqlc'),
  151. );
  152. $items['sql-sanitize'] = array(
  153. 'description' => "Run sanitization operations on the current database.",
  154. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
  155. 'hidden' => TRUE,
  156. 'options' => array(
  157. 'sanitize-password' => 'The password to assign to all accounts in the sanitization operation, or "no" to keep passwords unchanged. Default is "password".',
  158. 'sanitize-email' => 'The username for test email addresses in the sanitization operation, or "no" to keep email addresses unchanged. May contain replacement patterns %uid, %mail or %login. Default is "user+%uid@localhost".',
  159. ),
  160. 'aliases' => array('sqlsan'),
  161. );
  162. return $items;
  163. }
  164. /**
  165. * Command callback. Displays the Drupal site's database connection string.
  166. */
  167. function drush_sql_conf() {
  168. if (drush_get_option('db-url', FALSE)) {
  169. $db_spec['db-url'] = $GLOBALS['db_url'];
  170. }
  171. elseif (drush_get_option('all', FALSE)) {
  172. $db_spec = _drush_sql_get_all_db_specs();
  173. }
  174. if (!isset($db_spec)) {
  175. $db_spec = _drush_sql_get_db_spec();
  176. }
  177. drush_backend_set_result($db_spec);
  178. if (!drush_get_option('show-passwords', FALSE)) {
  179. drush_unset_recursive($db_spec, 'password');
  180. }
  181. drush_print_r($db_spec);
  182. }
  183. /**
  184. * Command callback. Emits a connect string for mysql or pgsql.
  185. */
  186. function _drush_sql_connect($db_spec = NULL) {
  187. switch (_drush_sql_get_scheme($db_spec)) {
  188. case 'mysql':
  189. $command = 'mysql';
  190. break;
  191. case 'pgsql':
  192. $command = 'psql';
  193. break;
  194. case 'sqlite':
  195. $command = 'sqlite3';
  196. break;
  197. }
  198. $command .= _drush_sql_get_credentials($db_spec);
  199. return $command;
  200. }
  201. function drush_sql_connect() {
  202. drush_print(_drush_sql_connect());
  203. }
  204. /**
  205. * Command callback. Outputs the entire Drupal database in SQL format using mysqldump.
  206. */
  207. function drush_sql_dump_execute() {
  208. list($exec, $file) = drush_sql_dump();
  209. // Avoid the php memory of the $output array in drush_shell_exec().
  210. if (!$return = drush_op_system($exec)) {
  211. if ($file) {
  212. drush_log(dt('Database dump saved to !path', array('!path' => $file)), 'success');
  213. }
  214. }
  215. return $return;
  216. }
  217. function drush_sql_get_table_selection() {
  218. // Skip large core tables if instructed. Also used by 'sql-sync' command.
  219. $skip_tables = _drush_sql_get_table_list('skip-tables');
  220. // Skip any structure-tables as well.
  221. $structure_tables = _drush_sql_get_table_list('structure-tables');
  222. // Dump only the specified tables. Takes precedence over skip-tables and structure-tables.
  223. $tables = _drush_sql_get_table_list('tables');
  224. return array('skip' => $skip_tables, 'structure' => $structure_tables, 'tables' => $tables);
  225. }
  226. /**
  227. * Build a mysqldump/pg_dump/sqlite statement.
  228. *
  229. * @param db_spec
  230. * For D5/D6, a $db_url. For D7, a target in the default DB connection.
  231. * @return array
  232. * An array with items.
  233. * 1. A mysqldump/pg_dump/sqlite statement that is ready for executing.
  234. * 2. The filepath where the dump will be saved.
  235. */
  236. function drush_sql_dump($db_spec = NULL) {
  237. return drush_sql_build_dump_command(drush_sql_get_table_selection(), $db_spec);
  238. }
  239. /**
  240. * Build a mysqldump/pg_dump/sqlite statement.
  241. *
  242. * @param array $table_selection
  243. * Supported keys: 'skip', 'structure', 'tables'.
  244. * @param db_spec
  245. * For D5/D6, a $db_url. For D7, a target in the default DB connection.
  246. * @return array
  247. * An array with items.
  248. * 1. A mysqldump/pg_dump/sqlite statement that is ready for executing.
  249. * 2. The filepath where the dump will be saved.
  250. */
  251. function drush_sql_build_dump_command($table_selection, $db_spec = NULL) {
  252. $skip_tables = $table_selection['skip'];
  253. $structure_tables = $table_selection['structure'];
  254. $tables = $table_selection['tables'];
  255. $ignores = array();
  256. $skip_tables = array_merge($structure_tables, $skip_tables);
  257. $data_only = drush_get_option('data-only');
  258. // The ordered-dump option is only supported by MySQL for now.
  259. // @todo add documention once a hook for drush_get_option_help() is available.
  260. // @see drush_get_option_help() in drush.inc
  261. $ordered_dump = drush_get_option('ordered-dump');
  262. if (is_null($db_spec)) {
  263. $db_spec = _drush_sql_get_db_spec();
  264. }
  265. $database = $db_spec['database'];
  266. // Get the setting of --result-file. If the user
  267. // has set $options['result-file'] = TRUE, then we
  268. // will generate an SQL dump file in the same backup
  269. // directory that pm-updatecode uses.
  270. $file = NULL;
  271. if ($file = drush_get_option('result-file', FALSE)) {
  272. if ($file === TRUE) {
  273. // User did not pass a specific value for --result-file. Make one.
  274. drush_include_engine('version_control', 'backup');
  275. $backup = new drush_pm_version_control_backup();
  276. $backup_dir = $backup->prepare_backup_dir($db_spec['database']);
  277. if (empty($backup_dir)) {
  278. $backup_dir = "/tmp";
  279. }
  280. $file = $backup_dir . '/@DATABASE_@DATE.sql';
  281. }
  282. $file = str_replace(array('@DATABASE', '@DATE'), array($database, gmdate('Ymd_his')), $file);
  283. }
  284. switch (_drush_sql_get_scheme($db_spec)) {
  285. case 'mysql':
  286. $exec = 'mysqldump';
  287. if ($file) {
  288. $exec .= ' --result-file '. $file;
  289. }
  290. // mysqldump wants 'databasename' instead of 'database=databasename' for no good reason.
  291. $extra = ' --single-transaction --opt -Q' . str_replace('--database=', ' ', _drush_sql_get_credentials($db_spec));
  292. if (isset($data_only)) {
  293. $extra .= ' --no-create-info';
  294. }
  295. if (isset($ordered_dump)) {
  296. $extra .= ' --skip-extended-insert --order-by-primary';
  297. }
  298. $exec .= $extra;
  299. if (!empty($tables)) {
  300. $exec .= ' ' . implode(' ', $tables);
  301. }
  302. else {
  303. // Append the ignore-table options.
  304. foreach ($skip_tables as $table) {
  305. $ignores[] = "--ignore-table=$database.$table";
  306. }
  307. $exec .= ' '. implode(' ', $ignores);
  308. // Run mysqldump again and append output if we need some structure only tables.
  309. if (!empty($structure_tables)) {
  310. $exec .= "; mysqldump --no-data $extra " . implode(' ', $structure_tables);
  311. if ($file) {
  312. $exec .= " >> $file";
  313. }
  314. }
  315. }
  316. break;
  317. case 'pgsql':
  318. $create_db = drush_get_option('create-db');
  319. $exec = 'pg_dump ';
  320. if ($file) {
  321. $exec .= ' --file '. $file;
  322. }
  323. // Unlike psql, pg_dump does not take a '--dbname=' before the database name.
  324. $extra = str_replace('--dbname=', ' ', _drush_sql_get_credentials($db_spec));
  325. if (isset($data_only)) {
  326. $extra .= ' --data-only';
  327. }
  328. $exec .= $extra;
  329. $exec .= (!isset($create_db) && !isset($data_only) ? ' --clean' : '');
  330. if (!empty($tables)) {
  331. foreach ($tables as $table) {
  332. $exec .= " --table=$table";
  333. }
  334. }
  335. else {
  336. foreach ($skip_tables as $table) {
  337. $ignores[] = "--exclude-table=$table";
  338. }
  339. $exec .= ' '. implode(' ', $ignores);
  340. // Run pg_dump again and append output if we need some structure only tables.
  341. if (!empty($structure_tables)) {
  342. $schemaonlies = array();
  343. foreach ($structure_tables as $table) {
  344. $schemaonlies[] = "--table=$table";
  345. }
  346. $exec .= "; pg_dump --schema-only " . implode(' ', $schemaonlies) . $extra;
  347. if ($file) {
  348. $exec .= " >> $file";
  349. }
  350. }
  351. }
  352. break;
  353. case 'sqlite':
  354. // Dumping is usually not necessary in SQLite, since all database data
  355. // is stored in a single file on the filesystem which can be copied just
  356. // like any other file. But it still has a use in migration purposes and
  357. // building human-readable diffs and such, so let's do it anyway.
  358. $exec = _drush_sql_connect();
  359. // SQLite's dump command doesn't support many of the features of its
  360. // Postgres or MySQL equivalents. We may be able to fake some in the
  361. // future, but for now, let's just support simple dumps.
  362. $exec .= ' ".dump"';
  363. if ($file = drush_get_option('result-file')) {
  364. $exec .= ' > '. $file;
  365. }
  366. break;
  367. }
  368. if (drush_get_option('gzip')) {
  369. if ($file) {
  370. // Gzip the result-file
  371. $exec .= "; gzip $file";
  372. $file .= '.gz';
  373. }
  374. else {
  375. // gzip via pipe since user has not specified a file.
  376. $exec .= "| gzip";
  377. }
  378. }
  379. return array($exec, $file);
  380. }
  381. /**
  382. * Consult the specified options and return the list of tables
  383. * specified.
  384. *
  385. * @param option_name
  386. * The option name to check: skip-tables, structure-tables
  387. * or tables. This funciton will check both *-key and *-list,
  388. * and, in the case of sql-sync, will also check target-*
  389. * and source-*, to see if an alias set one of these options.
  390. * @returns array
  391. * Returns an array of tables based on the first option
  392. * found, or an empty array if there were no matches.
  393. */
  394. function _drush_sql_get_table_list($option_name) {
  395. foreach(array('' => 'cli', 'target-,,source-' => NULL) as $prefix_list => $context) {
  396. foreach(explode(',',$prefix_list) as $prefix) {
  397. $key_list = drush_get_option($prefix . $option_name . '-key', NULL, $context);
  398. foreach(explode(',', $key_list) as $key) {
  399. $all_tables = drush_get_option($option_name, array());
  400. if (array_key_exists($key, $all_tables)) {
  401. return $all_tables[$key];
  402. }
  403. if ($option_name != 'tables') {
  404. $all_tables = drush_get_option('tables', array());
  405. if (array_key_exists($key, $all_tables)) {
  406. return $all_tables[$key];
  407. }
  408. }
  409. }
  410. $table_list = drush_get_option($prefix . $option_name . '-list', NULL, $context);
  411. if (isset($table_list)) {
  412. return empty($table_list) ? array() : explode(',', $table_list);
  413. }
  414. }
  415. }
  416. return array();
  417. }
  418. /**
  419. * Command callback. Executes the given SQL query on the Drupal database.
  420. */
  421. function drush_sql_query($query) {
  422. $filename = drush_get_option('file', NULL);
  423. return _drush_sql_query($query, NULL, $filename);
  424. }
  425. /*
  426. * Execute a SQL query.
  427. *
  428. * @param string $query
  429. * The SQL to be executed. Should be NULL if $file is provided.
  430. * @param array $db_spec
  431. * A database target.
  432. * @param string $filename
  433. * A path to a file containing the SQL to be executed.
  434. */
  435. function _drush_sql_query($query, $db_spec = NULL, $filename = NULL) {
  436. $scheme = _drush_sql_get_scheme($db_spec);
  437. // Inject table prefixes as needed.
  438. if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_DATABASE)) {
  439. if ($filename) {
  440. $query = file_get_contents($filename);
  441. }
  442. if (drush_drupal_major_version() >= 7) {
  443. $query = Database::getConnection()->prefixTables($query);
  444. }
  445. else {
  446. $query = db_prefix_tables($query);
  447. }
  448. }
  449. // Convert mysql 'show tables;' query into something pgsql understands
  450. if (($scheme == 'pgsql') && ($query == 'show tables;')) {
  451. $query = drush_sql_show_tables_pgsql();
  452. }
  453. // Save $query to a tmp file if needed. We will redirect it in.
  454. if (!$filename) {
  455. $filename = drush_save_data_to_temp_file($query);
  456. }
  457. $exec = drush_sql_build_exec($db_spec, $filename);
  458. if ($output_file = drush_get_option('result-file')) {
  459. $exec .= ' > '. drush_escapeshellarg($output_file);
  460. }
  461. // In --simulate mode, drush_op will show the call to mysql or psql,
  462. // but the sql query itself is stored in a temp file and not displayed.
  463. // We will therefore show the query explicitly in the interest of full disclosure.
  464. if (drush_get_context('DRUSH_SIMULATE')) {
  465. drush_print('sql-query: ' . $query);
  466. }
  467. $return = drush_op_system($exec) == 0;
  468. return $return;
  469. }
  470. function drush_sql_drop() {
  471. if (!drush_confirm(dt('Do you really want to drop all tables?'))) {
  472. return drush_user_abort();
  473. }
  474. _drush_sql_drop();
  475. }
  476. // n.b. site-install uses _drush_sql_drop as a fallback technique if
  477. // drop database; create database fails. If _drush_sql_drop
  478. // is rewritten to also use that technique, it should maintain
  479. // the drop tables code here as a fallback.
  480. function _drush_sql_drop($db_spec = NULL) {
  481. // TODO: integrate with _drush_sql_get_table_list?
  482. $scheme = _drush_sql_get_scheme($db_spec);
  483. switch ($scheme) {
  484. case 'pgsql':
  485. $query = drush_sql_show_tables_pgsql();
  486. break;
  487. case 'sqlite':
  488. $query = '.tables';
  489. break;
  490. default:
  491. $query = 'SHOW TABLES;';
  492. }
  493. $filename = drush_save_data_to_temp_file($query);
  494. $exec = drush_sql_build_exec($db_spec, $filename);
  495. // Actually run this prep query no matter if in SIMULATE.
  496. $old = drush_get_context('DRUSH_SIMULATE');
  497. drush_set_context('DRUSH_SIMULATE', FALSE);
  498. drush_shell_exec($exec);
  499. drush_set_context('DRUSH_SIMULATE', $old);
  500. if ($tables = drush_shell_exec_output()) {
  501. if ($scheme === 'sqlite') {
  502. // SQLite's '.tables' command always outputs the table names in a column
  503. // format, like this:
  504. // table_alpha table_charlie table_echo
  505. // table_bravo table_delta table_foxtrot
  506. // …and there doesn't seem to be a way to fix that. So we need to do some
  507. // clean-up.
  508. // Since we're already doing iteration here, might as well build the SQL
  509. // too, since SQLite only wants one table per DROP TABLE command (so we have
  510. // to do "DROP TABLE foo; DROP TABLE bar;" instead of
  511. // "DROP TABLE foo, bar;").
  512. $sql = '';
  513. foreach ($tables as $line) {
  514. preg_match_all('/[^\s]+/', $line, $matches);
  515. if (!empty($matches[0])) {
  516. foreach ($matches[0] as $match) {
  517. $sql .= "DROP TABLE {$match};";
  518. }
  519. }
  520. }
  521. // We can't use drush_op('db_query', $sql) because it will only perform one
  522. // SQL command and we're technically performing several.
  523. $exec = _drush_sql_connect($db_spec);
  524. $exec .= " '{$sql}'";
  525. return drush_op_system($exec) == 0;
  526. }
  527. else {
  528. // Shift off the header of the column of data returned.
  529. array_shift($tables);
  530. $sql = 'DROP TABLE '. implode(', ', $tables);
  531. return _drush_sql_query($sql, $db_spec);
  532. }
  533. }
  534. else {
  535. drush_log(dt('No tables to drop.'), 'ok');
  536. }
  537. return TRUE;
  538. }
  539. function drush_sql_cli() {
  540. proc_close(proc_open(_drush_sql_connect(), array(0 => STDIN, 1 => STDOUT, 2 => STDERR), $pipes));
  541. }
  542. /**
  543. * Command callback. Run's the sanitization operations on the current database.
  544. */
  545. function drush_sql_sanitize() {
  546. if (!drush_confirm(dt('Do you really want to sanitize the current database?'))) {
  547. return drush_user_abort();
  548. }
  549. drush_include(DRUSH_BASE_PATH . '/commands/sql', 'sync.sql');
  550. drush_command_invoke_all('drush_sql_sync_sanitize', 'default');
  551. $options = drush_get_context('post-sync-ops');
  552. if (!empty($options)) {
  553. if (!drush_get_context('DRUSH_SIMULATE')) {
  554. $messages = _drush_sql_get_post_sync_messages();
  555. if ($messages) {
  556. drush_print();
  557. drush_print($messages);
  558. }
  559. }
  560. }
  561. $sanitize_query = '';
  562. foreach($options as $id => $data) {
  563. $sanitize_query .= $data['query'] . " ";
  564. }
  565. if ($sanitize_query) {
  566. if (!drush_get_context('DRUSH_SIMULATE')) {
  567. drush_sql_query($sanitize_query);
  568. }
  569. else {
  570. drush_print("Executing: $sanitize_query");
  571. }
  572. }
  573. }
  574. //////////////////////////////////////////////////////////////////////////////
  575. // SQL SERVICE HELPERS
  576. /**
  577. * Get a database specification for the active DB connection. Honors the
  578. * 'database' and 'target command' line options.
  579. *
  580. * @return
  581. * An info array describing a database target.
  582. */
  583. function _drush_sql_get_db_spec() {
  584. $database = drush_get_option('database', 'default');
  585. $target = drush_get_option('target', 'default');
  586. switch (drush_drupal_major_version()) {
  587. case 5:
  588. case 6:
  589. $url = $GLOBALS['db_url'];
  590. // TODO: array version not working?
  591. $url = is_array($url) ? $url[$database] : $url;
  592. return drush_convert_db_from_db_url($url);
  593. default:
  594. // We don't use DB API here `sql-sync` would have to messily addConnection.
  595. if (!isset($GLOBALS['databases']) || !array_key_exists($database, $GLOBALS['databases']) || !array_key_exists($target, $GLOBALS['databases'][$database])) {
  596. return NULL;
  597. }
  598. return $GLOBALS['databases'][$database][$target];
  599. }
  600. }
  601. function _drush_sql_get_all_db_specs() {
  602. switch (drush_drupal_major_version()) {
  603. case 5:
  604. case 6:
  605. return drush_sitealias_convert_db_from_db_url($GLOBALS['db_url']);
  606. default:
  607. if (!isset($GLOBALS['databases'])) {
  608. return NULL;
  609. }
  610. return $GLOBALS['databases'];
  611. }
  612. }
  613. function _drush_sql_get_spec_from_options($prefix, $default_to_self = TRUE) {
  614. $db_spec = NULL;
  615. $databases = drush_get_option($prefix . 'databases');
  616. if (isset($databases) && !empty($databases)) {
  617. $database = drush_get_option($prefix . 'database', 'default');
  618. $target = drush_get_option($prefix . 'target', 'default');
  619. if (array_key_exists($database, $databases) && array_key_exists($target, $databases[$database])) {
  620. $db_spec = $databases[$database][$target];
  621. }
  622. }
  623. else {
  624. $db_url = drush_get_option($prefix . 'db-url');
  625. if (isset($db_url)) {
  626. $db_spec = drush_convert_db_from_db_url($db_url);
  627. }
  628. elseif ($default_to_self) {
  629. $db_spec = _drush_sql_get_db_spec();
  630. }
  631. }
  632. if (isset($db_spec)) {
  633. $remote_host = drush_get_option($prefix . 'remote-host');
  634. if (!drush_is_local_host($remote_host)) {
  635. $db_spec['remote-host'] = $remote_host;
  636. $db_spec['port'] = drush_get_option($prefix . 'remote-port', $db_spec['port']);
  637. }
  638. }
  639. return $db_spec;
  640. }
  641. /**
  642. * Determine where to store an sql dump file. This
  643. * function is called by sql-sync if the caller did
  644. * not explicitly specify a dump file to use.
  645. *
  646. * @param db_spec
  647. * Information about the database being dumped; used
  648. * to generate the filename.
  649. * @return string
  650. * The path to the dump file
  651. */
  652. function drush_sql_dump_file(&$db_spec, $prefix) {
  653. // Use an entry in the db spec to indicate whether the dump
  654. // file we use is temporary or not.
  655. $db_spec['dump-is-temp'] = FALSE;
  656. // Make a base filename pattern to use to name the dump file
  657. $filename_pattern = $db_spec['database'];
  658. if (isset($db_spec['remote-host'])) {
  659. $filename_pattern = $db_spec['remote-host'] . '_' . $filename_pattern;
  660. }
  661. // If the user has set the --{prefix}-dir option, then
  662. // use the exact name provided.
  663. $dump_file = drush_get_option($prefix . 'dump');
  664. if (!isset($dump_file)) {
  665. // If the user has set the --dump-dir option, then
  666. // store persistant sql dump files there.
  667. $dump_dir = drush_get_option(array($prefix . 'dump-dir', 'dump-dir'));
  668. if (isset($dump_dir)) {
  669. $dump_file = $dump_dir . '/' . $filename_pattern . '.sql';
  670. }
  671. // If the --dump-dir option is not set, then store
  672. // the sql dump in a temporary file.
  673. else {
  674. $dump_file = drush_tempnam($filename_pattern . '.sql.');
  675. $db_spec['dump-is-temp'] = TRUE;
  676. }
  677. }
  678. return $dump_file;
  679. }
  680. function _drush_sql_get_scheme($db_spec = NULL) {
  681. if (is_null($db_spec)) {
  682. $db_spec = _drush_sql_get_db_spec();
  683. }
  684. return $db_spec['driver'];
  685. }
  686. /**
  687. * Build a fragment containing credentials and mysql-connection parameters.
  688. *
  689. * @param $db_spec
  690. * @return string
  691. */
  692. function _drush_sql_get_credentials($db_spec = NULL) {
  693. if (is_null($db_spec)) {
  694. $db_spec = _drush_sql_get_db_spec();
  695. }
  696. // Build an array of key-value pairs for the parameters.
  697. $parameters = array();
  698. switch (_drush_sql_get_scheme($db_spec)) {
  699. case 'mysql':
  700. // Some drush commands (e.g. site-install) want to connect to the
  701. // server, but not the database. Connect to the built-in database.
  702. $parameters['database'] = empty($db_spec['database']) ? 'information_schema' : $db_spec['database'];
  703. // Host is required.
  704. $parameters['host'] = $db_spec['host'];
  705. // An empty port is invalid.
  706. if (!empty($db_spec['port'])) {
  707. $parameters['port'] = $db_spec['port'];
  708. }
  709. // User is required. Drupal calls it 'username'. MySQL calls it 'user'.
  710. $parameters['user'] = $db_spec['username'];
  711. // EMPTY password is not the same as NO password, and is valid.
  712. if (isset($db_spec['password'])) {
  713. $parameters['password'] = $db_spec['password'];
  714. }
  715. break;
  716. case 'pgsql':
  717. // Some drush commands (e.g. site-install) want to connect to the
  718. // server, but not the database. Connect to the built-in database.
  719. $parameters['dbname'] = empty($db_spec['database']) ? 'template1' : $db_spec['database'];
  720. // Host and port are optional but have defaults.
  721. $parameters['host'] = empty($db_spec['host']) ? 'localhost' : $db_spec['host'];
  722. $parameters['port'] = empty($db_spec['port']) ? '5432' : $db_spec['port'];
  723. // Username is required.
  724. $parameters['username'] = $db_spec['username'];
  725. // Don't set the password.
  726. // @see http://drupal.org/node/438828
  727. break;
  728. case 'sqlite':
  729. // SQLite doesn't do user management, instead relying on the filesystem
  730. // for that. So the only info we really need is the path to the database
  731. // file, and not as a "--key=value" parameter.
  732. return ' ' . $db_spec['database'];
  733. break;
  734. }
  735. // Turn each parameter into a valid parameter string.
  736. $parameter_strings = array();
  737. foreach ($parameters as $key => $value) {
  738. // Only escape the values, not the keys or the rest of the string.
  739. $value = escapeshellcmd($value);
  740. $parameter_strings[] = "--$key=$value";
  741. }
  742. // Join the parameters and return.
  743. return ' ' . implode(' ', $parameter_strings);
  744. }
  745. function _drush_sql_get_invalid_url_msg($db_spec = NULL) {
  746. if (is_null($db_spec)) {
  747. $db_spec = _drush_sql_get_db_spec();
  748. }
  749. switch (drush_drupal_major_version()) {
  750. case 5:
  751. case 6:
  752. return dt('Unable to parse DB connection string');
  753. default:
  754. return dt('Unable to parse DB connection array');
  755. }
  756. }
  757. /**
  758. * Call from a pre-sql-sync hook to register an sql
  759. * query to be executed in the post-sql-sync hook.
  760. * @see drush_sql_pre_sql_sync() and @see drush_sql_post_sql_sync().
  761. *
  762. * @param $id
  763. * String containing an identifier representing this
  764. * operation. This id is not actually used at the
  765. * moment, it is just used to fufill the contract
  766. * of drush contexts.
  767. * @param $message
  768. * String with the confirmation message that describes
  769. * to the user what the post-sync operation is going
  770. * to do. This confirmation message is printed out
  771. * just before the user is asked whether or not the
  772. * sql-sync operation should be continued.
  773. * @param $query
  774. * String containing the sql query to execute. If no
  775. * query is provided, then the confirmation message will
  776. * be displayed to the user, but no action will be taken
  777. * in the post-sync hook. This is useful for drush modules
  778. * that wish to provide their own post-sync hooks to fix
  779. * up the target database in other ways (e.g. through
  780. * Drupal APIs).
  781. */
  782. function drush_sql_register_post_sync_op($id, $message, $query = NULL) {
  783. $options = drush_get_context('post-sync-ops');
  784. $options[$id] = array('message' => $message, 'query' => $query);
  785. drush_set_context('post-sync-ops', $options);
  786. }
  787. /**
  788. * Builds a confirmation message for all post-sync operations.
  789. *
  790. * @return string
  791. * All post-sync operation messages concatenated together.
  792. */
  793. function _drush_sql_get_post_sync_messages() {
  794. $messages = FALSE;
  795. $options = drush_get_context('post-sync-ops');
  796. if (!empty($options)) {
  797. $messages = dt('The following post-sync operations will be done on the destination:') . "\n";
  798. foreach($options as $id => $data) {
  799. $messages .= " * " . $data['message'] . "\n";
  800. }
  801. }
  802. return $messages;
  803. }
  804. // Convert mysql 'show tables;' query into something pgsql understands.
  805. function drush_sql_show_tables_pgsql() {
  806. return "select tablename from pg_tables where schemaname='public';";
  807. }
  808. /*
  809. * Drop all tables or DROP+CREATE target database.
  810. *
  811. * return boolean
  812. * TRUE or FALSE depending on success.
  813. */
  814. function drush_sql_empty_db($db_spec = NULL) {
  815. if (is_null($db_spec)) {
  816. $db_spec = drush_sql_read_db_spec();
  817. }
  818. $sql = drush_sql_build_createdb_sql($db_spec, TRUE);
  819. // Get credentials to connect to the server, but not the database which we
  820. // are about to DROP. @see _drush_sql_get_credentials().
  821. $create_db_spec = $db_spec;
  822. unset($create_db_spec['database']);
  823. $create_db_su = drush_sql_su($create_db_spec);
  824. if (!_drush_sql_query($sql, $create_db_su)) {
  825. // If we could not drop the database, try instead to drop all
  826. // of the tables in the database (presuming it exists...).
  827. // If we cannot do either operation, then fail with an error.
  828. if (!_drush_sql_drop($db_spec)) {
  829. return drush_set_error(dt('Could not drop and create database: @name', array('@name' => $db_name)));
  830. }
  831. }
  832. return TRUE;
  833. }
  834. /**
  835. * Return a db_spec based on supplied db_url/db_prefix options or an existing
  836. * settings.php.
  837. */
  838. function drush_sql_read_db_spec() {
  839. if ($db_url = drush_get_option('db-url')) {
  840. // We were passed a db_url. Usually a fresh site.
  841. $db_spec = drush_convert_db_from_db_url($db_url);
  842. $db_spec['db_prefix'] = drush_get_option('db-prefix');
  843. return $db_spec;
  844. }
  845. elseif (drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION)) {
  846. // We have an existing settings.php.
  847. $db_spec = _drush_sql_get_db_spec();
  848. $db_spec['db_prefix'] = $GLOBALS['db_prefix'];
  849. return $db_spec;
  850. }
  851. else {
  852. return FALSE;
  853. }
  854. }
  855. /*
  856. * Build DB connection array with superuser credentials if provided.
  857. */
  858. function drush_sql_su($db_spec) {
  859. $create_db_target = $db_spec;
  860. $create_db_target['database'] = '';
  861. $db_superuser = drush_get_option(array('db-su', 'target-db-su'));
  862. if (isset($db_superuser)) {
  863. $create_db_target['username'] = $db_superuser;
  864. }
  865. $db_su_pw = drush_get_option(array('db-su-pw', 'target-db-su-pw'));
  866. if (isset($db_su_pw)) {
  867. $create_db_target['password'] = $db_su_pw;
  868. }
  869. return $create_db_target;
  870. }
  871. /*
  872. * Build a SQL string for dropping and creating a database.
  873. *
  874. * @param array $db_spec
  875. * A database specification array.
  876. *
  877. * @param boolean $quoted
  878. * Quote the database name. Mysql uses backticks to quote which can cause problems
  879. * in a Windows shell. Set TRUE of the CREATE is not running on the bash command line.
  880. */
  881. function drush_sql_build_createdb_sql($db_spec, $quoted = FALSE) {
  882. switch (_drush_sql_get_scheme($db_spec)) {
  883. case 'mysql':
  884. $dbname = $quoted ? '`' . $db_spec['database'] . '`' : $db_spec['database'];
  885. $sql[] = sprintf('DROP DATABASE IF EXISTS %s; ', $dbname);
  886. $sql[] = sprintf('CREATE DATABASE %s /*!40100 DEFAULT CHARACTER SET utf8 */;', $dbname);
  887. $sql[] = sprintf('GRANT ALL PRIVILEGES ON %s.* TO \'%s\'@\'%s\'', $dbname, $db_spec['username'], $db_spec['host']);
  888. $sql[] = sprintf("IDENTIFIED BY '%s';", $db_spec['password']);
  889. $sql[] = 'FLUSH PRIVILEGES;';
  890. break;
  891. case 'pgsql':
  892. $dbname = $quoted ? '"' . $db_spec['database'] . '"' : $db_spec['database'];
  893. $sql[] = sprintf('drop database if exists %s;', $dbname);
  894. $sql[] = sprintf("create database %s ENCODING 'UTF8';", $dbname);
  895. break;
  896. }
  897. return implode(' ', $sql);
  898. }
  899. function drush_sql_build_exec($db_spec, $filepath) {
  900. $scheme = _drush_sql_get_scheme($db_spec);
  901. switch ($scheme) {
  902. case 'mysql':
  903. $exec = 'mysql';
  904. $exec .= _drush_sql_get_credentials($db_spec);
  905. $exec .= ' ' . drush_get_option('extra');
  906. $exec .= " < $filepath";
  907. break;
  908. case 'pgsql':
  909. $exec = 'psql -q ';
  910. $exec .= _drush_sql_get_credentials($db_spec);
  911. $exec .= ' ' . (drush_get_option('extra') ? drush_get_option('extra') : "--no-align --field-separator='\t' --pset footer=off");
  912. $exec .= " --file $filepath";
  913. break;
  914. case 'sqlite':
  915. $exec = 'sqlite3';
  916. $exec .= ' ' . drush_get_option('extra');
  917. $exec .= _drush_sql_get_credentials($db_spec);
  918. $exec .= " < $filepath";
  919. break;
  920. }
  921. return $exec;
  922. }