From 029c48c68d7f4f2bc94e93e3333908ca158433c5 Mon Sep 17 00:00:00 2001 From: Dries Buytaert <dries@buytaert.net> Date: Thu, 10 Sep 2009 06:38:20 +0000 Subject: [PATCH] - Patch #306151 by agentrickard, David_Rothstein, Dave Reid, dbabbage, moshe weitzman: automatically install/uninstall schema. --- includes/common.inc | 3 +- includes/database/database.inc | 4 ++- includes/install.inc | 5 ++- modules/aggregator/aggregator.install | 11 ------- modules/block/block.install | 8 ----- modules/book/book.install | 4 --- modules/comment/comment.install | 11 ------- modules/contact/contact.install | 11 ------- modules/dblog/dblog.install | 16 ---------- modules/field/field.install | 7 ---- .../field_sql_storage.install | 14 -------- modules/forum/forum.install | 3 -- modules/image/image.install | 4 --- modules/locale/locale.install | 5 --- modules/menu/menu.install | 4 --- modules/openid/openid.install | 16 ---------- modules/poll/poll.install | 16 ---------- modules/profile/profile.install | 11 ------- modules/search/search.install | 11 ------- modules/simpletest/simpletest.install | 4 --- .../simpletest/tests/database_test.install | 14 -------- modules/simpletest/tests/field_test.install | 14 -------- .../simpletest/tests/taxonomy_test.install | 15 --------- modules/statistics/statistics.install | 11 ------- modules/system/system.admin.inc | 4 +-- modules/system/system.api.php | 32 +++++++++++++++---- modules/system/system.install | 3 ++ modules/taxonomy/taxonomy.install | 11 ------- modules/trigger/trigger.install | 11 ------- modules/update/update.install | 10 ------ modules/upload/upload.install | 20 ------------ 31 files changed, 39 insertions(+), 274 deletions(-) diff --git a/includes/common.inc b/includes/common.inc index 75f6f820bb10..781e27a9c59a 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -4798,9 +4798,10 @@ function drupal_get_schema_unprocessed($module, $table = NULL) { if (!is_null($table) && isset($schema[$table])) { return $schema[$table]; } - else { + else if (!empty($schema)) { return $schema; } + return array(); } /** diff --git a/includes/database/database.inc b/includes/database/database.inc index 22bd8fe2ef54..f298376ff4de 100644 --- a/includes/database/database.inc +++ b/includes/database/database.inc @@ -2130,7 +2130,9 @@ function db_close(array $options = array()) { * A Schema API table definition array. */ function db_create_table(&$ret, $name, $table) { - return Database::getConnection()->schema()->createTable($ret, $name, $table); + if (!db_table_exists($name)) { + return Database::getConnection()->schema()->createTable($ret, $name, $table); + } } /** diff --git a/includes/install.inc b/includes/install.inc index 0e68a1b062c6..e8efe4b07b92 100644 --- a/includes/install.inc +++ b/includes/install.inc @@ -586,8 +586,9 @@ function drupal_install_modules($module_list = array(), $disable_modules_install */ function _drupal_install_module($module) { if (drupal_get_installed_schema_version($module, TRUE) == SCHEMA_UNINSTALLED) { - module_load_install($module); drupal_load('module', $module); + drupal_install_schema($module); + // Now allow the module to perform install tasks. module_invoke($module, 'install'); $versions = drupal_get_schema_versions($module); drupal_set_installed_schema_version($module, $versions ? max($versions) : SCHEMA_INSTALLED); @@ -663,6 +664,8 @@ function drupal_uninstall_modules($module_list = array()) { // Uninstall the module. module_load_install($module); module_invoke($module, 'uninstall'); + drupal_uninstall_schema($module); + watchdog('system', '%module module uninstalled.', array('%module' => $module), WATCHDOG_INFO); // Now remove the menu links for all paths declared by this module. diff --git a/modules/aggregator/aggregator.install b/modules/aggregator/aggregator.install index e992e70d3fae..cc4d2258a735 100644 --- a/modules/aggregator/aggregator.install +++ b/modules/aggregator/aggregator.install @@ -6,21 +6,10 @@ * Install, update and uninstall functions for the aggregator module. */ -/** - * Implement hook_install(). - */ -function aggregator_install() { - // Create tables. - drupal_install_schema('aggregator'); -} - /** * Implement hook_uninstall(). */ function aggregator_uninstall() { - // Remove tables. - drupal_uninstall_schema('aggregator'); - variable_del('aggregator_allowed_html_tags'); variable_del('aggregator_summary_items'); variable_del('aggregator_clear'); diff --git a/modules/block/block.install b/modules/block/block.install index 0363d682fac2..f1b2b3e7a538 100644 --- a/modules/block/block.install +++ b/modules/block/block.install @@ -205,7 +205,6 @@ function block_schema() { * Implement hook_install(). */ function block_install() { - drupal_install_schema('block'); // Block should go first so that other modules can alter its output // during hook_page_alter(). Almost everything on the page is a block, @@ -216,13 +215,6 @@ function block_install() { ->execute(); } -/** - * Implement hook_uninstall(). - */ -function block_uninstall() { - drupal_uninstall_schema('block'); -} - /** * Set system.weight to a low value for block module. * diff --git a/modules/book/book.install b/modules/book/book.install index 1948d6d11a19..e93518e24a04 100644 --- a/modules/book/book.install +++ b/modules/book/book.install @@ -10,8 +10,6 @@ * Implement hook_install(). */ function book_install() { - // Create tables. - drupal_install_schema('book'); // Add the node type. _book_install_type_create(); } @@ -23,8 +21,6 @@ function book_uninstall() { // Delete menu links. db_query("DELETE FROM {menu_links} WHERE module = 'book'"); menu_cache_clear_all(); - // Remove tables. - drupal_uninstall_schema('book'); } function _book_install_type_create() { diff --git a/modules/comment/comment.install b/modules/comment/comment.install index 7257930995a8..84111ce1ee09 100644 --- a/modules/comment/comment.install +++ b/modules/comment/comment.install @@ -6,21 +6,10 @@ * Install, update and uninstall functions for the comment module. */ -/** - * Implement hook_install(). - */ -function comment_install() { - // Create tables. - drupal_install_schema('comment'); -} - /** * Implement hook_uninstall(). */ function comment_uninstall() { - // Remove tables. - drupal_uninstall_schema('comment'); - // Remove variables. variable_del('comment_block_count'); $node_types = array_keys(node_type_get_types()); diff --git a/modules/contact/contact.install b/modules/contact/contact.install index 929cc54fa184..3e22f41bb8f1 100644 --- a/modules/contact/contact.install +++ b/modules/contact/contact.install @@ -6,21 +6,10 @@ * Install, update and uninstall functions for the contact module. */ -/** - * Implement hook_install(). - */ -function contact_install() { - // Create tables. - drupal_install_schema('contact'); -} - /** * Implement hook_uninstall(). */ function contact_uninstall() { - // Remove tables. - drupal_uninstall_schema('contact'); - variable_del('contact_default_status'); variable_del('contact_form_information'); variable_del('contact_hourly_threshold'); diff --git a/modules/dblog/dblog.install b/modules/dblog/dblog.install index 717be592cffc..fef784fb7b1f 100644 --- a/modules/dblog/dblog.install +++ b/modules/dblog/dblog.install @@ -6,22 +6,6 @@ * Install, update and uninstall functions for the dblog module. */ -/** - * Implement hook_install(). - */ -function dblog_install() { - // Create tables. - drupal_install_schema('dblog'); -} - -/** - * Implement hook_uninstall(). - */ -function dblog_uninstall() { - // Remove tables. - drupal_uninstall_schema('dblog'); -} - /** * Implement hook_schema(). */ diff --git a/modules/field/field.install b/modules/field/field.install index a7b9df27b320..ba3db1df5ae0 100644 --- a/modules/field/field.install +++ b/modules/field/field.install @@ -6,13 +6,6 @@ * Install, update and uninstall functions for the field module. */ -/** - * Implement hook_install(). - */ -function field_install() { - drupal_install_schema('field'); -} - /** * Implement hook_schema. */ diff --git a/modules/field/modules/field_sql_storage/field_sql_storage.install b/modules/field/modules/field_sql_storage/field_sql_storage.install index 450029e4b127..67b7e4afc08f 100644 --- a/modules/field/modules/field_sql_storage/field_sql_storage.install +++ b/modules/field/modules/field_sql_storage/field_sql_storage.install @@ -6,20 +6,6 @@ * Install, update and uninstall functions for the field_sql_storage module. */ -/** - * Implement hook_install(). - */ -function field_sql_storage_install() { - drupal_install_schema('field_sql_storage'); -} - -/** - * Implement hook_uninstall(). - */ -function field_sql_storage_uninstall() { - drupal_uninstall_schema('field_sql_storage'); -} - /** * Implement hook_schema(). */ diff --git a/modules/forum/forum.install b/modules/forum/forum.install index 6ad9ee9b765c..c42490e2eb92 100644 --- a/modules/forum/forum.install +++ b/modules/forum/forum.install @@ -10,8 +10,6 @@ * Implement hook_install(). */ function forum_install() { - // Create tables. - drupal_install_schema('forum'); // Set the weight of the forum.module to 1 so it is loaded after the taxonomy.module. db_update('system') ->fields(array('weight' => 1)) @@ -61,7 +59,6 @@ function forum_uninstall() { $vid = variable_get('forum_nav_vocabulary', 0); taxonomy_vocabulary_delete($vid); - drupal_uninstall_schema('forum'); variable_del('forum_containers'); variable_del('forum_nav_vocabulary'); variable_del('forum_hot_topic'); diff --git a/modules/image/image.install b/modules/image/image.install index b963605dc244..8cd205e193eb 100644 --- a/modules/image/image.install +++ b/modules/image/image.install @@ -10,8 +10,6 @@ * Implement hook_install(). */ function image_install() { - drupal_install_schema('image'); - // Create the styles directory and ensure it's writable. $path = file_directory_path() . '/styles'; file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); @@ -21,8 +19,6 @@ function image_install() { * Implement hook_uninstall(). */ function image_uninstall() { - drupal_uninstall_schema('image'); - // Remove the styles directory and generated images. $path = file_directory_path() . '/styles'; file_unmanaged_delete_recursive($path); diff --git a/modules/locale/locale.install b/modules/locale/locale.install index f08b75d44257..d487803c13c7 100644 --- a/modules/locale/locale.install +++ b/modules/locale/locale.install @@ -14,9 +14,6 @@ function locale_install() { // fields; non-MySQL database servers need to ensure the field type is text // and that LIKE produces a case-sensitive comparison. - // Create tables. - drupal_install_schema('locale'); - db_insert('languages') ->fields(array( 'language' => 'en', @@ -83,8 +80,6 @@ function locale_uninstall() { // try to query the unexisting {locales_source} and {locales_target} tables. drupal_language_initialize(); - // Remove tables. - drupal_uninstall_schema('locale'); } /** diff --git a/modules/menu/menu.install b/modules/menu/menu.install index 1ff901a09b6a..aca5835ee143 100644 --- a/modules/menu/menu.install +++ b/modules/menu/menu.install @@ -10,8 +10,6 @@ * Implement hook_install(). */ function menu_install() { - // Create tables. - drupal_install_schema('menu'); $system_menus = menu_list_system_menus(); $descriptions = array( 'navigation' => 'The <em>Navigation</em> menu contains links such as Recent posts (if the Tracker module is enabled). Non-administrative links are added to this menu by default by modules.', @@ -31,8 +29,6 @@ function menu_install() { * Implement hook_uninstall(). */ function menu_uninstall() { - // Remove tables. - drupal_uninstall_schema('menu'); menu_rebuild(); } diff --git a/modules/openid/openid.install b/modules/openid/openid.install index 6dd056bcd00e..62955ad2b513 100644 --- a/modules/openid/openid.install +++ b/modules/openid/openid.install @@ -6,22 +6,6 @@ * Install, update and uninstall functions for the openid module. */ -/** - * Implement hook_install(). - */ -function openid_install() { - // Create table. - drupal_install_schema('openid'); -} - -/** - * Implement hook_uninstall(). - */ -function openid_uninstall() { - // Remove table. - drupal_uninstall_schema('openid'); -} - /** * Implement hook_schema(). */ diff --git a/modules/poll/poll.install b/modules/poll/poll.install index 5ad698082cff..880b317fca1e 100644 --- a/modules/poll/poll.install +++ b/modules/poll/poll.install @@ -6,22 +6,6 @@ * Install, update and uninstall functions for the poll module. */ -/** - * Implement hook_install(). - */ -function poll_install() { - // Create tables. - drupal_install_schema('poll'); -} - -/** - * Implement hook_uninstall(). - */ -function poll_uninstall() { - // Remove tables. - drupal_uninstall_schema('poll'); -} - /** * Implement hook_schema(). */ diff --git a/modules/profile/profile.install b/modules/profile/profile.install index b07a6b8b36b9..b07780671c65 100644 --- a/modules/profile/profile.install +++ b/modules/profile/profile.install @@ -6,21 +6,10 @@ * Install, update and uninstall functions for the profile module. */ -/** - * Implement hook_install(). - */ -function profile_install() { - // Create tables. - drupal_install_schema('profile'); -} - /** * Implement hook_uninstall(). */ function profile_uninstall() { - // Remove tables - drupal_uninstall_schema('profile'); - variable_del('profile_block_author_fields'); } diff --git a/modules/search/search.install b/modules/search/search.install index 239edb48463a..5cb6f887b7dd 100644 --- a/modules/search/search.install +++ b/modules/search/search.install @@ -6,21 +6,10 @@ * Install, update and uninstall functions for the search module. */ -/** - * Implement hook_install(). - */ -function search_install() { - // Create tables. - drupal_install_schema('search'); -} - /** * Implement hook_uninstall(). */ function search_uninstall() { - // Remove tables. - drupal_uninstall_schema('search'); - variable_del('minimum_word_size'); variable_del('overlap_cjk'); variable_del('search_cron_limit'); diff --git a/modules/simpletest/simpletest.install b/modules/simpletest/simpletest.install index 1ca0bae183ff..d12d5befd9f5 100644 --- a/modules/simpletest/simpletest.install +++ b/modules/simpletest/simpletest.install @@ -10,7 +10,6 @@ * Implement hook_install(). */ function simpletest_install() { - drupal_install_schema('simpletest'); // Check for files directory. $path = 'public://simpletest'; if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) { @@ -110,9 +109,6 @@ function simpletest_uninstall() { variable_del('simpletest_clear_results'); variable_del('simpletest_verbose'); - // Uninstall schema. - drupal_uninstall_schema('simpletest'); - // Remove generated files. $path = file_directory_path() . '/simpletest'; $files = file_scan_directory($path, '/.*/'); diff --git a/modules/simpletest/tests/database_test.install b/modules/simpletest/tests/database_test.install index 6b9607775032..b9fe61ab41e6 100644 --- a/modules/simpletest/tests/database_test.install +++ b/modules/simpletest/tests/database_test.install @@ -204,17 +204,3 @@ function database_test_schema() { return $schema; } - -/** - * Implement hook_install(). - */ -function database_test_install() { - drupal_install_schema('database_test'); -} - -/** - * Implement hook_uninstall(). - */ -function database_test_uninstall() { - drupal_uninstall_schema('database_test'); -} diff --git a/modules/simpletest/tests/field_test.install b/modules/simpletest/tests/field_test.install index 1fa0eeb11714..83beb5e42338 100644 --- a/modules/simpletest/tests/field_test.install +++ b/modules/simpletest/tests/field_test.install @@ -64,17 +64,3 @@ function field_test_schema() { return $schema; } - -/** - * Implement hook_install(). - */ -function field_test_install() { - drupal_install_schema('field_test'); -} - -/** - * Implement hook_uninstall(). - */ -function field_test_uninstall() { - drupal_uninstall_schema('field_test'); -} diff --git a/modules/simpletest/tests/taxonomy_test.install b/modules/simpletest/tests/taxonomy_test.install index 2d771d0e22d8..c4a568da42d5 100644 --- a/modules/simpletest/tests/taxonomy_test.install +++ b/modules/simpletest/tests/taxonomy_test.install @@ -33,18 +33,3 @@ function taxonomy_test_schema() { return $schema; } - -/** - * Implement hook_install(). - */ -function taxonomy_test_install() { - drupal_install_schema('taxonomy_test'); -} - -/** - * Implement hook_uninstall(). - */ -function taxonomy_test_uninstall() { - drupal_uninstall_schema('taxonomy_test'); -} - diff --git a/modules/statistics/statistics.install b/modules/statistics/statistics.install index aca554790bdd..396faab80057 100644 --- a/modules/statistics/statistics.install +++ b/modules/statistics/statistics.install @@ -6,21 +6,10 @@ * Install, update and uninstall functions for the statistics module. */ -/** - * Implement hook_install(). - */ -function statistics_install() { - // Create tables. - drupal_install_schema('statistics'); -} - /** * Implement hook_uninstall(). */ function statistics_uninstall() { - // Remove tables. - drupal_uninstall_schema('statistics'); - // Remove variables. variable_del('statistics_count_content_views'); variable_del('statistics_enable_access_log'); diff --git a/modules/system/system.admin.inc b/modules/system/system.admin.inc index 37d1fc2a0927..2d1875e746c2 100644 --- a/modules/system/system.admin.inc +++ b/modules/system/system.admin.inc @@ -1051,10 +1051,10 @@ function system_modules_uninstall($form_state = NULL) { // Grab the module info $info = unserialize($module->info); - // Load the .install file, and check for an uninstall hook. + // Load the .install file, and check for an uninstall or schema hook. // If the hook exists, the module can be uninstalled. module_load_install($module->name); - if (module_hook($module->name, 'uninstall')) { + if (module_hook($module->name, 'uninstall') || module_hook($module->name, 'schema')) { $form['modules'][$module->name]['name'] = array('#markup' => $info['name'] ? $info['name'] : $module->name); $form['modules'][$module->name]['description'] = array('#markup' => t($info['description'])); $options[$module->name] = ''; diff --git a/modules/system/system.api.php b/modules/system/system.api.php index 2c5bcbd2788f..da3d489ee33a 100644 --- a/modules/system/system.api.php +++ b/modules/system/system.api.php @@ -1797,7 +1797,10 @@ function hook_query_TAG_alter(QueryAlterableInterface $query) { } /** - * Install the current version of the database schema, and any other setup tasks. + * Perform setup tasks when the module is installed. + * + * If the module implements hook_schema(), the database tables will + * be created before this hook is fired. * * The hook will be called the first time a module is installed, and the * module's schema version will be set to the module's greatest numbered update @@ -1807,7 +1810,7 @@ function hook_query_TAG_alter(QueryAlterableInterface $query) { * * See the Schema API documentation at * @link http://drupal.org/node/146843 http://drupal.org/node/146843 @endlink - * for details on hook_schema, where a database tables are defined. + * for details on hook_schema and how database tables are defined. * * Note that since this function is called from a full bootstrap, all functions * (including those in modules enabled by the current page request) are @@ -1818,9 +1821,20 @@ function hook_query_TAG_alter(QueryAlterableInterface $query) { * be removed during uninstall should be removed with hook_uninstall(). * * @see hook_uninstall() + * @see hook_schema() */ function hook_install() { - drupal_install_schema('upload'); + // Populate the default {node_access} record. + db_insert('node_access') + ->fields(array( + 'nid' => 0, + 'gid' => 0, + 'realm' => 'all', + 'grant_view' => 1, + 'grant_update' => 0, + 'grant_delete' => 0, + )) + ->execute(); } /** @@ -1949,15 +1963,19 @@ function hook_update_last_removed() { * * The information that the module should remove includes: * - variables that the module has set using variable_set() or system_settings_form() - * - tables the module has created, using drupal_uninstall_schema() * - modifications to existing tables * - * The module should not remove its entry from the {system} table. + * The module should not remove its entry from the {system} table. Database tables + * defined by hook_schema() will be removed automatically. * - * The uninstall hook will fire when the module gets uninstalled. + * The uninstall hook will fire when the module gets uninstalled but before the + * module's database tables are removed, allowing your module to query its own + * tables during this routine. + * + * @see hook_install() + * @see hook_schema() */ function hook_uninstall() { - drupal_uninstall_schema('upload'); variable_del('upload_file_types'); } diff --git a/modules/system/system.install b/modules/system/system.install index 4757d9b75f7d..380d679fcf04 100644 --- a/modules/system/system.install +++ b/modules/system/system.install @@ -338,6 +338,9 @@ function system_install() { $modules = array('system', 'filter', 'user', 'node'); foreach ($modules as $module) { drupal_install_schema($module); + $versions = drupal_get_schema_versions($module); + $version = $versions ? max($versions) : SCHEMA_INSTALLED; + drupal_set_installed_schema_version($module, $version); } // Load system theme data appropriately. diff --git a/modules/taxonomy/taxonomy.install b/modules/taxonomy/taxonomy.install index 807c93375a60..c4c2a542c7e1 100644 --- a/modules/taxonomy/taxonomy.install +++ b/modules/taxonomy/taxonomy.install @@ -6,21 +6,10 @@ * Install, update and uninstall functions for the taxonomy module. */ - /** - * Implement hook_install(). - */ -function taxonomy_install() { - // Create tables. - drupal_install_schema('taxonomy'); -} - /** * Implement hook_uninstall(). */ function taxonomy_uninstall() { - // Remove tables. - drupal_uninstall_schema('taxonomy'); - // Remove variables. variable_del('taxonomy_override_selector'); variable_del('taxonomy_terms_per_page_admin'); diff --git a/modules/trigger/trigger.install b/modules/trigger/trigger.install index 631efd18b170..2e014d071d2f 100644 --- a/modules/trigger/trigger.install +++ b/modules/trigger/trigger.install @@ -10,21 +10,10 @@ * Implement hook_install(). */ function trigger_install() { - // Create tables. - drupal_install_schema('trigger'); - // Do initial synchronization of actions in code and the database. actions_synchronize(); } -/** - * Implement hook_uninstall(). - */ -function trigger_uninstall() { - // Remove tables. - drupal_uninstall_schema('trigger'); -} - /** * Implement hook_schema(). */ diff --git a/modules/update/update.install b/modules/update/update.install index f8a6e3ab6081..0c2713149fd4 100644 --- a/modules/update/update.install +++ b/modules/update/update.install @@ -6,20 +6,10 @@ * Install, update and uninstall functions for the update module. */ -/** - * Implement hook_install(). - */ -function update_install() { - // Create cache table. - drupal_install_schema('update'); -} - /** * Implement hook_uninstall(). */ function update_uninstall() { - // Remove cache table. - drupal_uninstall_schema('update'); // Clear any variables that might be in use $variables = array( 'update_check_frequency', diff --git a/modules/upload/upload.install b/modules/upload/upload.install index 395abfebf127..62acef71712e 100644 --- a/modules/upload/upload.install +++ b/modules/upload/upload.install @@ -11,26 +11,6 @@ * This is the install file for the upload module. */ -/** - * Implement hook_install(). - */ -function upload_install() { - // Create table. The upload table might have been created in the Drupal 5 - // to Drupal 6 upgrade, and was migrated from the file_revisions table. So - // in this case, there is no need to create the table, it is already there. - if (!db_table_exists('upload')) { - drupal_install_schema('upload'); - } -} - -/** - * Implement hook_uninstall(). - */ -function upload_uninstall() { - // Remove tables. - drupal_uninstall_schema('upload'); -} - /** * Implement hook_schema(). */ -- GitLab