3

We have a "MyModule" module, which has a mymodule.install. Inside the file, there is mymodule_update_8001, 8002 ... etc.

How can I tell programmatically that on which version the module has at the moment? I know where to find them in database (key_value table), but I would like to use a service instead, if that's possible.

The closest I found:

$update_hook_count = \Drupal::keyValue('system.schema')->get('mymodule');

This returns the update hook number.

2
  • You answered yourself: You are actually using a service. If you mean you want to use an injected dependency, you cannot, from a function.
    – apaderno
    Jul 27, 2016 at 11:13
  • Thank you! Am aware of that, I just pasted this way for the sake of simplicity. Should I separate the answer, so I could accept it.
    – ssibal
    Jul 27, 2016 at 11:35

4 Answers 4

5

You can use the global function drupal_get_installed_schema_version():

$update_hook_count = drupal_get_installed_schema_version('mymodule');
4
  • Thank you!! I prefer the service approach tho. It's good to know it's using the same service.
    – ssibal
    Jul 27, 2016 at 13:42
  • No problem, the function uses the same service. The only difference is that it stores the result in a static variable. If someone ask again about a schema version in the same request, it is then prepared to deliver a very fast answer.
    – 4uk4
    Jul 27, 2016 at 13:55
  • That's true, and cool stuff. Wondering when it could be a performance issue to ask for schema version so many times ^^
    – ssibal
    Jul 27, 2016 at 13:59
  • My guess is this would only happen if you run update.php.
    – 4uk4
    Jul 27, 2016 at 14:04
1
drush sql-query "SELECT schema_version FROM system WHERE name = 'mymodule';"
2
  • Probably this is the worst solution from clean and robust perspective, but it's simple.
    – ssibal
    Apr 9, 2019 at 6:09
  • Was useful for debugging.
    – Ivan Chaer
    Apr 9, 2019 at 9:28
1

drush php-eval "echo drupal_get_installed_schema_version('my_module');"

From: https://www.drupal.org/docs/drupal-apis/update-api/updating-database-schema-andor-data-in-drupal-8#s-useful-drush-commands-while-developing-your-hook-update-n

0

The Ivan Chaer's answer is deprecated due to database changes. Update below:

drush sql-query "SELECT CONVERT(value USING utf8) FROM key_value WHERE name = 'mymodule'"

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.