Skip to content

Instantly share code, notes, and snippets.

@Niklan
Created January 24, 2020 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Niklan/8c84df26ddc0db1c431acb9edb5b5853 to your computer and use it in GitHub Desktop.
Save Niklan/8c84df26ddc0db1c431acb9edb5b5853 to your computer and use it in GitHub Desktop.
Rerun Drupal hook_post_update_NAME()

To use it:

  1. Create file with .php extension anywhere in your project. E.g. "script.php".
  2. Change value form $post_update_name to post update name you would like to reset.
  3. Run drush scr path/to/script.php with relataive path to file where you create it.
  4. Delete your file "script.php".
<?php
/**
* @file
* Provides script to rollback post update record.
*
* This not actually rollback any changes was made in update, it's only delete
* knowledge about it from Drupal. It will allows to re-run update again.
*
* Use it only for development purposes.
*/
use Drupal\Component\Render\FormattableMarkup;
/** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value */
$key_value = \Drupal::keyValue('post_update');
$existing_updates = $key_value->get('existing_updates');
$messenger = \Drupal::messenger();
// The full post_update function name to rollback.
$post_update_name = 'hook_post_update_NAME';
if (!in_array($post_update_name, $existing_updates)) {
$messenger->addStatus(new FormattableMarkup('The post update @name was not found.', [
'@name' => $post_update_name,
]));
}
else {
if (($key = array_search($post_update_name, $existing_updates)) !== false) {
unset($existing_updates[$key]);
}
$key_value->set('existing_updates', $existing_updates);
$messenger->addStatus(new FormattableMarkup('The update @name was cleaned. You can rerun it now.', [
'@name' => $post_update_name,
]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment