Drupal investigation

migrate_upgrade.drush.inc 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @file
  4. * Command-line tools to aid performing and developing upgrade migrations.
  5. */
  6. use Drupal\migrate_upgrade\MigrateUpgradeDrushRunner;
  7. /**
  8. * Implements hook_drush_command().
  9. */
  10. function migrate_upgrade_drush_command() {
  11. $items['migrate-upgrade'] = [
  12. 'description' => 'Perform one or more upgrade processes.',
  13. 'options' => [
  14. 'legacy-db-url' => 'A Drupal 6 style database URL. Required if you do not set legacy-db-key.',
  15. 'legacy-db-key' => 'A database connection key from settings.php. Use as an alternative to legacy-db-url',
  16. 'legacy-db-prefix' => 'Prefix of the legacy Drupal installation.',
  17. 'legacy-root' => 'Site address or root of the legacy Drupal installation',
  18. 'configure-only' => 'Set up the appropriate upgrade processes but do not perform them',
  19. 'migration-prefix' => 'With configure-only, a prefix to apply to generated migration ids. Defaults to \'upgrade_\'',
  20. ],
  21. 'examples' => [
  22. 'migrate-upgrade --legacy-db-url=\'mysql://root:pass@127.0.0.1/d6\'' => 'Upgrade a Drupal 6 database to Drupal 8',
  23. 'migrate-upgrade --legacy-db-key=\'drupal_7\'' => 'Upgrade Drupal 7 database where the connection to Drupal 7 has already been created in settings.php ($databases[\'drupal_7\'])',
  24. 'migrate-upgrade --legacy-db-url=\'mysql://root:pass@127.0.0.1/d7\' --configure-only --migration-prefix=d7_custom_' => 'Generate migrations for a custom migration from Drupal 7 to Drupal 8',
  25. ],
  26. 'drupal dependencies' => ['migrate_upgrade'],
  27. ];
  28. $items['migrate-upgrade-rollback'] = [
  29. 'description' => 'Rolls back and removes upgrade migrations.',
  30. 'examples' => [
  31. 'migrate-upgrade-rollback' => 'Rolls back a previously-run upgrade',
  32. ],
  33. 'drupal dependencies' => ['migrate_upgrade'],
  34. ];
  35. return $items;
  36. }
  37. /**
  38. * Execute the upgrade command, configuring the necessary migrations and
  39. * optionally perform the imports.
  40. */
  41. function drush_migrate_upgrade() {
  42. $runner = new MigrateUpgradeDrushRunner();
  43. try {
  44. $runner->configure();
  45. if (drush_get_option('configure-only')) {
  46. $runner->export();
  47. }
  48. else {
  49. $runner->import();
  50. \Drupal::state()->set('migrate_drupal_ui.performed', REQUEST_TIME);
  51. }
  52. // Remove the global database state.
  53. \Drupal::state()->delete('migrate.fallback_state_key');
  54. }
  55. catch (\Exception $e) {
  56. drush_log($e->getMessage(), 'error');
  57. }
  58. }
  59. /**
  60. * Rolls back any upgrade migrations that are present, and deletes the migrations
  61. * themselves.
  62. */
  63. function drush_migrate_upgrade_rollback() {
  64. if ($date_performed = \Drupal::state()->get('migrate_drupal_ui.performed')) {
  65. if (drush_confirm(dt('All migrations tagged as \'Drupal\' will be rolled back. Are you sure?'))) {
  66. $runner = new MigrateUpgradeDrushRunner();
  67. try {
  68. drush_log(dt('Rolling back the upgrades performed @date',
  69. ['@date' => \Drupal::service('date.formatter')->format($date_performed)]));
  70. $runner->rollback();
  71. \Drupal::state()->delete('migrate_drupal_ui.performed');
  72. drush_log(dt('Rolled back upgrades'));
  73. }
  74. catch (\Exception $e) {
  75. drush_log($e->getMessage(), 'error');
  76. }
  77. }
  78. else {
  79. drush_user_abort();
  80. }
  81. }
  82. else {
  83. drush_log(dt('No upgrade operation has been performed.'), 'warning');
  84. }
  85. }