Update API
The Update API is Drupal's set of hooks, such as hook_update_N() and hook_post_update_NAME(), that bring existing sites' stored data up to date.
The Update API is the part of Drupal that lets a module change the data it has already stored when its code changes. A module adds an update function; Drupal notices that the function has not run yet, and runs it once, in order, when you open update.php or run drush updatedb. Sites moving from Drupal 6 or 7 use the Migrate API instead.
How it works in Drupal 11
A module needs update code when a change makes stored data on existing sites incompatible with the new code. Core names three kinds: configuration changes such as a renamed key, database schema changes, and changes to content entity or field definitions.
hook_update_N()goes inmy_module.installand is namedmy_module_update_NNNN(). The number must be higher than whateverhook_update_last_removed()returns. By convention it combines the core version, the module's major version and a two-digit counter, and the lowest number Drupal recognizes is 8001.hook_post_update_NAME()goes inmy_module.post_update.php. It runs after allhook_update_N()functions, when Drupal is fully bootstrapped, and it is skipped when the module is first installed.hook_update_dependencies()orders updates across modules,hook_update_last_removed()declares the highest number deleted from the file, andhook_removed_post_updates()does the same for post-update names.
Long jobs take a $sandbox argument. The function does one chunk of work and sets $sandbox['#finished'] to a value between 0 and 1; Drupal calls it again until it reaches 1. To report a failure, throw Drupal\Core\Utility\UpdateException.
Example
A module adds a subtitle column to its own table. Core strips the line breaks from the docblock above the function and shows the result as the description on the pending updates list.
/**
* Adds the subtitle column to the my_table table.
*/
function my_module_update_11101() {
$spec = [
'type' => 'varchar',
'description' => 'Subtitle.',
'length' => 255,
'not null' => FALSE,
];
\Drupal::database()->schema()->addField('my_table', 'subtitle', $spec);
}
The number 11101 reads as core 11, module major version 1, update 01.
Common mistakes
- Calling
hook_schema()inside an update. The schema may have changed again by the time the update runs. Copy the field definition into the function instead. - Saving entities in
hook_update_N(). Core says loading, saving or any other entity operation is never safe there, because it involves hooks and services. Usehook_post_update_NAME()for that. - Saving configuration without trusted data. Core says to pass
$has_trusted_data = TRUE, and to match the schema as it was when you wrote the update. - Rebuilding the router during an update. Core forbids it. Invalidating caches is safe.
For marketing and content teams
You will see the Update API only as a pending-updates screen or a Status report line. If the Status report shows Database updates as Out of date, a module has update functions waiting, and Drupal's own message says to run the update script immediately. Ask your developers to run them; editors should not have to.
Sources
- Update API topic, Drupal API (11.x)
- Drupal.org: Update API, writing hook_update_N() functions
- Drupal core 11.4.10: module.api.php, hook_update_N() and related hooks
- Drupal core 11.4.10: UpdateHookRegistry
- Drupal core 11.4.10: SystemRequirementsHooks, Database updates check
- Drush updatedb command
- Drush updatedb:status command