Manage Drupal Feature Updates

When using Features module to move changes from one  development environment to the next, it can be tricky to track which Feature ought to be 'reverted' in order make the new code be incontrol.  Sure you can revert them all in one shot with `drush features-revert-all`, but that is pretty intensive to do especially on a production site and can lead to some race conditions.

One nice way to keep it within the 'Drupal Way' and give you more fine grained control and awareness of what reversion  is being applied is to put the revert in a hook_update_N() in a feature_name.install file within the Feature.  This has a couple of nice results:

  1. You can see what Features need to be reverted by visiting upate.php
  2. The comment on the hook_update_n() indicates what the change in the Feature was that is going to get applied.
  3. Sync the N in hook_update_N with the version number of your Feature to make even more information available on update.php.
  4. No waiting around for all Features to be reverted.  This way you only revert the ones you want.

How to create hook_update_N in Feature

Here are the steps to accomplish this.

  1. Make any changes needed to your Feature.
  2. Recreate the Feature in the admin or run `drush features-update your_feature_name`
  3. Open up the .info file and increment the version number (make note of it).
    example: version = 7.x-1.2
  4. Add a file your_feature_name.install if it does not already have one.
  5. Add an instance of hook_update_N to the .install

The code would look like this if the version number was 7.x-1.2:

<?php
 
/**
 * Add date field to content type Foo.
 */
function your_feature_name_update_7102(&$sandbox) {
 
features_revert_module('your_feature_name');
}
?>

This will cause an update to be listed on update.php that says 'Add date field to content type Foo.' Run the update and your module is now reverted.

modules:

Comments

swirt's picture

After doing a bunch of these, I built a better solution. A set of tools for handling feature deployment through update hooks.
https://www.drupal.org/project/hook_update_deploy_tools