upgrade.drush.inc

  1. 3.x commands/core/upgrade.drush.inc
  2. 4.x commands/core/upgrade.drush.inc

Functions

Namesort descending Description
drush_upgrade_fix_db_url
drush_upgrade_site_upgrade A drush command callback.
drush_upgrade_site_upgrade_validate
upgrade_drush_command Implement hook_drush_command().
upgrade_drush_help Implement hook_drush_help().

File

commands/core/upgrade.drush.inc
View source
  1. <?php
  2. /* TODO
  3. * - upgrade to specific releases.
  4. /*
  5. /**
  6. * @file
  7. * Refine your Drupal major version upgrade.
  8. */
  9. /**
  10. * Implement hook_drush_command().
  11. */
  12. function upgrade_drush_command() {
  13. $items = array();
  14. $items['site-upgrade'] = array(
  15. 'description' => "Run a major version upgrade for Drupal core and contrib modules.",
  16. 'drupal dependencies' => array('update'),
  17. 'core' => array(6), // Remove once 3.0 is released.
  18. 'arguments' => array(
  19. 'target' => 'The name of a sitealias, which points to the destination site. root, uri, and db-url keys are required. See examples/aliases.drushrc.php for more information.'),
  20. 'examples' => array(
  21. 'drush site-upgrade @onward' => 'Upgrade from the current site to the site specified by @onward alias.'
  22. ),
  23. 'options' => array(
  24. 'structure-tables-key' => 'A key in the structure-tables array. @see example.drushrc.php. Defaults to \'common\'.',
  25. 'source-dump' => 'Path to dump file. Medium or large sized sites should set this. Optional; default is to create a temporary file.',
  26. 'db-su' => 'DB username to use when dropping and creating the target database. Optional.',
  27. 'db-su-pw' => 'DB password to use when dropping and creating the target database. Optional.',
  28. ),
  29. 'aliases' => array('sup'),
  30. );
  31. return $items;
  32. }
  33. /**
  34. * Implement hook_drush_help().
  35. */
  36. function upgrade_drush_help($section) {
  37. switch ($section) {
  38. case 'drush:site-upgrade':
  39. return dt("Execute a major version upgrade for Drupal core and enabled contrib modules. Command will download next version of Drupal and all available contrib modules that have releases (if not already downloaded). It prepares a settings.php for the target site, and copies the prior version's database to the target site. Finally, updatedb is run. The intent is for developers to keep re-running this command until they are satisfied with the resulting site.");
  40. }
  41. }
  42. function drush_upgrade_site_upgrade_validate($target_key = NULL) {
  43. if (empty($target_key)) {
  44. return drush_set_error(dt('Missing argument: target'));
  45. }
  46. if (!$target_alias = drush_sitealias_get_record($target_key)) {
  47. return drush_set_error('Site alias not found: @target-key. See example.drushrc.inc.', array('@target-key' => $target_key));
  48. }
  49. if (!file_exists(dirname($target_alias['root']))) {
  50. drush_set_error('Site alias root not found: @root. See example.drushrc.inc.', array('@root' => dirname($target_alias['root'])));
  51. }
  52. }
  53. /**
  54. * A drush command callback.
  55. */
  56. function drush_upgrade_site_upgrade($target_key) {
  57. $source_version = drush_drupal_major_version();
  58. $target_version = $source_version + 1;
  59. $target_alias = drush_sitealias_get_record($target_key);
  60. $destination_core = $target_alias['root'];
  61. // Fetch target core and place as per target alias root.
  62. if (!file_exists($destination_core)) {
  63. drush_set_option('destination', dirname($destination_core));
  64. drush_set_option('drupal-project-rename', basename($destination_core));
  65. // No need for version control in this command.
  66. drush_set_option('version-control', 'backup');
  67. // TODO: get releases other than dev snapshot.
  68. drush_pm_download('drupal-'. $target_version . '.x');
  69. if (drush_get_error()) return -1; // Early exit if we see an error.
  70. }
  71. // Get enabled projects and their paths.
  72. // TODO: D5 compatibility.
  73. _update_cache_clear();
  74. module_load_include('inc', 'update', 'update.compare');
  75. $projects = update_get_projects();
  76. // We already downloaded Drupal project.
  77. unset($projects['drupal']);
  78. $projects = pm_get_project_path($projects, 'includes');
  79. // Fetch and place each project into target.
  80. // TODO: use non dev snapshot releases.
  81. // TODO: Fix pm-download so this cancel hack is not needed.
  82. drush_set_option('bootstrap_cancel', TRUE);
  83. foreach ($projects as $key => $project) {
  84. if (empty($project['path'])) {
  85. $project['path'] = 'sites/all/modules';
  86. }
  87. $destination_module = $destination_core . '/' . $project['path'];
  88. if (!file_exists($destination_module)) {
  89. drush_set_option('destination', dirname($destination_module));
  90. drush_pm_download($key . '-'. $target_version . '.x');
  91. }
  92. }
  93. if (drush_get_error()) return -1; // Early exit if we see an error.
  94. // Create sites subdirectory in target if needed.
  95. $settings_source = conf_path() . '/settings.php';
  96. $settings_destination = $destination_core . '/' . $settings_source;
  97. if (!file_exists(dirname($settings_destination))) {
  98. if (!drush_op('mkdir', $settings_destination) && !drush_get_context('DRUSH_SIMULATE')) {
  99. drush_set_error(dt('Failed to create directory @settings_destination', array('@settings_destination' => $settings_destination)));
  100. return;
  101. }
  102. }
  103. // Copy settings.php to target.
  104. if (!file_exists($settings_destination)) {
  105. if (!drush_op('copy', $settings_source, $settings_destination) && !drush_get_context('DRUSH_SIMULATE')) {
  106. drush_set_error(dt('Failed to copy @source to @dest', array('@source' => $settings_source, 'dest' => $settings_destination)));
  107. return;
  108. }
  109. }
  110. // Append new $db_url with new DB name in target's settings.php.
  111. drush_upgrade_fix_db_url($target_alias, $settings_destination);
  112. // Copy source database to target database. The source DB is not changed.
  113. // Always set 'common' at minimum. Sites that want other can create other key in drushrc.php.
  114. if (!drush_get_option('structure-tables-key')) {
  115. drush_set_option('structure-tables-key', 'common');
  116. }
  117. // Always blow away the target database so we start fresh.
  118. drush_set_option('create-db', TRUE);
  119. drush_include(DRUSH_BASE_PATH . '/commands/sql', 'sync.sql');
  120. drush_sql_sync('@self', $target_key);
  121. if (drush_get_error()) return -1; // Early exit if we see an error.
  122. // Run update.php in a subshell. It is run on @target site whereas this request was on @self.
  123. drush_do_site_command($target_alias, 'updatedb', array(), array(), TRUE);
  124. }
  125. // Replace db_url with DB name from target. updatedb will later append a DBTNG compatible version.
  126. function drush_upgrade_fix_db_url($target_alias, $settings_destination) {
  127. $old_url = $GLOBALS['db_url'];
  128. if (is_array($old_url)) {
  129. $old_url = $old_url['default'];
  130. }
  131. $target_alias_databases = sitealias_get_databases_from_record($target_alias);
  132. $new_url = substr($old_url, 0, strrpos(trim($old_url), '/')) . '/'. $target_alias_databases['default']['default']['database'];
  133. $append = "\n# Added by drush site-upgrade.";
  134. if (drush_drupal_major_version() <= 6) {
  135. $append .= "\n" . '$db_url = \'' . $new_url . '\';';
  136. }
  137. else {
  138. $databases = $GLOBALS['databases'];
  139. $databases['default']['default']['database'] = $target_alias_databases['default']['default']['database'];
  140. $append .= "\n" . '$databases = ' . var_export($databases, TRUE) . ';';
  141. }
  142. drush_op('file_put_contents', $settings_destination, $append, FILE_APPEND);
  143. }