DEV Community

Claudio Alese
Claudio Alese

Posted on

Drush, a useful insight

Taking a cue from a project I recently did for a client, today I'm talking about a very useful tool for Drupal 8: Drush.

What's Drush?

From official guide

“Drush is a command line shell and Unix scripting interface for Drupal.
Drush core ships with lots of useful commands for interacting with code like modules/themes/profiles. Similarly, it runs update.php, executes sql queries and DB migrations, and misc utilities like run cron or clear cache.”

I had some problems installing it, I chose to install it via composer, then importing it as a project dependency into composer.json (in root of our Drupal folder installation)
Drupal Composer

Then run

composer install

Now in vendor directory have drush directory like this
Drush into Vendor

Drush will be executable run

./vendor/bin/drush

The importance of the cex and cim commands

How many times have you had to manually carry over changes to some configurations, for example bringing some new developments from a test environment to production?
Frustrating, right?

Drush comes to our rescue by introducing two very useful commands, cex and cim.

Whenever we make a change to the site settings, such as:

  • creation / modification / deletion of content types
  • creation / modification / deletion of a user role
  • creation / modification / deletion of a new language
  • enabling / disabling modules
  • tons of other configurations

To enjoy all the potential of the two commands, however, it is necessary to set a new setting in the settings.php file, here an example:

$settings['config_sync_directory'] = 'sites/default/config';

where sites/default/config will be the path to the folder where the configuration files will be placed.

Run

./vendor/bin/drush cex

there will be many new files in the yml format that correspond to all the exported configurations (if you have already done other exports, the exported files will be only the updated files), below an example:
Drush CEX

Synchronizing these files in the folder of other environment (for example, in production) and after run

./vendor/bin/drush cim

and we will have the configurations aligned between different environments.

Very useful, right?

Top comments (0)