5

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
    Commented 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
    Commented Jul 27, 2016 at 11:35

6 Answers 6

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
    Commented 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
    Commented 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
    Commented Jul 27, 2016 at 13:59
  • My guess is this would only happen if you run update.php.
    – 4uk4
    Commented 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
    Commented Apr 9, 2019 at 6:09
  • Was useful for debugging.
    – Ivan Chaer
    Commented 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

1

I came here to find a version of module but this question is actually about getting module's database schema version. So here is a way to get only module version:

$version = \Drupal::service("extension.list.module")->getExtensionInfo('mymodule')['version'] ?? 0;

And if you need later compare version:

$affectedVersion = version_compare($version, '1.3.0', '>=');
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'"
0

Drupal versions 9 and above would work with \Drupal::service('update.update_hook_registry')->getInstalledVersion('MODULE_NAME');.

See this for more information.

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.