3

I know there is a question hook_update_N not running but I checked its answers and it didn't help. I'm just learning developing modules for Drupal so maybe it's something really stupid but I have no idea...

My hook (in testModule4.install):

function testModule4_update_7001() {

$schema['testModule4_table'] = array(
    'description' => 'TODO: Description',
    'fields' => array(
        'main_id' => array(
            'description' => 'TODO: Description.',
            'type' => 'serial',
            'not null' => TRUE,
        ),
        'settings' => array(
            'description' => 'TODO: Description',
            'type' => 'text',
            'not null' => FALSE,
        ),
        'uid' => array(
            'description' => 'TODO: Description',
            'type' => 'int',
            'not null' => FALSE,
        ),
        'created' => array(
            'description' => 'TODO: Description',
            'type' => 'int',
            'not null' => FALSE,
        ),
        'changed' => array(
            'description' => 'TODO: Description',
            'type' => 'int',
            'not null' => FALSE,
        ),
    ),
    'primary key' => array('main_id'),
);

db_create_table('testModule4_table', $schema['testModule4_table']);

}

My module is working just fine. The schema_version of module in database is 0 but update.php just cannot see it. What can be the cause of that? :/ I was trying clearing cache and reenabling a module but without success. I just don't know where to search for source of the problem. If you need more data, just comment :/ .

Edit:

I created simple module:

testModule.module:

<?php


function testModule_myform($delta = "") {

    $form['something'] = array(
        '#type' => 'markup',
        '#markup' => 'Something',
    );
    return $form;
}



/**
 * Implements hook_menu();
 */

function testModule_menu() {
    $items["test/test"] = array(
        'title' => 'testModule',
        'description' => 'TODO: Description',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('testModule_myform'),
        'access callback' => TRUE,

    );
    return $items;
}

testModule.info:

name = testModule
description = Test module for Drupal experiments.
package = CustomTest
core = 7.x

testModule.install:

function testModule_update_7001() {
    drupal_set_message('It works!');
    print 'Show something!';
}

And it still ignores this hook... Is it officially a Drupal bug and I need to reinstall the whole Drupal? >_<

1
  • 1
    On a side note, if you follow the Drupal coding standards your function names (and therefore module names as well) should be lowercase, with words separated by an underscore. Jul 5, 2012 at 6:58

5 Answers 5

13

The problem is upper/lowercase in your module name.

To be able to run update functions, your module must consist of lowercase characters only. Unlike all other hooks, those are detected through regexes and the way it's implemented isn't compatible with mixed case.

4
  • You was right. I changed name to "newmodule" and now I can see that after installing it "schema_version" in database has proper number (just like update hook). I have ActionScript 3 background and we are using "camel case" for naming lol, it was nasty trap for the beginner. Thanks for help! Jul 5, 2012 at 8:17
  • Interesting, I didn't know that. So my earlier sidenote was more relevant than I thought... Thanks Berdir. Jul 5, 2012 at 9:14
  • Any reason why the regex is case-sensitive? I smell a bug...
    – Clive
    Jul 5, 2012 at 10:57
  • I dont remember the exact problem I think it's not just the regex, I would have to dig in again, was a long time ago. This is like this at least since Drupal 5 (I had the problem there IIRC). And yes, this is very nasty.
    – Berdir
    Jul 5, 2012 at 12:06
5

What reported by Berdir about the module name is correct, but there is something else to consider.

I created a module (testmodule.module) that defined an update function (testmodule_update_7001()) in its installation file. After I installed it, the {system} database table contained the following row about my test module.

screenshot

Until I don't add a function using an update number higher than 7001, Drupal will consider my module as updated. In fact, this is what I get when I run update.php.

screenshot

Drupal doesn't see any update.

Now I change the installation file. This is what it contains now.

/**
 * Check the update functions are invoked.
 */
function testmodule_update_7001() {
  drupal_set_message('testmodule_update_7001: It works!');
} 

This is how I change it.

/**
 * Check the update functions are invoked.
 */
function testmodule_update_7002() {
  drupal_set_message('testmodule_update_7002: It works!');
} 

I run update.php again, and this time I obtain the following.

screenshot

This time Drupal noticed there are updates, simply for the fact I used an update number higher than the update number Drupal recorded in the {system} table.

Bear in mind that update hooks are not invoked when a module is installed; only hook_install() is invoked when a module is installed.
After a module is installed, the update hooks are invoked, when their update number is higher than the update number Drupal registered when the module was installed. If your module didn't have any update hooks, Drupal will record 0 as schema version, and any update hooks you add after will be invoked.

As side note, the description shown from update.php is taken from the comment for the update hook. The code for that is contained in update_get_update_list().

  $updates = drupal_map_assoc($updates);
  foreach (array_keys($updates) as $update) {
    if ($update > $schema_version) {
      // The description for an update comes from its Doxygen.
      $func = new ReflectionFunction($module . '_update_' . $update);
      $description = str_replace(array("\n", '*', '/'), '', $func->getDocComment());
      $ret[$module]['pending'][$update] = "$update - $description";
      if (!isset($ret[$module]['start'])) {
        $ret[$module]['start'] = $update;
      }
    }
  }
4

hook_update_N() is only called when a module is being updated. If you're installing it for the first time, or you uninstalled and are reinstalling, the update functions will not be called at all.

So to test your update functions the correct procedure would be to:

  1. Remove (or comment out) all instances of hook_update_N()
  2. Install the module
  3. Replace (or uncomment) all update functions
  4. Run update.php

Remember that your hook_schema() and hook_install() should always be in sync with your latest numbered update function.

As a side note, please check the documentation for hook_update_N() for how the numbering scheme works. In your example, you would want to add a version in your testModule.info and use that to determine what number update function you should use.

0

Try this. It might help.

function testModule_update_7050() {
    drupal_set_message('It works!');
    print 'Show something!';
}

Note: changed the number in the function.

0

Did you try to disable, uninstall, and enable again? This just helped me to have .install running.

1
  • Didn't help... I though that Drupal will run all updates while installing the module but it didn't happen. schema_version is still 0 :/ . PS. Other functions in .install file works just fine. Jul 4, 2012 at 21:09

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.