Problem/Motivation

Currently all the post_update methods are run on alphabetical order (see UpdateRegistry::getAvailableUpdateFunctions).
The documentation suggests adding numerical prefix the update names or making them completely numerical in order to control the order of execution. However, this does not take into account that post_update methods can come from different components which makes controlling the order harder.

In contrast, update methods can define requirements to one another which ensures that the order in which they run is consistent.

Proposed resolution

We could either implement a more complex requirement based solution for post_updates (like we do with updates) or we could add an alter hook that allows to alter the final order of execution.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3129231

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ieguskiza created an issue. See original summary.

ieguskiza’s picture

StatusFileSize
new5.26 KB

I gave a go to a simple alter hook and included a small test that shows what it would allow to do.

upchuk’s picture

Status: Active » Needs work
  1. +++ b/core/lib/Drupal/Core/Update/UpdateRegistry.php
    @@ -130,6 +130,39 @@ protected function getAvailableUpdateFunctions() {
    +   * This function is similar to \Drupal::moduleHandler()->invokeAll(), with the
    

    This comment is outdated. There are no enabled/disabled modules anymore.

    Also, the function below doesn't do that. It loops through the extension list and it simply skips the uninstalled ones.

  2. +++ b/core/lib/Drupal/Core/Update/UpdateRegistry.php
    @@ -130,6 +130,39 @@ protected function getAvailableUpdateFunctions() {
    +   * @return callable[]
    

    It doesn't return anything.

  3. +++ b/core/lib/Drupal/Core/Update/UpdateRegistry.php
    @@ -130,6 +130,39 @@ protected function getAvailableUpdateFunctions() {
    +   * @see hook_post_updates_alter()
    

    See where? We should add documentation to the hook in the module.api.php file next to the others.

  4. +++ b/core/lib/Drupal/Core/Update/UpdateRegistry.php
    @@ -130,6 +130,39 @@ protected function getAvailableUpdateFunctions() {
    +  function alterAvailableUpdateFunctions($updates) {
    

    Missing protected.

  5. +++ b/core/lib/Drupal/Core/Update/UpdateRegistry.php
    @@ -130,6 +130,39 @@ protected function getAvailableUpdateFunctions() {
    +    // Get a list of installed modules, arranged so that we invoke their hooks in
    

    I would rephrase this comment to something a bit more relevant to what the code does below, if needed at all. Not to mention that you are loading all extensions, not just modules :)

  6. +++ b/core/modules/system/tests/modules/update_test_postupdate/update_test_postupdate.post_update.php
    @@ -79,3 +90,16 @@ function update_test_postupdate_removed_post_updates() {
    +  $updates[] = 'update_test_postupdate_post_update_last';
    

    Can we get a patch that contains the failing test that proves this actually works (the fact that the post update function goes to the end from another location)?

Please be aware that the tests are also failing.

ieguskiza’s picture

StatusFileSize
new6.29 KB
new3.59 KB
new3.95 KB

1. I removed the extra comment, as you mention it doesn't add anything of value.

2. The method does return a list of callable strings, I kept the documentation.

3. Good point, I added the entry on module.api.php.

4. Good point, fixed.

5. I removed most of the comment. However reading through drupal_get_installed_schema_version it does appear to only return module information, so it would still be correct.

6. I added an extra patch with only the tests.

ieguskiza’s picture

StatusFileSize
new5.84 KB
new1.05 KB

Unit tests failed because we use a global function in the updateregistry class. I am not sure if we even need to include the install files since we can rely on the post_update files entirely so I removed that part for now.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ieguskiza’s picture

StatusFileSize
new5.96 KB

Rerolling patch for 8.9

upchuk’s picture

Status: Needs work » Reviewed & tested by the community

Looks good to me. We have been using this for quite some time now.

quietone’s picture

Status: Reviewed & tested by the community » Needs work

Thank you for getting this to RTBC.

Doing a 'light' review to make sure this ready for a committer. I found a few things that should be addressed so setting NW.

  • The proposed resolution in the Issue Summary show two options but does not indicate which one is implemented and why.
  • Testbot shows that there are coding standard errors.
  • Shouldn't this be on 9.2.x?
ravi.shankar’s picture

StatusFileSize
new5.87 KB
new475 bytes

Fixed CS issues of patch #7.

chewie’s picture

StatusFileSize
new5.9 KB

Rerolled patch for 9.1.x

upchuk’s picture

Status: Needs work » Needs review
StatusFileSize
new6.02 KB

Rerolled the patch for 9.2.x

upchuk’s picture

StatusFileSize
new6.02 KB

Fixing phpcs issue.

Status: Needs review » Needs work

The last submitted patch, 13: 3129231-13.patch, failed testing. View results

upchuk’s picture

Status: Needs work » Needs review
StatusFileSize
new6.02 KB

Fixed the test.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
longwave’s picture

In #3262036: Convert hook_update_dependencies() to PHP attributes I propose using PHP attributes instead of a separate hook to determine hook_update_N() dependencies.

Perhaps we could do the same here, although this would mean this was PHP 8 and 10.0.x only.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sinn’s picture

StatusFileSize
new6.02 KB
new660 bytes

Rerolled patch for 9.4.x

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new138 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joevagyok made their first commit to this issue’s fork.

joevagyok’s picture

StatusFileSize
new6.01 KB

Rerolled over 11.x.

joevagyok’s picture

StatusFileSize
new6 KB

Patch for 10.5.x version of core.

nicxvan’s picture

I'll be honest, I have strong reservations about this.

We do have a way to order them already just by numbering them.

This looks suspiciously like hook module implements alter that was a monumental effort to replace.

Can we get a description of the scenarios where ordering is necessary here and why alpha numeric isn't sufficient?

I'd also want to review hook update dependencies in depth to see if that can be applied.

joevagyok’s picture

StatusFileSize
new6.45 KB

In the patch made for 10.5.x in comment #27 I realized that one function call is missing and the feature doesn't work as expected. Uploading the correct patch.

joevagyok’s picture

joevagyok’s picture

@nicxvan Our use case that requires this is the following:
We have an install profile and we need to make sure, that all dependent components/modules run their post_update functions before the install profile post updates, hence why the existing solutions are not enough, like it is described in the issue description.

joevagyok’s picture

StatusFileSize
new5.99 KB

Fixed the patch for 10.5.x.

joevagyok’s picture

StatusFileSize
new6.37 KB

Added the missing part in the 10.5.x patch.

    public function getPendingUpdateFunctions() { ...
    return $this->alterAvailableUpdateFunctions($not_executed_update_functions);
hernani’s picture

Version: 11.x-dev » 11.2.x-dev
StatusFileSize
new6.36 KB

Patch for 11.2.x

quietone’s picture

Version: 11.2.x-dev » 11.x-dev
Issue summary: View changes

Hi, in Drupal core changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies. Thanks.

quietone’s picture

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.