upgrade.drush.inc

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

Refine your Drupal major version upgrade.

Functions

Namesort ascending Description
_drush_upgrade_restore_preserved_modules Restore modules that were preserved by _drush_upgrade_preserve_modules.
_drush_upgrade_preserve_modules Preserve existing modules. Move them out of the way prior to updatedb of Drupal core. We will move them back afterwards.
upgrade_drush_help Implement hook_drush_help().
upgrade_drush_command Implement hook_drush_command().
drush_upgrade_site_upgrade_validate Do some sanity checks to make sure that we are ready to perform an upgrade, and that the command is being called with reasonable-looking parameters.
drush_upgrade_site_upgrade_rollback Rollback function: restore our modules if updatedb fails.
drush_upgrade_site_upgrade_prepare Prepare to upgrade; the first step is to disable all contrib modules.
drush_upgrade_site_upgrade_modules Upgrade all of the contrib modules of the site being upgraded.
drush_upgrade_site_upgrade Main command hook for site-upgrade.
drush_upgrade_project_map http://drupal.org/node/895314 lists projects that are now in core; it also has a list of projects that require special handling. Keep a record here too.
drush_upgrade_fix_db_url Replace db_url with DB name from target. updatedb will later append a DBTNG compatible version.
drush_upgrade_download_drupal Download the upgraded version of Drupal for site-upgrade.
drush_upgrade_copy_settings Copy the settings.php file from the source site to the target site, and fix it up so that it will have its own database settings.

File

commands/core/upgrade.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Refine your Drupal major version upgrade.
  5. */
  6. /**
  7. * Implement hook_drush_command().
  8. */
  9. function upgrade_drush_command() {
  10. $items = array();
  11. $items['site-upgrade'] = array(
  12. 'description' => 'Run a major version upgrade for Drupal (e.g. Drupal 6 to Drupal 7). A copy of the site is made, and then upgraded; the source site is not changed.',
  13. 'drupal dependencies' => array('update'),
  14. 'drush dependencies' => array('sql', 'pm', 'core'),
  15. 'core' => array(6), // Add 7 once drush supports 7 -> 8 upgrades.
  16. 'arguments' => array(
  17. 'target' => "The name of a sitealias, which points to the destination site. 'root' and 'uri' keys are required; db-url is recommended. See examples/aliases.drushrc.php for more information about creating a site alias.",
  18. ),
  19. 'examples' => array(
  20. 'drush site-upgrade @onward' => 'Upgrade from the current site to the site specified by @onward alias.',
  21. ),
  22. 'options' => array(
  23. 'structure-tables-key' => 'A key in the structure-tables array. @see example.drushrc.php. Defaults to \'common\'.',
  24. 'source-dump' => 'Path to dump file. Medium or large sized sites should set this. Optional; default is to create a temporary file.',
  25. 'db-su' => 'DB username to use when dropping and creating the target database. Optional.',
  26. 'db-su-pw' => 'DB password to use when dropping and creating the target database. Optional.',
  27. 'no-cache' => 'Transfer a fresh database from source site. Otherwise, DB dump is re-used for 24 hours.',
  28. 'core-only' => 'Stop after upgrading Drupal core; do not download and enable new versions of the site\'s modules.',
  29. 'force-sites-default' => 'Forces settings.php to be written in sites/default folder, even if source settings.php is not.',
  30. 'replace' => 'Replace target if it already exists. Default is to prompt.',
  31. 'reuse' => 'Reuse target if it already exists. Default is to prompt.',
  32. 'uninstall' => 'Comma-separated list of modules to uninstall in the target database prior to upgrade. n.b. The source site is not affected.',
  33. ),
  34. 'aliases' => array('sup'),
  35. 'topics' => array('docs-aliases'),
  36. );
  37. $items['site-upgrade-prepare'] = array(
  38. 'description' => 'Prior to running updatedb on Drupal core, disable all contrib modules and uninstall any module the user specified should be uninstalled. Called automatically by site-upgrade.',
  39. 'hidden' => TRUE,
  40. 'arguments' => array(
  41. 'modules' => 'The modules to disable.',
  42. ),
  43. 'options' => array(
  44. 'uninstall' => 'Comma-separated list of modules to uninstall prior to upgrade.',
  45. ),
  46. );
  47. $items['site-upgrade-modules'] = array(
  48. 'description' => 'Download, enable, and run updatedb on all contrib modules after an upgrade. Called automatically by site-upgrade.',
  49. 'hidden' => TRUE,
  50. 'arguments' => array(
  51. 'modules' => 'The modules to download and enable.',
  52. ),
  53. 'options' => array(
  54. 'projects' => 'The projects that must be downloaded to provide the specified module list.',
  55. ),
  56. );
  57. return $items;
  58. }
  59. /**
  60. * Implement hook_drush_help().
  61. */
  62. function upgrade_drush_help($section) {
  63. switch ($section) {
  64. case 'drush:site-upgrade':
  65. 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. 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. Run this command from within your source site (D6). Note that this command uses pm-download and sql-sync internally so most options for those commands are valid here too.");
  66. }
  67. }
  68. /**
  69. * Do some sanity checks to make sure that we are ready to perform an upgrade, and
  70. * that the command is being called with reasonable-looking parameters.
  71. */
  72. function drush_upgrade_site_upgrade_validate($target_key = NULL) {
  73. if (empty($target_key)) {
  74. return drush_set_error('DRUSH_UPGRADE_NO_TARGET', dt('Missing argument: target'));
  75. }
  76. if (!$target_alias = drush_sitealias_get_record($target_key)) {
  77. return drush_set_error('DRUSH_UPGRADE_NO_TARGET', dt('Site alias not found: @target-key. See example.drushrc.php.', array('@target-key' => $target_key)));
  78. }
  79. if (!file_exists(dirname($target_alias['root']))) {
  80. drush_set_error('DRUSH_UPGRADE_NO_TARGET', dt('Parent directory for site alias root not found: @root; this folder must exist before running site-upgrade. See example.drushrc.php.', array('@root' => dirname($target_alias['root']))));
  81. }
  82. if (realpath($target_alias['root']) == realpath(DRUPAL_ROOT)) {
  83. drush_set_error('DRUSH_UPGRADE_NO_TARGET', dt('Target site alias must have a different Drupal root directory than the source site. Both are at @root.', array('@root' => $target_alias['root'])));
  84. }
  85. }
  86. /**
  87. * Main command hook for site-upgrade.
  88. *
  89. * This runs bootstrapped to the SOURCE site.
  90. */
  91. function drush_upgrade_site_upgrade($target_key) {
  92. // Presume we are ready to go (n.b. some checks already performed in 'validate')
  93. $ready_to_upgrade = TRUE;
  94. $result = TRUE;
  95. // PREPARE: Find the target version and determine the contrib projects and enabled modules installed.
  96. $source_version = drush_drupal_major_version();
  97. $target_version = $source_version + 1;
  98. $target_alias = drush_sitealias_get_record($target_key);
  99. if (empty($target_alias)) {
  100. return drush_set_error('DRUSH_UPGRADE_NO_TARGET', dt("Could not find target site for upgrade: !target", array("!target" => $target_key)));
  101. }
  102. $destination_core = $target_alias['root'];
  103. $destination_conf_path = conf_path();
  104. if (drush_get_option('force-sites-default')) {
  105. $destination_conf_path = 'sites/default';
  106. }
  107. // Get a list of enabled contrib extensions.
  108. $values = drush_invoke_process_args('pm-list', array(), array('status'=>'enabled','no-core'=>TRUE, '#integrate' => FALSE, '#override-simulated' => TRUE));
  109. if ($values['error_status'] != 0) return FALSE;
  110. $contrib_extensions = array_keys($values['object']);
  111. // Get a list of enabled core extensions.
  112. $values = drush_invoke_process_args('pm-list', array(), array('status'=>'enabled','core'=>TRUE, '#integrate' => FALSE, '#override-simulated' => TRUE));
  113. if ($values['error_status'] != 0) return FALSE;
  114. $core_extensions = array_keys($values['object']);
  115. // Make a list of modules that are not available to be enabled in the target
  116. // version of Drupal, either because they have not been ported yet, or because
  117. // they have been rolled into core.
  118. $unavailable_extensions = array();
  119. // Get the list of modules the user would like to uninstall (if any).
  120. $uninstall_extensions = drush_get_option('uninstall', '');
  121. if ($uninstall_extensions == "all") {
  122. $uninstall_extensions = $contrib_extensions;
  123. }
  124. else {
  125. $uninstall_extensions = explode(',', $uninstall_extensions);
  126. }
  127. // WARN: Tell the user about any special situations that might exist with contrib modules.
  128. $project_download_list = array();
  129. $extension_info = drush_pm_get_extensions();
  130. if (!empty($contrib_extensions) && !drush_get_option('core-only')) {
  131. // Make a list of all of the extensions to download. We will
  132. // download everything in the contrib extension list, but we
  133. // will skip projects that already exist.
  134. $special_projects = drush_upgrade_project_map($target_version);
  135. $in_core = array();
  136. $special_warning = array();
  137. foreach ($contrib_extensions as $extension_name) {
  138. // Only check extensions that are NOT in our uninstall list.
  139. if (!in_array($extension_name, $uninstall_extensions) && array_key_exists($extension_name, $extension_info)) {
  140. $project = $extension_info[$extension_name]->info['project'];
  141. // Check our lookup table to see if a project has been renamed.
  142. if (array_key_exists($project, $special_projects['project-remap'])) {
  143. $project = $special_projects['project-remap'][$project];
  144. }
  145. // If the module has been rolled into core in the next major release of
  146. // Drupal, then we do not need to download it. Add it to an array for
  147. // reporting purposes.
  148. if ((in_array($project, $special_projects['projects-in-core'])) || (in_array($extension_name, $special_projects['modules-in-core']))) {
  149. $in_core[$extension_name] = $extension_name;
  150. // Might some of these need to be enabled?
  151. $unavailable_extensions[] = $extension_name;
  152. }
  153. elseif (($extension_info[$extension_name]->type == 'module') && !is_dir($destination_core . '/sites/all/modules/' . $project) && !is_dir($destination_core . '/' . $destination_conf_path . '/modules/' . $project)) {
  154. $project_download_list[$project][] = $extension_name;
  155. }
  156. // If there is a special warning about a project, then add it
  157. // to the warning list for later reporting.
  158. if (array_key_exists($project . ':' . $extension_name, $special_projects['warning'])) {
  159. $special_warning[$project] = $special_projects['warning'][$project . ':' . $extension_name];
  160. }
  161. elseif ((array_key_exists($project, $special_projects['warning'])) && (!array_key_exists($project, $special_warning))) {
  162. $special_warning[$project] = $special_projects['warning'][$project];
  163. }
  164. }
  165. }
  166. // Consider each project from the contrib extensions and check with PM to see if there is
  167. // a recommended release available to download. If there is NO release available, then
  168. // we are not ready to upgrade (but still can, without that project); if there is no RECOMMENDED
  169. // release, then we might not be ready to upgrade (but still can, with a non-recommended release).
  170. if (!empty($project_download_list)) {
  171. $result = drush_invoke_process('@none', 'pm-releases', array_keys($project_download_list), array('default-major' => $target_version, '#integrate' => FALSE, '#override-simulated' => TRUE));
  172. $project_releases = $result['object'];
  173. foreach ($project_download_list as $project => $extension_list) {
  174. if (!array_key_exists($project, $project_releases)) {
  175. drush_log(dt('The project !project has no releases in version !version', array('!project' => $project, '!version' => $target_version)), 'warning');
  176. $unavailable_extensions = array_merge($unavailable_extensions, $extension_list);
  177. $ready_to_upgrade = FALSE;
  178. }
  179. else {
  180. if (empty($project_releases[$project]['recommended'])) {
  181. drush_log(dt('The project !project has no recommended release in version !version', array('!project' => $project, '!version' => $target_version)), 'warning');
  182. $ready_to_upgrade = 'maybe';
  183. }
  184. }
  185. }
  186. }
  187. // Print out some messages about projects that migrated to core, or modules that will require special processing.
  188. if (!empty($in_core)) {
  189. drush_log(dt('The following contrib modules were enabled in your Drupal site, but are now standard in core: !in_core. These modules may need to be reconfigured after the upgrade is complete.', array('!in_core' => implode(', ', $in_core))), 'ok');
  190. }
  191. foreach ($special_warning as $project => $warning) {
  192. if ($warning === TRUE) {
  193. $warning = 'Please see !project_page and !source for more information on how to do this.';
  194. }
  195. if ($warning === FALSE) {
  196. $warning = 'So far there is no indication of when a migration path will be provided. Check !project_page for updates.';
  197. $ready_to_upgrade = 'maybe';
  198. }
  199. drush_log(dt("You are using the project !project, which requires data migration or other special processing. $warning", array('!project' => $project, '!project_page' => 'http://drupal.org/project/' . $project, '!source' => $special_projects['source'])), 'warning');
  200. }
  201. }
  202. // CONFIRM: Ask the user before overwriting an exsiting site, and ask if an upgrade is really decided if the site is not ready yet.
  203. // Summarize whether or not there is a good chance that the site can be upgraded.
  204. if ($ready_to_upgrade !== TRUE) {
  205. drush_log(dt("Based on the contrib modules enabled in this site, it is possible that the site-upgrade command might fail. See warnings above."), (($ready_to_upgrade === FALSE) ? 'warning' : 'notice'));
  206. }
  207. // Check to see what we should do if the target Drupal folder already exists.
  208. $options = array(
  209. 'replace' => dt("Delete the existing site and start over."),
  210. 'reuse' => dt("Re-use the existing code, re-import the database from the source site and run updatedb again."),
  211. );
  212. $selection = NULL;
  213. foreach ($options as $option => $msg) {
  214. if (drush_get_option($option, FALSE)) {
  215. $selection = $option;
  216. }
  217. }
  218. if (!isset($selection) && (file_exists($destination_core))) {
  219. $selection = drush_choice($options, dt("Drupal site already exists at !root. Would you like to:", array('!root' => $destination_core)));
  220. if (!$selection) {
  221. return drush_user_abort();
  222. }
  223. }
  224. elseif($ready_to_upgrade !== TRUE) {
  225. if (!drush_confirm('Would you like to continue anyway?')) {
  226. return drush_user_abort();
  227. }
  228. }
  229. // User has already been prompted; skip further confirms.
  230. drush_set_context('DRUSH_AFFIRMATIVE', TRUE);
  231. // We need to know where our destination settings file is regardless of which
  232. // code path we take; therefore, we will precompute it here.
  233. $settings_destination = $destination_core . '/' . $destination_conf_path . '/settings.php';
  234. // STEP 1: Download the next major version of Drupal.
  235. if (($selection == 'replace') || (!is_dir($destination_core))) {
  236. drush_upgrade_download_drupal($target_version, $destination_core);
  237. if (drush_get_error()) return FALSE; // Early exit if we see an error.
  238. drush_upgrade_copy_settings($target_alias, $settings_destination);
  239. }
  240. else {
  241. // Move sites/all/modules and $conf_path()/modules out of the way
  242. // so that updatedb can be run on core only.
  243. if (_drush_upgrade_preserve_modules($destination_core) === FALSE) {
  244. return FALSE;
  245. }
  246. }
  247. // Copy source database to target database. The source DB is not changed.
  248. // Always set 'common' at minimum. Sites that want other can create other key in drushrc.php.
  249. if (!drush_get_option('structure-tables-key')) {
  250. drush_set_option('structure-tables-key', 'common');
  251. }
  252. // Always blow away the target database so we start fresh.
  253. // We still have DRUSH_AFFIRMATIVE set from above, so this will not prompt.
  254. drush_set_option('create-db', TRUE);
  255. drush_include(DRUSH_BASE_PATH . '/commands/sql', 'sync.sql');
  256. drush_invoke('sql-sync', '@self', $target_key);
  257. if (drush_get_error()) return FALSE; // Early exit if we see an error.
  258. if (!empty($contrib_extensions)) {
  259. $target_alias_databases = sitealias_get_databases_from_record($target_alias);
  260. $modify_site_conf_path = NULL;
  261. // Make an alias record that uses the CODE from @self and the DATABASE from $target.
  262. // Since we just did an sql-sync from @self to @target, we can use this hybrid specification
  263. // to do manipulations on the target database before runing updatedb. In brief, we are going
  264. // to disable all contrib modules to prevent problems with updatedb.
  265. $modify_site = array (
  266. 'root' => DRUPAL_ROOT,
  267. 'uri' => $target_alias_databases['default']['default']['database'],
  268. );
  269. if (!drush_get_context('DRUSH_SIMULATE')) {
  270. // In theory, if the sql-sync worked, this should never be empty.
  271. if (empty($modify_site['uri'])) {
  272. return drush_set_error('DRUSH_UPGRADE_DATABASE_SPEC_UNKNOWN', dt('Failed to look up database spec for @target', array('@target' => $target_key)));
  273. }
  274. $modify_site_conf_path = dirname(conf_path()) . '/' . $modify_site['uri'];
  275. $modify_site_settings = $modify_site_conf_path . '/settings.php';
  276. drush_log('set up a fake site by copying ' . $settings_destination . ' to ' . $modify_site_settings);
  277. if (!file_exists($modify_site_settings)) {
  278. if ((drush_mkdir($modify_site_conf_path) === FALSE) || drush_op('copy', $settings_destination, $modify_site_settings) !== TRUE) {
  279. return drush_set_error('DRUSH_UPGRADE_COULD_NOT_DISABLE', dt("Could not create a temporary multisite "));
  280. }
  281. }
  282. }
  283. $result = (drush_invoke_sitealias_args($modify_site, 'site-upgrade-prepare', $contrib_extensions, array('uninstall' => implode(',', $uninstall_extensions), 'yes' => TRUE, '#interactive' => TRUE)) == 0);
  284. // Delete the temporary site now that we're done with it.
  285. if (isset($modify_site_conf_path)) {
  286. drush_delete_dir($modify_site_conf_path);
  287. }
  288. if ($result === FALSE) return FALSE;
  289. }
  290. // STEP 2: Call updatedb for Drupal core.
  291. // Run update.php in a subshell. It is run on @target site whereas this request was on @self.
  292. drush_log(dt('About to perform updatedb for Drupal core on !target', array('!target' => $target_key)), 'ok');
  293. // When we call drush_invoke_sitealias_args in #interactive mode, the result code comes from drush_op_system, where 0 == success.
  294. $result = drush_invoke_sitealias_args($target_alias, 'updatedb', array(), array('yes' => TRUE, '#interactive' => TRUE)) == 0;
  295. if ($result === FALSE) {
  296. return drush_set_error('DRUSH_DRUPAL_CORE_UPGRADE_FAILED', dt("The call to updatedb failed for Drupal core. This may be caused by a contrib module that is not yet ready for upgrade. Try running site-upgrade again with '--uninstall={module list}' to remove all contrib extensions prior to upgrade. Add modules back in until the problematic one is isolated. Please report problems in the issue queue of the module that is causing problems."));
  297. }
  298. drush_log(dt('updatedb complete for Drupal core'), 'ok');
  299. // If we moved our modules out of the way, bring them back now.
  300. _drush_upgrade_restore_preserved_modules();
  301. // STEP 3: Download and re-enable the contrib modules.
  302. if (!empty($contrib_extensions) && !drush_get_option('core-only')) {
  303. $options = array('#interactive' => TRUE);
  304. if (!empty($project_download_list)) {
  305. $projects = implode(',', array_keys($project_download_list));
  306. $options['projects'] = $projects;
  307. }
  308. // If a module changed name, then rename it prior to calling pm-enable.
  309. foreach ($contrib_extensions as $extension_name) {
  310. if (array_key_exists($extension_name, $special_projects['module-remap'])) {
  311. $unavailable_extensions[] = $extension_name;
  312. $contrib_extensions[] = $special_projects['module-remap'][$extension_name];
  313. }
  314. }
  315. // Redispatch to site-upgrade-modules command, so that we will be
  316. // bootstrapped to the target site.
  317. $result = (drush_invoke_sitealias_args($target_alias, 'site-upgrade-modules', array_merge($core_extensions, array_diff($contrib_extensions, $unavailable_extensions, $uninstall_extensions)), $options) == 0);
  318. }
  319. return $result;
  320. }
  321. /**
  322. * http://drupal.org/node/895314 lists projects that are now in
  323. * core; it also has a list of projects that require special handling.
  324. * Keep a record here too.
  325. *
  326. * @param $target_version
  327. * The version of Drupal being upgraded to.
  328. * @return @array
  329. * - source URL to the page where more information about this upgrade can be found.
  330. * - target-version The version of Drupal being upgraded to.
  331. * - projects-in-core An array containing projects that were once contrib that are now in core.
  332. * - modules-in-core An array containing modules that were once in contrib that are now in core.
  333. * Use 'modules-in-core' in place of 'projects-in-core' only if there is a
  334. * project where only some of its subcomponents were moved to core.
  335. * - warning An array of warning messages to display to the user related to this upgrade.
  336. * The key should be the name of the project that the warning applies to.
  337. * use 'project:module' as the key if the warning only applies when the given
  338. * module in the project is enabled; otherwise, the warning will be shown whenever
  339. * any module in the specific project is enabled. The value can either be the
  340. * warning string to display, or TRUE to tell the user to see the project page
  341. * for information on the migration path, or FALSE to tell the user that there
  342. * currently is no migration path for the given project.
  343. */
  344. function drush_upgrade_project_map($target_version) {
  345. $result = array(
  346. 'source' => '',
  347. 'target-version' => $target_version,
  348. 'projects-in-core' => array(),
  349. 'modules-in-core' => array(),
  350. 'warning' => array(),
  351. );
  352. if ($target_version == 7) {
  353. $result = array(
  354. 'source' => 'http://drupal.org/node/895314',
  355. 'projects-in-core' => array(
  356. 'abssrc',
  357. 'admin_hover',
  358. 'adminrole',
  359. 'ahah_helper',
  360. 'autoload',
  361. 'automaticmenu',
  362. 'automenu',
  363. 'auto_menutitle',
  364. 'block_edit',
  365. 'block_node_visibility',
  366. 'blocks404',
  367. 'canonical_url',
  368. 'checkbox_validate',
  369. 'comment_display',
  370. 'config_perms',
  371. 'ctm',
  372. 'dbtng',
  373. 'documentation',
  374. 'drupal_queue',
  375. 'edit_term',
  376. 'elements',
  377. 'element_themehook',
  378. 'filefield',
  379. 'filter_default',
  380. 'hook_file',
  381. 'imageapi',
  382. 'imagecache',
  383. 'imagefield',
  384. 'input_format_permissions',
  385. 'jq',
  386. 'jqp',
  387. 'jquery_cookie',
  388. 'jquery_form_update',
  389. 'jquery_ui',
  390. 'jsalter',
  391. 'login_security',
  392. 'menuless_nodetype',
  393. 'nodepreview_by_type',
  394. 'parallel',
  395. 'permissions_api',
  396. 'phpass',
  397. 'plugin_manager',
  398. 'plugins',
  399. 'poormanscron',
  400. 'preview',
  401. 'rdf',
  402. 'shortlink',
  403. 'simplecdn',
  404. 'simpletest',
  405. 'storage_api',
  406. 'tar',
  407. 'taxidermy',
  408. 'tinymce_ahah',
  409. 'tinymce_dragdrop',
  410. 'transaction',
  411. 'upload_element',
  412. 'upload_preview',
  413. 'url_alter',
  414. 'user_cancellation',
  415. 'user_default_filter',
  416. 'user_delete',
  417. 'vertical_tabs',
  418. 'view_unpublished',
  419. 'vocabperms',
  420. 'wysiwygcck',
  421. ),
  422. 'modules-in-core' => array(
  423. // 'date' project is still contrib, but date_timezone, one of its modules, has moved to core. See http://drupal.org/node/868028.
  424. 'date_timezone',
  425. ),
  426. 'project-remap' => array(),
  427. 'module-remap' => array(
  428. 'content' => 'cck',
  429. ),
  430. 'warning' => array(
  431. 'token' => dt('In Drupal 7, the contrib token module handles UI, as well as field and profile tokens; all other functionality has been migrated to core.'),
  432. 'cck' => TRUE,
  433. 'content_taxonomy' => dt('The project page for this module (!project_page) says that an upgrade path will be provided for this module at a future time.'),
  434. 'date:date_api' => dt("The d6 version of the date_api module in the date project defines a table called date_formats, which is also defined by system/system.install schema in d7. If this problem has not been fixed yet, then the updatedb function will fail, and it will not be possible to upgrade to d7. If this happens, disable and uninstall the date_api module before running site-upgrade (i.e. add '--uninstall=date_api' to site-upgrade call). See http://drupal.org/node/1013034."),
  435. 'field_taxonomy' => FALSE,
  436. 'filefield' => dt('Data migration for this module will be provided by the Content Migrate submodule of cck. Enable content_migrate after upgrading; see http://drupal.org/node/781088.'),
  437. 'imagefield' => dt('Data migration for this module will be provided by the Content Migrate submodule of cck. Enable content_migrate after upgrading; see http://drupal.org/node/781088.'),
  438. 'taxonomy_delegate' => FALSE,
  439. 'taxonomy_intro' => FALSE,
  440. 'term_fields' => FALSE,
  441. ),
  442. );
  443. }
  444. drush_command_invoke_all_ref('drush_upgrade_project_map_alter', $result);
  445. return $result;
  446. }
  447. /**
  448. * Prepare to upgrade; the first step is to disable all contrib modules.
  449. */
  450. function drush_upgrade_site_upgrade_prepare() {
  451. $contrib_extensions = func_get_args();
  452. $uninstall_extensions = explode(',', drush_get_option('uninstall', ''));
  453. // Set theme back to garland per Upgrade.txt.
  454. variable_set('theme_default', 'garland');
  455. // http://drupal.org/node/724102 recommends using "seven" as your admin theme. Don't switch it to garland if it is already seven.
  456. $admin_theme = variable_get('admin_theme', NULL);
  457. if ($admin_theme != "seven") {
  458. variable_set('admin_theme', 'garland');
  459. }
  460. else {
  461. drush_log(dt("Admin theme is already set to 'seven'."), 'ok');
  462. }
  463. // Disable all contrib modules per Upgrade.txt.
  464. drush_log(dt("Disabling !list", array('!list' => implode(", ", $contrib_extensions))), 'ok');
  465. call_user_func_array('drush_pm_disable', $contrib_extensions);
  466. if (drush_get_error()) return FALSE; // Early exit if we see an error.
  467. // Uninstall any modules specified via the --uninstall flag.
  468. if (!empty($uninstall_extensions)) {
  469. drush_log(dt("Uninstalling !list", array('!list' => implode(", ", $uninstall_extensions))), 'ok');
  470. call_user_func_array('drush_pm_uninstall', $uninstall_extensions);
  471. if (drush_get_error()) return FALSE; // Early exit if we see an error.
  472. }
  473. }
  474. /**
  475. * Upgrade all of the contrib modules of the site being upgraded.
  476. *
  477. * This runs bootstrapped to the TARGET site, after the new version
  478. * of Drupal has been downloaded, and after updatedb has been run
  479. * for Drupal core.
  480. */
  481. function drush_upgrade_site_upgrade_modules() {
  482. $extensions_to_enable = func_get_args();
  483. $project_download_list = drush_get_option('projects', '');
  484. if (!empty($project_download_list)) {
  485. // Download our contrib extensions.
  486. drush_log(dt('Download projects: !projects', array('!projects' => $project_download_list)), 'ok');
  487. drush_set_option('destination', NULL);
  488. // User has already been prompted; if there is no recommended release,
  489. // we will just take the most recent.
  490. drush_set_option('choice', '1');
  491. call_user_func_array('drush_pm_download', explode(',', $project_download_list));
  492. }
  493. // Run updatedb to update all of the contrib extensions.
  494. drush_log(dt('About to perform updatedb for extensions'), 'ok');
  495. $result = drush_invoke_process_args('updatedb', array(), array('yes' => TRUE, '#interactive' => TRUE));
  496. if ($result === FALSE) {
  497. return drush_set_error('DRUSH_DRUPAL_CONTRIB_UPGRADE_FAILED', dt("The call to updatedb failed for the enabled contrib modules. Try running site-upgrade again with '--uninstall={module list}' to remove all contrib extensions prior to upgrade. Add modules back in until the problematic one is isolated. Please report problems in the issue queue of the module that is causing problems."));
  498. }
  499. drush_log(dt('updatedb complete for extensions'), 'ok');
  500. // Finally, enable the modules that site-upgrade previously disabled.
  501. // We will set the option --resolve-dependencies to pick up new modules
  502. // that may now be required; for example, views-7.x picked up a dependency
  503. // on ctools that views-6.x did not have. We also set DRUSH_AFFIRMATIVE,
  504. // so everything from here on out will be processed with --yes.
  505. drush_set_option('resolve-dependencies', TRUE);
  506. drush_set_context('DRUSH_AFFIRMATIVE', TRUE);
  507. $result = drush_invoke_args('pm-enable', $extensions_to_enable);
  508. return $result;
  509. }
  510. /**
  511. * Download the upgraded version of Drupal for site-upgrade.
  512. */
  513. function drush_upgrade_download_drupal($target_version, $destination_core) {
  514. // Fetch target core and place as per target alias root.
  515. drush_set_option('destination', dirname($destination_core));
  516. drush_set_option('drupal-project-rename', basename($destination_core));
  517. // No need for version control in this command.
  518. drush_set_option('version-control', 'backup');
  519. if (drush_get_context('DRUSH_SIMULATE')) {
  520. drush_log(dt("Simulated download of drupal-!version", array('!version' => $target_version)));
  521. }
  522. else {
  523. drush_pm_download('drupal-' . $target_version);
  524. if (drush_get_error()) return FALSE; // Early exit if we see an error.
  525. // Check and see if there is a Drupal site at the target.
  526. if (!file_exists($destination_core . '/includes/bootstrap.inc')) {
  527. return drush_set_error('DRUSH_UPGRADE_NO_DRUPAL', dt('Drupal could not be downloaded to the target directory, @root. Move existing content out of the way first.', array('@root' => $target_alias['root'])));
  528. }
  529. }
  530. }
  531. /**
  532. * Copy the settings.php file from the source site to the target site,
  533. * and fix it up so that it will have its own database settings.
  534. */
  535. function drush_upgrade_copy_settings(&$target_alias, $settings_destination) {
  536. $settings_destination_folder = dirname($settings_destination);
  537. // Create sites subdirectory in target if needed.
  538. $settings_source = conf_path() . '/settings.php';
  539. if (!file_exists($settings_destination_folder)) {
  540. if (!drush_op('mkdir', $settings_destination_folder) && !drush_get_context('DRUSH_SIMULATE')) {
  541. return drush_set_error('DRUSH_UPGRADE_MKDIR_FAILED', dt('Failed to create directory @settings_destination', array('@settings_destination' => $settings_destination_folder)));
  542. }
  543. }
  544. // Copy settings.php to target.
  545. if (!file_exists($settings_destination)) {
  546. if (!drush_op('copy', $settings_source, $settings_destination) && !drush_get_context('DRUSH_SIMULATE')) {
  547. return drush_set_error('DRUSH_UPGRADE_COPY_FAILED', dt('Failed to copy @source to @dest', array('@source' => $settings_source, 'dest' => $settings_destination)));
  548. }
  549. }
  550. // Append new $db_url with new DB name in target's settings.php.
  551. return drush_upgrade_fix_db_url($target_alias, $settings_destination);
  552. }
  553. /**
  554. * Replace db_url with DB name from target. updatedb will later append a DBTNG compatible version.
  555. */
  556. function drush_upgrade_fix_db_url(&$target_alias, $settings_destination) {
  557. $old_url = $GLOBALS['db_url'];
  558. if (is_array($old_url)) {
  559. $old_url = $old_url['default'];
  560. }
  561. $old_databases = $GLOBALS['databases'];
  562. if (empty($old_databases)) {
  563. $old_databases = drush_sitealias_convert_db_from_db_url($old_url);
  564. }
  565. $target_alias_databases = sitealias_get_databases_from_record($target_alias);
  566. $database_name = $target_alias_databases['default']['default']['database'];
  567. if (empty($database_name)) {
  568. $database_name = str_replace("@", "", $target_alias['name']) . "db";
  569. drush_log(dt("No database name specified; defaulting to !dbname", array("!dbname" => $database_name)), 'notice');
  570. }
  571. $append = "\n# Added by drush site-upgrade.";
  572. if (drush_drupal_major_version() <= 6) {
  573. $new_url = substr($old_url, 0, strrpos(trim($old_url), '/')) . '/'. $database_name;
  574. $append .= "\n" . '$db_url = \'' . $new_url . '\';';
  575. $databases = drush_sitealias_convert_db_from_db_url($new_url);
  576. }
  577. else {
  578. $databases = $GLOBALS['databases'];
  579. $databases['default']['default']['database'] = $target_alias_databases['default']['default']['database'];
  580. $append .= "\n" . '$databases = ' . var_export($databases, TRUE) . ';';
  581. }
  582. // Caching the database record in the alias record allows sql-sync to work
  583. // before updatedb is called. updatedb is what converts from a db_url to a
  584. // DBTNG array; this conversion is required by sql-sync.
  585. drush_sitealias_cache_db_settings($target_alias, $databases);
  586. // Also append the new configuration options to the end of settings.php.
  587. drush_op('file_put_contents', $settings_destination, $append, FILE_APPEND);
  588. }
  589. /**
  590. * Rollback function: restore our modules if updatedb fails.
  591. */
  592. function drush_upgrade_site_upgrade_rollback($target_key) {
  593. _drush_upgrade_restore_preserved_modules();
  594. }
  595. /**
  596. * Preserve existing modules. Move them out of the way prior
  597. * to updatedb of Drupal core. We will move them back afterwards.
  598. */
  599. function _drush_upgrade_preserve_modules($destination_core) {
  600. $modules_preserve['root'] = $destination_core;
  601. $modules_preserve['list'] = array();
  602. $moduledir_list = array(
  603. 'sites_all_modules' => $destination_core . '/sites/all/modules',
  604. 'sites_conf_path_modules' => $destination_core . conf_path() . '/modules',
  605. );
  606. foreach ($moduledir_list as $moduledir_name => $moduledir) {
  607. if (is_dir($moduledir)) {
  608. $preserved_moduledir = drush_tempnam($moduledir_name, dirname($moduledir));
  609. $result = drush_move_dir($moduledir, $preserved_moduledir, TRUE);
  610. if ($result) {
  611. $modules_preserve['list'][$moduledir] = $preserved_moduledir;
  612. drush_log(dt('Move !src to !dest prior to updatedb on Drupal core.', array('!src' => $moduledir, '!dest' => $preserved_moduledir)), 'ok');
  613. }
  614. else {
  615. return drush_set_error('DRUSH_MODULE_PRESERVE_FAILED', dt('Failed to move !src to !dest.', array('!src' => $moduledir, '!dest' => $preserved_moduledir)));
  616. }
  617. }
  618. }
  619. drush_set_context('DRUSH_MODULES_PRESERVE', $modules_preserve);
  620. return TRUE;
  621. }
  622. /**
  623. * Restore modules that were preserved by _drush_upgrade_preserve_modules.
  624. */
  625. function _drush_upgrade_restore_preserved_modules() {
  626. $modules_preserve = drush_get_context('DRUSH_MODULES_PRESERVE', array());
  627. if (!empty($modules_preserve) && array_key_exists('list', $modules_preserve)) {
  628. foreach ($modules_preserve['list'] as $moduledir => $preserved_moduledir) {
  629. drush_move_dir($preserved_moduledir, $moduledir, TRUE);
  630. }
  631. }
  632. drush_set_context('DRUSH_MODULES_PRESERVE', array());
  633. }