Drush aliases - automatically enable development modules

Intermediate drush tutorial - Part 4

hand of time image

Web developers love automation. Why repeat error prone to-do lists when a short piece of code can replicate the task without fail? When copying over our production database we can automatically enable our development modules. This code combined with adding some configuration variables to the settings.php file, allows us to start developing immediately.

Drush kindly includes a number of example scripts that we can utilize. Firstly we need to find them contained in the drush app package. In your terminal you can run 'which drush' to find the path. You can further check with 'ls -la' if the result is a symlink. Open the examples folder in the drush directory and find the sync_enable.drush.inc file.

As with the policy.drush.inc file copy the sync_enable.drush.inc file to your $HOME/.drush directory. This will get the ball rolling. Now you can include the modules you want to enable to the YOUR_ALIAS.aliases.drushrc.php you created earlier.



<?php

//Add the modules you want to enable
      'enable' => array(
        'update',
        'dblog',
        'admin_menu',
        'admin_devel',
        'module_filter',
        'devel',
        'diff',
        'features_diff',
        'rules_admin',
        'field_ui',
        'schemaorg_ui',
        'variable_admin',
        'views_ui',
      ),
//Add the modules you want to disable
      'disable' => array(
        'syslog',
      ),
?>

The completed alias file should look like the one below.



<?php

$options = array(
  'target-command-specific' => array(
    'sql-sync' => array(
      //Create a fresh db not a merge.
      'create-db' => TRUE,
      //Sanitizing is useful, especially if you automate email campaigns.
      //You probably don't want to email your site users from a local machine
      //when testing.
      'sanitize' => true,
      'confirm-sanitizations' => TRUE,
      //%uid is the users id
      'sanitize-email' => 'user%uid@local',
      'sanitize-password' => 'pass',
      //A comma-separated list of tables to include for structure, but not data.
      //Include 'watchdog' if you have dlog on production. 
      //* is a wildcard so cache_* removes all cache data
      'structure-tables-list' => 'history,sessions,cache,cache_*',

      //Add the modules you want to enable
      'enable' => array(
        'update',
        'dblog',
        'admin_menu',
        'admin_devel',
        'module_filter',
        'devel',
        'diff',
        'features_diff',
        'rules_admin',
        'field_ui',
        'schemaorg_ui',
        'variable_admin',
        'views_ui',
      ),
       //Add the modules you want to disable
      'disable' => array(
        'syslog',
      ),
    ),
  ),
);

//local alias - details of you local server / machine
$aliases['local'] = array(
  'uri' => 'http://example.local',
  'root' => '/Users/YOUR_USERNAME/Sites/example.local',
  'path-aliases' => array(
    '%dump-dir' => '/tmp',
    '%files' => '/Users/YOUR_USERNAME/Sites/example.local/sites/default/files',
    '%drush-script' => 'PATH/TO/drush.php',  //example -> /Users/YOUR_USERNAME/.composer/vendor/bin/drush.php
  )
) +$options;
    
//production alias - details of you remote server 
$aliases['com'] = array(
  'remote-host' => 'SERVER_IP_ADDRESS',
  'remote-user' => 'USERNAME_ON_SERVER', // example -> www-data
  'root' => 'SITE ROOT', // example -> /var/www/html/example.com/public_html'
  'uri' => 'https://example.com',
  'os' => 'Linux',
  //If you use keys for ssh, add path to you ssh private key (on your local machine) 
  'ssh-options' => '-o PasswordAuthentication=no -i /home/YOUR_USERNAME/.ssh/id_rsa',
  'path-aliases' => array(
    '%dump-dir' => '/tmp',
    '%files' => '/var/www/html/example.com/public_html/sites/default/files',
    '%drush-script' => 'PATH/TO/drush.php', //example -> /home/YOUR_USERNAME/.config/composer/vendor/bin/drush.php
  )
  'php' => '/usr/bin/php', // PATH/TO/YOUR/PHP
  'php-options' => '-d error_reporting="E_ALL^E_DEPRECATED"',
);
?>

If you want page caching disabled and error reporting enabled you can add the following lines to the local settings.php file.


	
<?php
$conf['cache'] = FALSE;
$conf['preprocess_css'] = FALSE;
$conf['preprocess_js'] = FALSE;
$conf['page_compression'] = FALSE;
$conf['error_level'] = 2;
?>

Now when you run the command 'drush sql-sync @example.com @example.local' your local database is replaced by the production database, your development modules are enabled with caching and error reporting returned to the development state. Since you can repeat this process ad nauseum, you can save time with the Sisyphean nightmare that is website development.