Drupal 8: Set and get module version via Drush

Submitted by Peter Majmesku on Wed, 05/09/2018 - 09:19

Sometimes you want to know, which version of a certain Drupal 8 module is installed on your system. Then just execute via command line (you must have Drush installed):

drush ev "var_dump(\Drupal::keyValue('system.schema')->get('my_nice_module'))";

This will show you an output like

/Users/peter/Dev/project/vendor/drush/drush/src/Commands/core/PhpCommands.php(28) : eval()'d code:1:

So module version 8.0 is installed on your system. This is very good for update hooks! Because now you must create an update hook with the name "my_nice_module_update_8001()" to be applied via the "drush updb" command. 

Setting the module version can be done via the following command:

drush ev "\Drupal::keyValue('system.schema')->set('my_nice_module', (int) 8149)";

This can be helpful for workarounds, if you want to bypass "buggy" update hooks or just update hooks, which you do not want to apply.

You can check the KeyValueFactory PHP class to learn more about the inner workings of the Drush command (file location is "docroot/core/lib/Drupal/Core/KeyValueStore/KeyValueFactory.php").