Running Partial Updates in Drupal 8

January 20, 2016

During active development of a Drupal application, it is common to require changes to configuration, the database structure, or similar elements.

The standard method for this is the hook_update_N. Drupal 8 is no exception, and the documented way to apply these changes is by running the http://example.com/update.php script.

The Drupal Console project can execute these update functions, but it also includes options that provide significant flexibility for your update process.

Consider a scenario with two modules named `example` and `samples`, each containing several update functions, as shown in the following code snippets.

First, review the implementation of the `example` and `samples` modules.

[gist-embed data-gist-id="98b75e9689613bc54d5c" data-gist-file="example.php"]

[gist-embed data-gist-id="98b75e9689613bc54d5c" data-gist-file="samples.php"]

Using Drupal Console, it is possible to determine which updates are pending on a system with the update:debug command, as shown in the following image.

Update debugSo far, this is standard functionality. Now, let's examine a more interesting case.

Suppose you need to run update 8001 for the `samples` module. However, update function 8002 for the `example` module contains a fatal PHP error. In a real-world situation, the `samples` 8001 update is critical for production, but the failing `example` update is not. This circumstance might seem to require desperate measures.

There is no need for drastic actions like attempting to modify core. The options within the update:execute command can resolve this problem, as demonstrated in the image below.

The command to run would be: $ drupal update:execute samples 8001

Update execute by module and function

This precisely executes the required update without running the one that contains an error.

For another scenario, consider the `example` module with its three update functions. Imagine a situation where the last function should not be executed yet.

Following the same logic, one might run the command $ drupal update:execute example 8002

One might expect only that specific update to run, but Drupal Console provides a safeguard. Executing update functions out of sequence can cause data corruption and other serious issues.

For this reason, the update:execute command will process the request by running any pending prerequisite updates before executing the specified function. This behavior is shown in the image below.

Update execute with limit

As you can infer, providing only the module name will execute all of its pending updates. Likewise, if no module is specified, all pending updates for the site will be executed.

This feature offers fine-grained control over the update process.