hook_update_N

Definition

hook_update_N()
developer/hooks/install.php, line 216

Description

Perform a single update. For each patch which requires a database change add a new hook_update_N() which will be called by update.php.

The database updates are numbered sequentially according to the version of Drupal you are compatible with.

Schema updates should adhere to the Schema API: http://drupal.org/node/150215

Database updates consist of 3 parts:

  • 1 digit for Drupal core compatibility
  • 1 digit for your module's major release version (e.g. is this the 5.x-1.* (1) or 5.x-2.* (2) series of your module?)
  • 2 digits for sequential counting starting with 00
The 2nd digit should be 0 for initial porting of your module to a new Drupal core API.

Examples:

  • mymodule_update_5200()
    • This is the first update to get the database ready to run mymodule 5.x-2.*.
  • mymodule_update_6000()
    • This is the required update for mymodule to run with Drupal core API 6.x.
  • mymodule_update_6100()
    • This is the first update to get the database ready to run mymodule 6.x-1.*.
  • mymodule_update_6200()
    • This is the first update to get the database ready to run mymodule 6.x-2.*. Users can directly update from 5.x-2.* to 6.x-2.* and they get all 60XX and 62XX updates, but not 61XX updates, because those reside in the 6.x-1.x branch only.
A good rule of thumb is to remove updates older than two major releases of Drupal.

Never renumber update functions.

Further information about releases and release numbers:

Implementations of this hook should be placed in a mymodule.install file in the same directory at mymodule.module. Drupal core's updates are implemented using the system module as a name and stored in database/updates.inc.

Return value

An array with the results of the calls to update_sql().

Related topics

Namesort iconDescription
HooksAllow modules to interact with the Drupal core.

Code

<?php
function hook_update_N() {
  $ret = array();
  db_add_field($ret, 'mytable1', 'newcol', array('type' => 'int', 'not null' => TRUE));
  return $ret;
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.