33

I'm working on a module, and I changed a couple field types in the schema. I didn't take a backup before testing it and messed up the DB (it's just a dev site). Anyway, I reinstalled everything, and the schema version is set to that of the highest update. However, I actually do need to run the update; a View I have wouldn't install if I had the updated schema in the module code, so I figured this would be a good way to test that update.

How do I reset the schema version of my module to a particular version? In Drupal 7, I would have done this:

drush sqlq "UPDATE system SET schema_version=8102 WHERE name='flllpdf' AND type='module'"

4 Answers 4

75

While accessing state directly works, you can also just use the API: drupal_set_installed_schema_version(), which has existed since 4.7, as visible on the linked documentation page. This function is deprecated in Drupal 9.3.0 and removed in 10.0.0, where you should use \Drupal\Core\Update\UpdateHookRegistry::setInstalledVersion() instead.

To set it from drush:

drush ev "drupal_set_installed_schema_version('fillpdf', 8012)"

Note: In Drupal 7 you need to add extra include 'includes/install.inc';.

or (Drupal >= 9.3):

drush ev "\Drupal::service('update.update_hook_registry')->setInstalledVersion('fillpdf', 8012);"
4
  • 7
    Also nice if you simply need to check quick: drush ev "echo drupal_get_installed_schema_version('MODULE')"; api.drupal.org/api/drupal/core%21includes%21schema.inc/function/… Oct 23, 2017 at 20:20
  • or drush pm-info modulename
    – Beanluc
    Sep 11, 2018 at 15:56
  • @Beanluc - using drush pm-info modulename will give you the version of the module, but not the schema update version. Apr 8, 2020 at 10:15
  • Where do you need to add this: 'Note: In Drupal 7 you need to add extra include 'includes/install.inc';.' in the command or in the .module?
    – Michiel
    Dec 1, 2020 at 15:08
23

Drupal 8 now uses the keyvalue service to store information about the schema version. This is the same API the State API itself uses.

Value information is serialized when stored in the database, so using a direct SQL query is sub-optimal. Instead, use drush php-eval (or a script that bootstraps Drupal, if you don't have Drush) to execute the following:

<?php
  \Drupal::keyValue('system.schema')->set('fillpdf', (int) 8102);
?>

Change fillpdf and 8102 to your module name and desired schema version, respectively.

The Drush form of this command is:

drush ev "\Drupal::keyValue('system.schema')->set('fillpdf', (int) 8102)";

2
  • Note that if you're using ddev then this throws a bash error. You'll need to ddev ssh into the vm and then the command runs fine.
    – Mrweiner
    Feb 19, 2021 at 20:16
  • It's possible to run it from bash, but the escaping is a bit of a pain, so ddev ssh is indeed easier. Mar 10, 2021 at 9:45
0

To SET and GET module updates via code (without Drush). Below is the code

GET  = drupal_get_installed_schema_version('moduleName');

SET = drupal_set_installed_schema_version("moduleName", "8000");
0

Can also go update it directly in the database. If you're using something like Sequel Pro (mac) or whatever database UI tool windows/linux has it's really easy. Arguably not the best idea, and would not advise doing this against a production level database, but it'll get the job done!

Table = key_value

collection = system.schema
name = fillpdf (your_module)
value = update to i:8102;

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.