Last active
August 29, 2015 14:07
-
-
Save e0ipso/828924ad59bebf4a6dce to your computer and use it in GitHub Desktop.
Drupal local update. Update your local database based on a remote environment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implementation of hook_drush_command(). | |
*/ | |
function local_update_drush_command() { | |
$items = array(); | |
$items['local-update'] = array( | |
'description' => 'Downloads a dump of the remote database, suffixes that file with the current date and stores it in the selected location. After importing the database, it will run update hooks, revert all features, and clear cache. Once all this is finished, if everything is installed, the bundle command to rebuild all css will be run.', | |
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, | |
'arguments' => array( | |
'source' => 'Alias where to download from.', | |
), | |
'options' => array( | |
'dump-dir' => array( | |
'description' => 'Path to the folder where to store the database dumps and to read from.', | |
'example' => '~/.drush/dumps', | |
'required' => TRUE, | |
), | |
'force-download' => array( | |
'description' => 'By default only one database dump will be downloaded every 24h. Use this flag to force a re-download.', | |
), | |
), | |
'aliases' => array('lu'), | |
'examples' => array( | |
'drush lu @example.stage --dump-dir=~/.drush/dumps' => 'Update your local from the selected source alias and store possible database dumps in ~/.drush/dumps.', | |
'drush lu @example.stage --force-download --dump-dir=~/.drush/dumps' => 'Update your local from the selected source alias and store database dumps in ~/.drush/dumps. This will trigger a download and overwrite any possible dumps for the current date.', | |
), | |
); | |
return $items; | |
} | |
/** | |
* Implements drush_COMMANDNAME(). | |
*/ | |
function drush_local_update($source = NULL) { | |
// Process the arguments and prepare options. | |
if (empty($source)) { | |
return drush_set_error('DRUSH_ALIAS_NOT_FOUND', dt('Error: no alias record could be found.')); | |
} | |
$source_settings = drush_sitealias_overlay_options(drush_sitealias_get_record($source), 'source-'); | |
if (empty($source_settings)) { | |
return drush_set_error('DRUSH_ALIAS_NOT_FOUND', dt('Error: no alias record could be found for !source.', array('!source' => $source))); | |
} | |
if (empty($source_settings['root'])) { | |
return drush_set_error('DRUSH_ALIAS_NOT_FOUND', dt('Error: Please choose any of the available environments for this site.' . PHP_EOL . ' - !sites', array('!sites' => implode(PHP_EOL . ' - ', $source_settings['site-list'])))); | |
} | |
// Confirm the user wants to do this! | |
if (!drush_confirm(dt("You are about to replace your local database with !source. Do you want to continue?", array('!source' => $source)))) { | |
return drush_user_abort(); | |
} | |
try { | |
$status = drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_FULL); | |
if (!$status) { | |
throw new Exception('Process failed while bootstrapping Drupal.'); | |
} | |
$today = new \DateTime(); | |
// Get the temporary path in the remote environment. | |
$sync_settings = array( | |
'target-dump' => rtrim(drush_get_option('dump-dir'), '/') . '/' . preg_replace('/[^A-Za-z0-9]/', '', $source) . '.' . $today->format('Y-m-d') . '.sql', | |
'create-db' => TRUE, // local database will be created (or dropped if exists) | |
); | |
// Check for the force-download flag. | |
if (!drush_get_option('force-download')) { | |
$sync_settings['cache'] = TRUE; | |
} | |
// Clean install $source's one. | |
drush_log(dt('(Downloading and) importing database dump...'), 'notice'); | |
$status = drush_invoke_process('@self', 'sql-sync', array($source, '@self'), array( | |
'yes' => TRUE, | |
'sanitize' => TRUE, | |
) + $sync_settings); | |
if (!$status) { | |
throw new Exception('Process failed while syncing with the development environment\'s database.'); | |
} | |
// Run database updates. | |
drush_log(dt('Running updates...'), 'notice'); | |
$status = drush_invoke_process('@self', 'updatedb', array(), array('yes' => TRUE)); | |
if (!$status) { | |
throw new Exception('Process failed while running database updates.'); | |
} | |
// Might be unecessary, but 'features-revert-all' was not working for me without: | |
drush_invoke_process('@self', 'cache-clear', array('drush')); | |
// Revert all features. | |
drush_log(dt('Reverting features...'), 'notice'); | |
$status = drush_invoke_process('@self', 'features-revert-all', array(), array( | |
'yes' => TRUE, | |
'force' => TRUE, | |
)); | |
if (!$status) { | |
throw new Exception('Process failed while reverting all features.'); | |
} | |
drush_invoke_process('@self', 'cache-clear', array('all')); | |
drush_log(dt('You are up to date!'), 'success'); | |
drush_log(dt('Copy and paste this URL in your browser to log in as an admin (you\'ll need to adapt the hostname if you don\'t have it configured).'), 'success'); | |
drush_invoke_process('@self', 'user-login', array(1, 'admin'), array('browser' => 0)); | |
} | |
catch (Exception $e) { | |
drush_set_error(dt($e->getMessage())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment