Actually, hook_schema()
is not just used when a module is installed, and Drupal creates the database tables the module needs. It is also used from other functions or methods that need to know the fields a database table contains. For example, EntityFieldQuery::propertyQuery() and drupal_write_record() use it.
Then, when a module is enabled/installed, Drupal (via module_enable()
) invokes hook_schema()
twice:
// Refresh the schema to include it.
drupal_get_schema(NULL, TRUE);
// Update the theme registry to include it.
drupal_theme_rebuild();
// Clear entity cache.
entity_info_cache_clear();
// Now install the module if necessary.
if (drupal_get_installed_schema_version($module, TRUE) == SCHEMA_UNINSTALLED) {
drupal_install_schema($module);
// Set the schema version to the number of the last update provided
// by the module.
$versions = drupal_get_schema_versions($module);
$version = $versions ? max($versions) : SCHEMA_INSTALLED;
// If the module has no current updates, but has some that were
// previously removed, set the version to the value of
// hook_update_last_removed().
if ($last_removed = module_invoke($module, 'update_last_removed')) {
$version = max($version, $last_removed);
}
drupal_set_installed_schema_version($module, $version);
// Allow the module to perform install tasks.
module_invoke($module, 'install');
// Record the fact that it was installed.
$modules_installed[] = $module;
watchdog('system', '%module module installed.', array('%module' => $module), WATCHDOG_INFO);
}