hook_schema_alter

6 core.php hook_schema_alter(&$schema)
7 system.api.php hook_schema_alter(&$schema)
8 system.api.php hook_schema_alter(&$schema)

Perform alterations to existing database schemas.

When a module modifies the database structure of another module (by changing, adding or removing fields, keys or indexes), it should implement hook_schema_alter() to update the default $schema to take its changes into account.

See hook_schema() for details on the schema definition structure.

Parameters

$schema: Nested array describing the schemas for all modules.

Related topics

1 invocation of hook_schema_alter()

File

modules/system/system.api.php, line 3106
Hooks provided by Drupal core and the System module.

Code

function hook_schema_alter(&$schema) {
  // Add field to existing schema.
  $schema['users']['fields']['timezone_id'] = array(
    'type' => 'int', 
    'not null' => TRUE, 
    'default' => 0, 
    'description' => 'Per-user timezone configuration.',
  );
}

Comments

Example of how to use this

Example of how to use this hook -
http://drupal.org/node/185596#comment-941821

Drupal 6

That's a D6 example. It's NOT the code for D7. See the db_add_field.

"description" of the filed will be silently dropped

When drupal_get_schema() is called it goes through this sequence:
1) hook_schema()
2)_drupal_schema_initialize() (this DROPs the "description" field.)
3) hook_schema_alter() /* "description" field here will not be dropped. */

This creates inconsistency in the resulting $schema.

Should it be done in this sequence:
1) hook_schema()
2) hook_schema_alter()
3)_drupal_schema_initialize()

Login or register to post comments