hook_update_N()
contributions/docs/developer/hooks/install.php, line 202
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 starting with 1 in each module. The first is mymodule_update_1().
A good rule of thumb is to remove updates older than two major releases of Drupal. Never renumber update functions. See hook_update_last_removed() to notify Drupal about the removals.
Whenever possible implement both PostgreSQL and MySQL at the same time. If PostgreSQL updates are added later, add a new update function which only does the PostgreSQL update. Be sure to use comments to describe which updates are the same if they do get separated.
Implementations of this hook should be placed in a mymodule.install file in the same directory as mymodule.module. Drupal core's updates are implemented using the system module as a name and stored in database/updates.inc.
The following examples serve as a quick guide to MySQL to PostgreSQL conversion. Usually (but not always!) you will use following SQL statements:
An array with the results of the calls to update_sql().
| Name | Description |
|---|---|
| Hooks | Allow modules to interact with the Drupal core. |
function hook_update_N() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'pgsql':
db_add_column($ret, 'contact', 'weight', 'smallint', array('not null' => TRUE, 'default' => 0));
db_add_column($ret, 'contact', 'selected', 'smallint', array('not null' => TRUE, 'default' => 0));
break;
case 'mysql':
case 'mysqli':
$ret[] = update_sql("ALTER TABLE {contact} ADD COLUMN weight tinyint(3) NOT NULL DEFAULT 0");
$ret[] = update_sql("ALTER TABLE {contact} ADD COLUMN selected tinyint(1) NOT NULL DEFAULT 0");
break;
}
return $ret;
}