Forked from wayne-weibel/reset_hook_post_update.php
Last active
September 28, 2026 18:21
-
-
Save robme/0de6632a63fde2fa970db20d2161b890 to your computer and use it in GitHub Desktop.
Drupal 9.x Drush 10.x PHP Script to Reset Post Update Hooks, see https://drupal.stackexchange.com/q/238682/11788
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Drush\Commands\DrushCommands; | |
| /** | |
| * @file | |
| * Reset module hook_post_update_NAME | |
| * | |
| * A tool for developing and debugging post_update hooks. | |
| * | |
| * This script will ONLY reset the hook to be run again ... | |
| * | |
| * If data is being changed, backup the database and restore as needed | |
| * | |
| * Usage: | |
| * drush php:script reset_hook_post_update.php [-- PREFIX] | |
| * | |
| * @see https://www.drush.org/latest/commands/php_script/ | |
| * | |
| * https://gist.github.com/bryanbraun/852646078ef6b33d2dc2ecacc96c9865 | |
| * https://www.drush.org/latest/api/Drush/Commands/DrushCommands.html | |
| */ | |
| $key_value = \Drupal::keyValue('post_update'); | |
| $update_list = $key_value->get('existing_updates'); | |
| $indexes = array_flip($update_list); | |
| $choices = array_reverse($update_list); | |
| if ($extra) { | |
| // The `$extra` variable is added by Drush when running php:script | |
| $prefix = array_shift($extra); | |
| $choices = array_filter( | |
| $choices, | |
| fn($c) => str_starts_with($c, $prefix) | |
| ); | |
| } | |
| $io = DrushCommands::io(); | |
| $reset = $io->choice("Which post_update hook do you want to reset?", $choices, FALSE); | |
| if ($reset !== FALSE) { | |
| if (isset($indexes[$reset])) { | |
| unset($update_list[$indexes[$reset]]); | |
| $key_value->set("existing_updates", $update_list); | |
| $io->success("$reset has been reset."); | |
| } | |
| else { | |
| // Really should not even get here! | |
| $io->error("Unable to match selection!"); | |
| } | |
| } | |
| // Cancelled message is handled in `choice`. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment