Question

How can i get hook_install() to run all updates that I have for my module? I have in it mymod.install file.

I have tried

function mymod_install() {
  mymod_update_6001();
}

but it does not seem to do anything. If I have an mymod_update_6002() would I still call 6001(), just call 6002() or call both in the order 6001(), 6002().

I thought it would be common sense to run all the updates when you installed a module by default.

Was it helpful?

Solution

You shouldn't get your install hook to run updates. When a module is installed for the first time no updates should be necessary.

Updates are used when the module already is installed (the database or variables have been created). In that case, you don't want to reinstall the module as it will wipe all data, instead you create update_N hooks. Drupal will detect which updates are needed and those will be run going to update.php. In Drupal 6 it will auto select which updates to do, but you can change it, while this is not possible in Drupal 7.

Drupal detects which updates are needed by saving the number of the last run update. This can be changes in the database, which will allow to rerun updates in Drupal 7. Rerunning updates is usually a bad idea and will most often cause errors and can in some cases mess up your data.

Always remember to backup your database before running updates.

OTHER TIPS

The purpose of the hooks is different.

  • hook_install() is invoked when a module is installed; it means the module was not previously installed, and therefore, it doesn't need to be updated.
  • hook_update_N() is invoked when a module is already installed, and it needs to be updated.

If there are some tasks that needs to be done both when the module is installed, and when the module is updated, then the code should be present in both the hooks. There are no module that invokes all the update hooks during the installation, and that would be especially wrong when the update functions update the schema of the database tables used from the module; hook_schema() should always return the updated schema, and updating the schema also with the update functions would be wrong.

The code you wrote doesn't work because calling one of the update functions doesn't automatically execute all the other update functions.
Calling the update functions from the implementation of hook_install()is wrong, as it wrong to call an update function from another one; if there is some code that needs to be executed from two or more update functions, then that code should be placed in a function that is called from the update functions, and from hook_install(), if necessary.

The install hook is only called when the module is freshly installed, so there is no need for any updates. The updates are executed only when the module is already installed, so it can apply additional changes.

To force the update functions to run on install, you've to change your schema version, for instance:

function mymod_install() {
  // Reset the schema version, so our update hooks can be processed during the installation.
  drupal_set_installed_schema_version('mymod', '7000');
  // Then run the updates as usual.
  mymod_update_7001();
}

Or run it from drush: drush -y updb.

See also: Is it possible to force your module's update hook to run?

hook_install would not work with in mymod.module file.

You have to place hook_install and hook_uninstall in a separate file mymod.install

I guess that hook_update_N() should also be placed in mymod.install


Answer after the question updated:

Quoting from a link, Please check your version number in info file.

Since I am developing privately and not for contribution, my 'VERSION' field in my module's .info file is either blank or unchanged when I update a module. I am actually using Subversive for eclipse PDT and therefore my info files version numbers never get updated. Thus, my hook_update_6001 was NOT being called by update.php.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top