-2

I am trying to drush updb with an argument that is passed to the hook_update_N() implementation done from a module I wrote, which would use it to execute some update tasks.

function my_module_update_9001(&$value) {
  switch ($value) {
    case "unique":
      _set_unique_identifier();
      break;

    case "get":
      _get_field_typ_map();
      break;

    default:
      echo "Something went wrong...";
  }
}

function _set_unique_identifier() {
  _get_database_shame_connector()->uniqueIdentifier();
}
    
function _get_field_typ_map() {
  _get_database_shame_connector()->getFieldTypeMap();
}

How do I tell drush updb to pass an argument to an hook_update_N() implementation?

6
  • Not really clear what you're trying to do/want to do.
    – Jaypan
    Commented Jun 24, 2022 at 16:35
  • 1
    Update hooks take $sandbox argument which contains information about the running batch. api.drupal.org/api/drupal/… - i.e. not a callable function you can invoke whenever, and will only run once. updb is not for this type of data manipulation. You need to write an actual Drush command that accepts arguments to build this out.
    – Kevin
    Commented Jun 24, 2022 at 16:53
  • 1
    Agreed. As written, this is impossible.
    – mona lisa
    Commented Jun 24, 2022 at 17:31
  • It's also not possible because drush updb doesn't accept such parameter; it only accepts four options.
    – avpaderno
    Commented Jun 24, 2022 at 17:58
  • Updb is the wrong direction. Create your own custom Drush command.
    – leymannx
    Commented Jun 25, 2022 at 1:02

1 Answer 1

1

It's not possible because drush updb doesn't accept a parameter that is passed to hook_update_N() implementations, which makes sense, since the only parameter accepted from hook_update_N() is an array that allows that hook to use batch operations to run its updates. Passing a different argument to hook_update_N() implementations would cause issues with those implementations that use the batch API to run updates.

Even in the case drush updb would accept an argument to pass to an update hook and the function name for a hook_update_N() implementation, that implementation should be able to understand whether it's called by Drush with a different argument or by Drupal core with the expected argument.

There is a simpler way to execute code that runs after drush updb: Implement a post-command hook, as described on How to invoke a post drush command? for example.

namespace Drush\Commands;

class PostUpdateCommands extends DrushCommands {

  /**
   * Executes commands right after `drush updatedb`.
   *
   * @hook post-command updatedb
   *
   */
  public function postUpdate() {
    // Your code
  }

} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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