Using Drupal Automatic Updates with patches

Submitted by Darren Oh on
National park patches spread out on a table with a map, passport, and camera.

Automatic Updates is a module included in Drupal CMS that keeps code up to date. To do that, it copies just the files required for an update to a temporary directory and runs the update there. If there are no problems, it copies the update back to the Drupal site. The problem we ran into on Drupal Forge was that Automatic Updates did not copy patches we applied to improve the user experience. During automatic updates, our patches were not applied. Most projects solve this by configuring automatic updates to copy everything. But that is not a great solution for sites that have limited disk space, like Drupal Forge demos.

For a while we solved this with a patch that let us specify extra files that are required for updates. You can check out this solution in Drupal core issue #3531174. But last week I found a way to specify extra files that did not require a patch.

The trick uses the Drupal Composer Scaffold plugin. Usually this plugin is used to copy files from a Composer package into a different location. It lets us define mappings that tell Composer where to copy these files. Automatic Updates treats any file listed in the mappings as required for updates.1 You can add a mapping for any file, even a file that is not copied from a Composer package. (You cannot add a mapping for a directory; but, if you map a file within the directory, the whole directory will be copied.) Here is what we add to composer.json to make automatic updates include patches.json, patches.lock.json, and the patches directory:

    "extra": {
        "drupal-scaffold": {
            "file-mapping": {
                "patches.json": false,
                "patches.lock.json": false,
                "patches/drupal/drupal_cms/373.patch": false
            }
        }
    }

false tells Drupal Composer Scaffold to leave a file alone. Automatic Updates will still copy it.

With this trick, sites no longer need an extra patch to copy just the files they need for automatic updates.


References

  1. ^ UnknownPathExcluder::getExcludedPaths()

Photo by Josh Carter on Unsplash