1

I've implemented hook_post_update_NAME to bulk import some files into Drupal (creating file and media entities for them). This hook runs correctly when I use update.php. But it seems to silently fail via drush updb.

My update hooks looks like this

/**
 * Creates SVG icons as media entities.
 */
function my_module_default_content_post_update_create_icons() {

  $module_handler = \Drupal::service('module_handler');
  $module_path = $module_handler->getModule('my_module_default_content')->getPath();

  \Drupal::logger('my_module_default_content')->notice('Importing icons from @path', ['@path' => $module_path]);

  $dir = new DirectoryIterator("$module_path/icons");

  \Drupal::logger('my_module_default_content')->notice('Directory contains @count items', ['@count', count($dir)]);

  foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {

      \Drupal::logger('my_module_default_content')->notice('Importing file @file', ['@file' => $fileinfo->getFilename()]);

      my_module_default_content_create_icon($fileinfo->getPathname());
    }
  }
}

This works fine via update.php. All the files are copied, relevant entities created, and messages logged to watchdog. When running drush updb I only get

[ok] Update completed: my_module_default_content_post_update_create_icons

[success] Finished performing updates.

None of my log messages are output (while using --debug option) to the screen or to watchdog.

My initial thought was a permissions issue, but if it's not outputting any of my log messages. I'm not positive the function is even being run. Adding in a die() on the first line of the function gave the exact same output/outcome.

Any ideas on what could cause this to silently fail when running via Drush?

4
  • I haven't done much development for D8, but I wonder: Where does \Drupal::logger('my_module_default_content')->notice() output the notice? I expected it to go to the normal Drupal log / recent log messages. Drush doesn't display those (and --debug isn't connected to \Drupal::logger logging). Have you checked recent log messages after running drush updb? – hansfn Mar 18 '19 at 9:06
  • The messages do output to watchdog. Was able to confirm that when running it via update.php. No messages when running it via drush updb. – Dracs Mar 18 '19 at 11:30
  • Another obvious comment: You are resetting the Drupal environment completely? I wonder because - from the docs: "Drupal also ensures to not execute the same hook_post_update_NAME() function twice." That could explain the lack of messages. – hansfn Mar 18 '19 at 12:25
  • I was doing full DB drop and re-import between attempts. I've since re-worked the update hook to use the batch API to prevent timeout issues and run it via update.php. I suppose the question is academic at this point. – Dracs Mar 19 '19 at 4:57
0

For anyone else who runs into this, make sure that the post-update hooks are defined in a MODULE_NAME.post_update.php file. It seems as though update.php will happily invoke post-update hooks defined in the MODULE_NAME.install file, while Drush will not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.