Skip to content

Instantly share code, notes, and snippets.

@e0ipso
Created November 29, 2013 09:01
Show Gist options
  • Save e0ipso/7703162 to your computer and use it in GitHub Desktop.
Save e0ipso/7703162 to your computer and use it in GitHub Desktop.
Drush command to update your local environment syncing down the DB from a remote environment, running updates and reverting all features.
<?php
/**
* Implementation of hook_drush_command().
*/
function operations_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.',
'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 @aliasname.acc --dump-dir=~/.drush/dumps' => 'Update your local from the selected source alias and store possible database dumps in ~/.drush/dumps.',
'drush lu @aliasname.acc --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_COMMANDFILE_COMMANDNAME().
*/
function drush_operations_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['env'])) {
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();
}
$today = new \DateTime();
$sync_settings = array(
'target-dump' => rtrim(drush_get_option('dump-dir'), '/') . '/' . preg_replace('/[^A-Za-z0-9]/', '', $source) . '.' . $today->format('Y-m-d') . '.sql',
'source-dump' => '/mnt/tmp/' . 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;
}
try {
// Clean install $source's one.
drush_log(dt('(Downloading and) importing database dump...'), 'success');
$status = drush_invoke_process('@self', 'sql-sync', array($source, '@self'), array(
'yes' => 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...'), 'success');
$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('cache-clear', array('drush'));
// Revert all features.
drush_log(dt('Reverting features...'), 'success');
$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.');
}
$status = drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
if (!$status) {
throw new Exception('Process failed while bootstrapping Drupal.');
}
drush_invoke('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('user-login', array(1, 'admin'), array('browser' => 0));
operations_notify(dt('Local update completed'));
}
catch (Exception $e) {
drush_set_error(dt($e->getMessage()));
}
}
/**
* Helper function to deliver a notification.
*/
function operations_deliver_notification($message) {
if (function_exists('drush_notify_send_audio')) {
drush_notify_send_audio($message);
}
else if (function_exists('drush_notify_send')) {
drush_notify_send($message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment