sync.sql.inc

  1. 6.x commands/sql/sync.sql.inc
  2. 3.x commands/sql/sync.sql.inc
  3. 4.x commands/sql/sync.sql.inc
  4. 5.x commands/sql/sync.sql.inc

Functions

Namesort ascending Description
sql_drush_sql_sync_sanitize Sql sync sanitization function. This hook function will sanitize usernames, passwords, and sessions when the --sanitize option is used. It is also an example of how to write a database sanitizer for sql sync.
drush_sql_sync_init Sql sync init function. Bootstrap either the source or the destination site. At least one of the sites must be local for this to work; if both sites are remote, then it clearly will not be possible to bootstrap to either of them. If both are local,…
drush_sql_sync
drush_sql_post_sql_sync Apply all post-sync operations that were registered in any pre-sync hook. Follow the pattern of this function to make your own post-sync hook. If changing the database, be sure to also include a pre-sync hook to notify the user of the change that will…

File

commands/sql/sync.sql.inc
View source
  1. <?php
  2. require_once DRUSH_BASE_PATH . '/commands/core/rsync.core.inc';
  3. /**
  4. * Sql sync init function. Bootstrap either the source or the
  5. * destination site. At least one of the sites
  6. * must be local for this to work; if both sites are remote,
  7. * then it clearly will not be possible to bootstrap to
  8. * either of them. If both are local, the source site is preferred.
  9. */
  10. function drush_sql_sync_init($source = NULL, $destination = NULL) {
  11. // Preflight destination in case it defines the alias used by the source
  12. _drush_sitealias_get_record($destination);
  13. // After preflight, get source and destination settings
  14. $source_settings = drush_sitealias_get_record($source);
  15. $destination_settings = drush_sitealias_get_record($destination);
  16. // Apply command-specific options.
  17. drush_sitealias_command_default_options($source_settings, 'source-');
  18. drush_sitealias_command_default_options($destination_settings, 'target-');
  19. // Insure that we have database records for the source and destination
  20. // alias records. sitealias_get_databases_from_record will cache the
  21. // database info inside the alias records, and drush_sitealias_set_alias_context
  22. // will copy the database record into the 'alias' context. We do not
  23. // actually use the databases record at this time.
  24. sitealias_get_databases_from_record($source_settings);
  25. sitealias_get_databases_from_record($destination_settings);
  26. // Bootstrap to the source sites being sync'ed if it is local.
  27. // This allows modules enabled in the site to participate in the
  28. // sql-sync hook functions (e.g. to add sanitization operations, etc.).
  29. // If the source is remote and the destination is local, then we
  30. // will determine the sanitization operations after the database
  31. // has been copied.
  32. if (!drush_get_option('deferred-sanitization', FALSE) && drush_get_option(array('sanitize', 'destination-sanitize'), FALSE)) {
  33. $bootstrapped = drush_bootstrap_max_to_sitealias($source_settings);
  34. if ($bootstrapped) {
  35. drush_command_invoke_all('drush_sql_sync_sanitize', $source);
  36. }
  37. else {
  38. drush_set_option('deferred-sanitization', TRUE);
  39. }
  40. }
  41. // By default, sql-sync will do an ordered dump.
  42. // Set --no-ordered-dump to override.
  43. if (!drush_get_option('no-ordered-dump', FALSE)) {
  44. drush_set_option('ordered-dump', TRUE);
  45. }
  46. return TRUE;
  47. }
  48. /**
  49. * Sql sync sanitization function. This hook function will sanitize usernames,
  50. * passwords, and sessions when the --sanitize option is used. It is
  51. * also an example of how to write a database sanitizer for sql sync.
  52. *
  53. * To write your own sync hook function, define mymodule_drush_sql_sync_sanitize()
  54. * and follow the form of this function to add your own database
  55. * sanitization operations via the register post-sync op function;
  56. * @see drush_sql_register_post_sync_op(). This is the only thing that the
  57. * sync hook function needs to do; sql-sync takes care of the rest.
  58. *
  59. * The function below has a lot of logic to process user preferences and
  60. * generate the correct SQL regardless of whether Postgres, Mysql,
  61. * Drupal 6 or Drupal 7 is in use. A simpler sanitize function that
  62. * always used default values and only worked with Drupal 6 + mysql
  63. * appears in the drush.api.php. @see hook_drush_sql_sync_sanitize().
  64. */
  65. function sql_drush_sql_sync_sanitize($site) {
  66. $site_settings = drush_sitealias_get_record($site);
  67. $databases = sitealias_get_databases_from_record($site_settings);
  68. $user_table_updates = array();
  69. $message_list = array();
  70. // Sanitize passwords.
  71. $newpassword = drush_get_option(array('sanitize-password', 'destination-sanitize-password'), 'password');
  72. if ($newpassword != 'no') {
  73. $major_version = drush_drupal_major_version();
  74. $pw_op = "";
  75. // In Drupal 6, passwords are hashed via the MD5 algorithm.
  76. if ($major_version == 6) {
  77. $pw_op = "MD5('$newpassword')";
  78. }
  79. // In Drupal 7, passwords are hashed via a more complex algorithm,
  80. // available via the user_hash_password function.
  81. elseif ($major_version >= 7) {
  82. $core = DRUSH_DRUPAL_CORE;
  83. include_once $core . '/includes/password.inc';
  84. include_once $core . '/includes/bootstrap.inc';
  85. $hash = user_hash_password($newpassword);
  86. $pw_op = "'$hash'";
  87. }
  88. if (!empty($pw_op)) {
  89. $user_table_updates[] = "pass = $pw_op";
  90. $message_list[] = "passwords";
  91. }
  92. }
  93. // Sanitize email addresses.
  94. $newemail = drush_get_option(array('sanitize-email', 'destination-sanitize-email'), 'user+%uid@localhost');
  95. if ($newemail != 'no') {
  96. if (strpos($newemail, '%') !== FALSE) {
  97. // We need a different sanitization query for Postgres and Mysql.
  98. $db_driver = $databases['default']['default']['driver'];
  99. if ($db_driver == 'pgsql') {
  100. $email_map = array('%uid' => "' || uid || '", '%mail' => "' || replace(mail, '@', '_') || '", '%name' => "' || replace(name, ' ', '_') || '");
  101. $newmail = "'" . str_replace(array_keys($email_map), array_values($email_map), $newemail) . "'";
  102. }
  103. else {
  104. $email_map = array('%uid' => "', uid, '", '%mail' => "', replace(mail, '@', '_'), '", '%name' => "', replace(name, ' ', '_'), '");
  105. $newmail = "concat('" . str_replace(array_keys($email_map), array_values($email_map), $newemail) . "')";
  106. }
  107. $user_table_updates[] = "mail = $newmail, init = $newmail";
  108. }
  109. else {
  110. $user_table_updates[] = "mail = '$newemail', init = '$newmail'";
  111. }
  112. $message_list[] = 'email addresses';
  113. }
  114. if (!empty($user_table_updates)) {
  115. $sanitize_query = "UPDATE users SET " . implode(', ', $user_table_updates) . " WHERE uid > 0;";
  116. drush_sql_register_post_sync_op('user-email', dt('Reset !message in user table', array('!message' => implode(' and ', $message_list))), $sanitize_query);
  117. }
  118. // Seems quite portable (SQLite?) - http://en.wikipedia.org/wiki/Truncate_(SQL)
  119. $sql_sessions = 'TRUNCATE TABLE sessions;';
  120. drush_sql_register_post_sync_op('sessions', dt('Truncate Drupal\'s sessions table'), $sql_sessions);
  121. }
  122. function drush_sql_sync($source = NULL, $destination = NULL) {
  123. $source_settings = drush_sitealias_overlay_options(drush_sitealias_get_record($source), 'source-');
  124. $destination_settings = drush_sitealias_overlay_options(drush_sitealias_get_record($destination), 'target-');
  125. $source_os = drush_os($source_settings);
  126. $target_os = drush_os($destination_settings);
  127. // Check to see if this is an sql-sync multiple command (multiple sources and multiple destinations)
  128. $is_multiple = drush_do_multiple_command('sql-sync', $source_settings, $destination_settings);
  129. if ($is_multiple === FALSE) {
  130. // Get the options for the source and target databases
  131. $source_db_url = drush_sitealias_get_db_spec($source_settings, FALSE, 'source-');
  132. // The host may have special ssh requirements
  133. $source_remote_ssh_options = drush_sitealias_get_option($source_settings, 'ssh-options');
  134. // rsync later will also have to know this option
  135. $source_rsync_options = array('ssh-options' => $source_remote_ssh_options);
  136. $target_db_url = drush_sitealias_get_db_spec($destination_settings, FALSE, 'target-');
  137. // The host may have special ssh requirements
  138. $target_remote_ssh_options = drush_sitealias_get_option($destination_settings, 'ssh-options');
  139. // rsync later will also have to know this option
  140. $target_rsync_options = array('ssh-options' => $target_remote_ssh_options);
  141. if (empty($source_db_url)) {
  142. if (empty($source_settings)) {
  143. return drush_set_error('DRUSH_ALIAS_NOT_FOUND', dt('Error: no alias record could be found for !source', array('!source' => $source)));
  144. }
  145. return drush_set_error('DRUSH_DATABASE_NOT_FOUND', dt('Error: no database record could be found for !source', array('!source' => $source)));
  146. }
  147. if (empty($target_db_url)) {
  148. if (empty($destination_settings)) {
  149. return drush_set_error('DRUSH_ALIAS_NOT_FOUND', dt('Error: no alias record could be found for !destination', array('!destination' => $destination)));
  150. }
  151. return drush_set_error('DRUSH_DATABASE_NOT_FOUND', dt('Error: no database record could be found for !destination', array('!destination' => $destination)));
  152. }
  153. // Set up the result file and the remote file.
  154. // If the result file is not set, then create a temporary file.
  155. // If the remote file is not set, use the same name for the remote
  156. // and local files and hope for the best.
  157. $source_dump = drush_sql_dump_file($source_settings);
  158. $target_dump = drush_sql_dump_file($destination_settings);
  159. $use_temp_files = drush_get_option('temp');
  160. // Only use one dump file if both the source and the target are on the local machine
  161. if (!isset($source_db_url['remote-host']) && !isset($target_db_url['remote-host'])) {
  162. if ((!$destination_settings['dump-is-temp']) && ($source_settings['dump-is-temp'])) {
  163. $source_dump = $target_dump;
  164. $source_settings['dump-is-temp'] = FALSE;
  165. }
  166. else {
  167. $target_dump = $source_dump;
  168. $destination_settings['dump-is-temp'] = $source_settings['dump-is-temp'];
  169. }
  170. $local_file = $source_dump;
  171. }
  172. else {
  173. // If one of the systems is remote, then set the --remove-source-files
  174. // rsync option if the source dump file is temporary. This will get
  175. // rsync to clean up after us automatically; useful if the source is remote.
  176. if ($source_settings['dump-is-temp']) {
  177. $source_rsync_options['remove-source-files'] = TRUE;
  178. }
  179. // Set $local_file to whichever side of the operation is local, or make
  180. // a temporary file if both source and target are remote.
  181. if (!isset($source_db_url['remote-host'])) {
  182. $local_file = $source_dump;
  183. }
  184. elseif (!isset($target_db_url['remote-host'])) {
  185. $local_file = $target_dump;
  186. }
  187. else {
  188. $local_file = drush_tempnam($source_db_url['database'] . ($source_db_url['database'] == $target_db_url['database'] ? '' : '-to-' . $target_db_url['database']) . '.sql.');
  189. }
  190. }
  191. // If source is remote, then use ssh to dump the database and then rsync to local machine
  192. // If source is local, call drush_sql_dump to dump the database to local machine
  193. // In either case, the '--no-dump' option will cause the sql-dump step to be skipped, and
  194. // we will import from the existing local file (first using rsync to fetch it if it does not exist)
  195. //
  196. // No dump affects both local and remote sql-dumps; it prevents drush sql-sync
  197. // from calling sql-dump when the local cache file is newer than the cache threshhold
  198. // No sync affects the remote sql-dump; it will prevent drush sql-sync from
  199. // rsyncing the local sql-dump file with the remote sql-dump file.
  200. $no_sync = drush_sitealias_get_option($source_settings, 'no-sync');
  201. $no_dump = drush_sitealias_get_option($source_settings, 'no-dump');
  202. $no_cache = drush_sitealias_get_option($source_settings, 'no-cache');
  203. if (!isset($no_cache)) {
  204. $cache = drush_sitealias_get_option($source_settings, 'cache');
  205. if (!isset($cache)) {
  206. $cache = 24; // Default cache is 24 hours if nothing else is specified.
  207. }
  208. }
  209. // If the 'cache' option is set, then we will set the no-dump option iff the
  210. // target file exists and its modification date is less than "cache" hours.
  211. // If --no-sync or --no-dump are already set, then this check is unnecessary.
  212. if (isset($cache) && !isset($no_sync) && !isset($no_dump)) {
  213. if (file_exists($local_file) && (filesize($local_file) > 0)) {
  214. if ((time() - filemtime($local_file)) < ($cache * 60 * 60)) {
  215. drush_log(dt('Modification time of local dump file !file is less than !cache hours old. Use the --no-cache option to force a refresh.', array('!file' => $local_file, '!cache' => $cache)), 'warning');
  216. $no_dump = TRUE;
  217. $no_sync = TRUE;
  218. }
  219. else {
  220. drush_log(dt('Local sql cache file exists but is greater than !cache hours old.', array('!cache' => $cache)));
  221. }
  222. }
  223. else {
  224. drush_log('Local sql cache file does not exist.');
  225. }
  226. }
  227. $table_selection = array();
  228. if (!isset($no_dump)) {
  229. $table_selection = drush_sql_get_table_selection();
  230. }
  231. // Prompt for confirmation. This is destructive.
  232. if (!drush_get_context('DRUSH_SIMULATE')) {
  233. // Check to see if we are using a temporary file in a situation
  234. // where the user did not specify "--temp".
  235. if (($source_settings['dump-is-temp'] || $destination_settings['dump-is-temp']) && (!isset($use_temp_files)) && (isset($source_db_url['remote-host']) || isset($target_db_url['remote-host']))) {
  236. drush_print(dt('WARNING: Using temporary files to store and transfer sql-dump. It is recommended that you specify --source-dump and --target-dump options on the command line, or set \'%dump\' or \'%dump-dir\' in the path-aliases section of your site alias records. This facilitates fast file transfer via rsync.'));
  237. }
  238. if (array_key_exists('tables', $table_selection) && (count($table_selection['tables']) > 0)) {
  239. drush_print();
  240. drush_print(dt(' Only the following tables will be transferred: !list', array('!list' => implode(',', $table_selection['tables']))));
  241. }
  242. elseif (!empty($table_selection)) {
  243. $skip_tables_list = implode(',', $table_selection['skip'] + $table_selection['structure']);
  244. if(!empty($skip_tables_list)) {
  245. drush_print();
  246. drush_print(dt(' The following tables will be skipped: !list', array('!list' => $skip_tables_list)));
  247. }
  248. }
  249. // If there are multiple destinations, then
  250. // prompt once here and suppress the warning message
  251. // and the normal confirmation below.
  252. if (array_key_exists('site-list', $destination_settings)) {
  253. drush_print();
  254. drush_print(dt('You are about to sync the database from !source, overwriting all of the following targets:', array('!source' => $source)));
  255. foreach ($destination_settings['site-list'] as $one_destination) {
  256. drush_print(dt(' !target', array('!target' => $one_destination)));
  257. }
  258. }
  259. else {
  260. drush_print();
  261. $txt_source = (isset($source_db_url['remote-host']) ? $source_db_url['remote-host'] . '/' : '') . $source_db_url['database'];
  262. $txt_destination = (isset($target_db_url['remote-host']) ? $target_db_url['remote-host'] . '/' : '') . $target_db_url['database'];
  263. drush_print(dt("You will destroy data in !target and replace with data from !source.", array('!source' => $txt_source, '!target' => $txt_destination)));
  264. }
  265. // If any sanitization operations are to be done, then get the
  266. // sanitization messages and print them as part of the confirmation.
  267. // If --sanitize was specified but there were no sanitize messages,
  268. // then warn that sanitization operations will be accumulated and
  269. // processed after the sync completes.
  270. $messages = _drush_sql_get_post_sync_messages();
  271. if ($messages) {
  272. drush_print();
  273. drush_print($messages);
  274. }
  275. else if (drush_get_option('deferred-sanitization', FALSE) && !drush_get_option('confirm-sanitizations', FALSE)) {
  276. drush_print();
  277. drush_print("WARNING: --sanitize was specified, but deferred (e.g. the source site is remote). The sanitization operations will be determined after the database is copied to the local system and will be run without further confirmation. Run with --confirm-sanitizations to force confirmation after the sync.");
  278. }
  279. // TODO: actually make the backup if desired.
  280. drush_print();
  281. drush_print(dt("You might want to make a backup first, using the sql-dump command.\n"));
  282. if (!drush_confirm(dt('Do you really want to continue?'))) {
  283. return drush_user_abort();
  284. }
  285. }
  286. if (!isset($no_dump)) {
  287. if (isset($source_db_url['remote-host'])) {
  288. $source_remote_user = '';
  289. $source_at = '';
  290. if (array_key_exists('remote-user', $source_settings)) {
  291. $source_remote_user = $source_settings['remote-user'];
  292. $source_at ='@';
  293. $source_remote_pass = array_key_exists('remote-pass', $source_settings) ? ':' . $source_settings['remote-pass'] : '';
  294. }
  295. $source_intermediate = $source_dump;
  296. $mv_intermediate = '';
  297. // If we are doing a remote dump and the source is not a temporary file,
  298. // then first dump to a temporary file and move it to the specified file after
  299. // the dump is complete. This will reduce contention during simultaneous dumps
  300. // from different users sharing the same dump file.
  301. if (!drush_is_windows($source_os) && (!$source_settings['dump-is-temp'])) {
  302. $source_intermediate = $source_dump . '-' . date("U");
  303. $mv_intermediate = '&& mv -f ' . $source_intermediate . ' ' . $source_dump;
  304. }
  305. list($dump_exec, $dump_file) = drush_sql_build_dump_command($table_selection, $source_db_url, $source_intermediate);
  306. $dump_exec .= $mv_intermediate;
  307. if (!drush_is_windows($source_os) && isset($cache) && !$source_settings['dump-is-temp']) {
  308. // Inject some bash commands to remotely test the modification date of the target file
  309. // if the cache option is set.
  310. $dump_exec = 'if [ ! -s ' . $source_dump . ' ] || [ $((`date "+%s"`-`stat --format="%Y" ' . $source_dump . '`)) -gt ' . ($cache * 60 * 60) . ' ] ; then ' . $dump_exec . '; fi';
  311. }
  312. }
  313. else {
  314. list($dump_exec, $dump_file) = drush_sql_build_dump_command($table_selection, $source_db_url, $local_file);
  315. $no_sync = TRUE;
  316. }
  317. // Wrap the dump command in a remote call if the source site is remote.
  318. $dump_exec = _drush_backend_generate_command($source_settings, $dump_exec);
  319. }
  320. // Call sql-dump, either on the local machine or remotely via ssh, as appropriate.
  321. if (!empty($dump_exec)) {
  322. if (drush_op_system($dump_exec)) {
  323. return drush_set_error('DRUSH_SQL_DUMP_FAIL', 'Database dump failed');
  324. }
  325. }
  326. // If the sql-dump was remote, then rsync the file over to the local machine.
  327. if (!isset($no_sync)) {
  328. // If the source file is a temporary file, then we will have rsync
  329. // delete it for us (remove-source-files option set above).
  330. if (!drush_core_call_rsync($source_remote_user . $source_at . $source_db_url['remote-host'] . ':' . $source_dump, drush_correct_absolute_path_for_exec($local_file, "RSYNC"), $source_rsync_options)) {
  331. return FALSE;
  332. }
  333. }
  334. // We will handle lists of destination sites differently from
  335. // single source-to-destination syncs.
  336. if (array_key_exists('site-list', $destination_settings)) {
  337. // Insure that we will not dump the source sql database
  338. // repeatedly, but will instead re-use it each time through
  339. // the redispatch loop.
  340. drush_set_option('no-dump', TRUE);
  341. drush_set_option('no-sync', TRUE);
  342. drush_set_option('source-dump', $source_dump);
  343. // Call sql-sync for each destination to push the $source_dump
  344. // to each target in turn.
  345. foreach ($destination_settings['site-list'] as $one_destination) {
  346. drush_do_command_redispatch('sql-sync', array($source, $one_destination));
  347. }
  348. }
  349. else {
  350. // Prior to database import, we will generate a "create database" command
  351. // if the '--create-db' option was specified. Note that typically the
  352. // web server user will not have permissions to create a database; to specify
  353. // a different user to use with the create db command, the '--db-su' option
  354. // may be used.
  355. // Under postgres, "alter role username with createdb;" will give create database
  356. // permissions to the specified user if said user was not created with this right.
  357. $pre_import_commands = '';
  358. $create_db = drush_sitealias_get_option($destination_settings, 'create-db');
  359. if (isset($create_db)) {
  360. $create_db_su = drush_sql_su($target_db_url, $destination_settings);
  361. $db_su_connect = _drush_sql_connect($create_db_su);
  362. $pre_import_sql = drush_sql_build_createdb_sql($target_db_url);
  363. $pre_import_commands = sprintf('echo "%s" | %s; ', $pre_import_sql, $db_su_connect);
  364. // Linux requires quotes around echo statements. Windows generally supports
  365. // quotes for echo statements, but will complain if they are present
  366. // when piping echo statements as input to other commands. Also,
  367. // Windows multi-commands normally work when separated by '&&', '&', or ';'
  368. // but will not function with ';' in this case.(http://drupal.org/node/1957020)
  369. if (drush_is_windows($target_os)) {
  370. $pre_import_commands = sprintf('echo %s | %s && ', $pre_import_sql, $db_su_connect);
  371. }
  372. else {
  373. $pre_import_commands = sprintf('echo "%s" | %s; ', $pre_import_sql, $db_su_connect);
  374. }
  375. }
  376. // Generate the import command
  377. $import_command = _drush_sql_connect($target_db_url);
  378. switch (_drush_sql_get_scheme($target_db_url)) {
  379. case 'mysql':
  380. $import_command .= ' --silent';
  381. break;
  382. case 'pgsql':
  383. $import_command .= ' -q';
  384. break;
  385. }
  386. // If destination is remote, then use rsync to push the database, then use ssh to import the database
  387. // If destination is local, then just import the database locally
  388. if (isset($target_db_url['remote-host'])) {
  389. $target_remote_user = '';
  390. $target_at = '';
  391. if (array_key_exists('remote-user', $destination_settings)) {
  392. $target_remote_user = $destination_settings['remote-user'];
  393. $target_at ='@';
  394. $target_remote_pass = array_key_exists('remote-pass', $destination_settings) ? ':' . $destination_settings['remote-pass'] : '';
  395. }
  396. if (!drush_core_call_rsync(drush_correct_absolute_path_for_exec($local_file, "RSYNC"), $target_remote_user . $target_at . $target_db_url['remote-host'] . ':' . $target_dump, $target_rsync_options)) {
  397. return FALSE;
  398. }
  399. $import_exec = $pre_import_commands . $import_command . ' < ' . drush_escapeshellarg($target_dump, $target_os);
  400. // Delete the remote target file if it is a temporary file.
  401. if (!drush_is_windows($target_os) && $destination_settings['dump-is-temp']) {
  402. $import_exec .= '; rm -f ' . drush_escapeshellarg($target_dump, $target_os);
  403. }
  404. // TODO: make sure that the remote tmp file is deleted on remote Windows machines.
  405. }
  406. else {
  407. $import_exec = $pre_import_commands . $import_command . ' < ' . drush_escapeshellarg($local_file);
  408. }
  409. $import_exec = _drush_backend_generate_command($destination_settings, $import_exec);
  410. drush_op_system($import_exec);
  411. // After the database is imported into the destination, we
  412. // will check and see if we did not collect sanitization
  413. // operations in drush_sql_sync_init (i.e. because the source
  414. // site was remote), and if the destination site is local,
  415. // then we will call the sanitization hooks now.
  416. // This presumes an important precondition, that the code
  417. // files were sync'ed before the database was sync'ed.
  418. if (drush_get_option('deferred-sanitization', FALSE) && (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_SITE) == FALSE)) {
  419. $bootstrapped = drush_bootstrap_max_to_sitealias($destination_settings);
  420. if ($bootstrapped) {
  421. drush_command_invoke_all('drush_sql_sync_sanitize', $destination);
  422. }
  423. }
  424. }
  425. }
  426. }
  427. /**
  428. * Apply all post-sync operations that were registered in any pre-sync hook.
  429. * Follow the pattern of this function to make your own post-sync hook.
  430. * If changing the database, be sure to also include a pre-sync hook to
  431. * notify the user of the change that will be made. @see drush_sql_pre_sql_sync().
  432. */
  433. function drush_sql_post_sql_sync($source = NULL, $destination = NULL) {
  434. $options = drush_get_context('post-sync-ops');
  435. if (!empty($options)) {
  436. // If 'deferred-sanitization' is set, then we collected the
  437. // sanitization operations -after- the database sync, which
  438. // means they were not confirmed up-front. We will show the
  439. // operations here, but we will not offer an opportunity to
  440. // confirm unless --confirm-sanitizations is specified.
  441. if (drush_get_option('deferred-sanitization', FALSE) || drush_get_option('confirm-sanitizations', FALSE)) {
  442. if (!drush_get_context('DRUSH_SIMULATE')) {
  443. $messages = _drush_sql_get_post_sync_messages();
  444. if ($messages) {
  445. drush_print();
  446. drush_print($messages);
  447. if (drush_get_option('confirm-sanitizations', FALSE)) {
  448. if (!drush_confirm(dt('Do you really want to sanitize?'))) {
  449. // Do not abort or return FALSE; that would trigger a rollback.
  450. // Just skip the sanitizations and signal that all is ok.
  451. drush_log(dt('Sanitizations skipped.'), 'ok');
  452. return TRUE;
  453. }
  454. }
  455. }
  456. }
  457. }
  458. $destination_settings = drush_sitealias_get_record($destination);
  459. $sanitize_query = '';
  460. foreach($options as $id => $data) {
  461. $sanitize_query .= $data['query'] . " ";
  462. }
  463. if ($sanitize_query) {
  464. if (!drush_get_context('DRUSH_SIMULATE')) {
  465. $result = drush_invoke_process($destination_settings, "sql-query", array($sanitize_query));
  466. }
  467. else {
  468. drush_print("Executing on $destination: $sanitize_query");
  469. }
  470. }
  471. }
  472. }