Drupal - Is it possible to force your module's update hook to run?

There are few ways of forcing module update.

  1. Calling the update function directly.

    $sandbox = [];
    module_load_include('install', 'FOO');
    FOO_update_7001($sandbox);
    
  2. Resetting the schema version to the point of interest and run the updates again as usual.

    drupal_set_installed_schema_version('module_name', '7000');
    

    Or reset to re-run only the latest update schema:

    drupal_set_installed_schema_version('foo', drupal_get_installed_schema_version('foo') - 1);
    

    Notes:

    • This can be placed in hook_install, so during the update process all sequent update hooks would be executed.
    • In order to use this function outside of the installation file, you've to include Drupal install.inc first and installation file of the module, e.g.

      require_once DRUPAL_ROOT . '/includes/install.inc';
      module_load_include('install', 'foo');
      
    • Consider adding ini_set('max_execution_time', 0); for longer installation updates to prevent PHP timeouts.
  3. Using drush. Find below few examples:

    • drush eval 'module_load_include('install', 'foo'); $s = []; foo_update_7001($s);'
    • drush sqlq "UPDATE system SET schema_version = 7000 WHERE name = 'foo'" && drush -y updb

extending on comment from Jimajamma:

do a variable_set() in your update function that sets a variable when it was successfully run that you could look at inside a _preprocess_page()

and instead of checking this on every page load do it only if browsing the admin area and if the installed version is 3.0 (3.1, 3.2, kill that check if you stop supporting the old version as an upgrade path).

Additionally make use of hook_requirements to provide feedback on the status report page:

Check installation requirements and do status reporting.
(...)
The 'runtime' phase is not limited to pure installation requirements but can also be used for more general status information like maintenance tasks and security issues.

Tags:

Updating

7