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.

  • You answered yourself: You are actually using a service. If you mean you want to use an injected dependency, you cannot, from a function. – kiamlaluno Jul 27 '16 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 '16 at 11:35

You can use the global function drupal_get_installed_schema_version():

$update_hook_count = drupal_get_installed_schema_version('mymodule');
| improve this answer | |
  • Thank you!! I prefer the service approach tho. It's good to know it's using the same service. – ssibal Jul 27 '16 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. – 4k4 Jul 27 '16 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 '16 at 13:59
  • My guess is this would only happen if you run update.php. – 4k4 Jul 27 '16 at 14:04
drush sql-query "SELECT schema_version FROM system WHERE name = 'mymodule';"
| improve this answer | |
  • Probably this is the worst solution from clean and robust perspective, but it's simple. – ssibal Apr 9 '19 at 6:09
  • Was useful for debugging. – Ivan Chaer Apr 9 '19 at 9:28

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'"
| improve this answer | |

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.