From 404a02616aa926f1a8a0da127bb4174bbc4b4728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harold=20Ju=C3=A1rez?= Date: Thu, 9 May 2019 18:12:50 -0600 Subject: [PATCH] 4020 fix update execute command (#4027) * [update:execute] Fixed the post update * [debug:update] Create a trait to show tables * [update:execute] Ask first to execute an update --- src/Command/Debug/UpdateCommand.php | 60 +++------------------- src/Command/Shared/UpdateTrait.php | 73 +++++++++++++++++++++++++++ src/Command/Update/ExecuteCommand.php | 60 ++++++++++++++++------ 3 files changed, 125 insertions(+), 68 deletions(-) create mode 100644 src/Command/Shared/UpdateTrait.php diff --git a/src/Command/Debug/UpdateCommand.php b/src/Command/Debug/UpdateCommand.php index d0ff04ddc..6f24765d6 100644 --- a/src/Command/Debug/UpdateCommand.php +++ b/src/Command/Debug/UpdateCommand.php @@ -7,6 +7,7 @@ namespace Drupal\Console\Command\Debug; +use Drupal\Console\Command\Shared\UpdateTrait; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Drupal\Console\Core\Command\Command; @@ -15,6 +16,8 @@ class UpdateCommand extends Command { + use UpdateTrait; + /** * @var Site */ @@ -65,16 +68,17 @@ protected function execute(InputInterface $input, OutputInterface $output) $requirements = update_check_requirements(); $severity = drupal_requirements_severity($requirements); $updates = update_get_update_list(); + $postUpdates = $this->postUpdateRegistry->getPendingUpdateInformation(); $this->getIo()->newLine(); if ($severity == REQUIREMENT_ERROR || ($severity == REQUIREMENT_WARNING)) { $this->populateRequirements($requirements); - } elseif (empty($updates)) { + } elseif (empty($updates) && empty($postUpdates)) { $this->getIo()->info($this->trans('commands.debug.update.messages.no-updates')); } else { - $this->populateUpdate($updates); - $this->populatePostUpdate(); + $this->showUpdateTable($updates, $this->trans('commands.debug.update.messages.module-list')); + $this->showPostUpdateTable($postUpdates, $this->trans('commands.debug.update.messages.module-list-post-update')); } } @@ -110,54 +114,4 @@ private function populateRequirements($requirements) $this->getIo()->table($tableHeader, $tableRows); } - - /** - * @param $updates - */ - private function populateUpdate($updates) - { - $this->getIo()->info($this->trans('commands.debug.update.messages.module-list')); - $tableHeader = [ - $this->trans('commands.debug.update.messages.module'), - $this->trans('commands.debug.update.messages.update-n'), - $this->trans('commands.debug.update.messages.description') - ]; - $tableRows = []; - foreach ($updates as $module => $module_updates) { - foreach ($module_updates['pending'] as $update_n => $update) { - list(, $description) = explode($update_n . " - ", $update); - $tableRows[] = [ - $module, - $update_n, - trim($description), - ]; - } - } - $this->getIo()->table($tableHeader, $tableRows); - } - - private function populatePostUpdate() - { - $this->getIo()->info( - $this->trans('commands.debug.update.messages.module-list-post-update') - ); - $tableHeader = [ - $this->trans('commands.debug.update.messages.module'), - $this->trans('commands.debug.update.messages.post-update'), - $this->trans('commands.debug.update.messages.description') - ]; - - $postUpdates = $this->postUpdateRegistry->getPendingUpdateInformation(); - $tableRows = []; - foreach ($postUpdates as $module => $module_updates) { - foreach ($module_updates['pending'] as $postUpdateFunction => $message) { - $tableRows[] = [ - $module, - $postUpdateFunction, - $message, - ]; - } - } - $this->getIo()->table($tableHeader, $tableRows); - } } diff --git a/src/Command/Shared/UpdateTrait.php b/src/Command/Shared/UpdateTrait.php new file mode 100644 index 000000000..c8598f684 --- /dev/null +++ b/src/Command/Shared/UpdateTrait.php @@ -0,0 +1,73 @@ +getIo()->info($messageKey); + $tableHeader = [ + $this->trans('commands.debug.update.messages.module'), + $this->trans('commands.debug.update.messages.update-n'), + $this->trans('commands.debug.update.messages.description') + ]; + $tableRows = []; + foreach ($updates as $module => $module_updates) { + foreach ($module_updates['pending'] as $update_n => $update) { + list(, $description) = explode($update_n . " - ", $update); + $tableRows[] = [ + $module, + $update_n, + trim($description), + ]; + } + } + $this->getIo()->table($tableHeader, $tableRows); + } + + /** + * @param $postUpdates + * @param $messageKey + * @return mixed + */ + public function showPostUpdateTable($postUpdates, $messageKey) + { + if(!$postUpdates) { + return 1; + } + + $this->getIo()->info($messageKey); + $tableHeader = [ + $this->trans('commands.debug.update.messages.module'), + $this->trans('commands.debug.update.messages.post-update'), + $this->trans('commands.debug.update.messages.description') + ]; + + $tableRows = []; + foreach ($postUpdates as $module => $module_updates) { + foreach ($module_updates['pending'] as $postUpdateFunction => $message) { + $tableRows[] = [ + $module, + $postUpdateFunction, + $message, + ]; + } + } + $this->getIo()->table($tableHeader, $tableRows); + } +} diff --git a/src/Command/Update/ExecuteCommand.php b/src/Command/Update/ExecuteCommand.php index 599012631..d22d2d95d 100644 --- a/src/Command/Update/ExecuteCommand.php +++ b/src/Command/Update/ExecuteCommand.php @@ -7,6 +7,7 @@ namespace Drupal\Console\Command\Update; +use Drupal\Console\Command\Shared\UpdateTrait; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; @@ -20,6 +21,8 @@ class ExecuteCommand extends Command { + use UpdateTrait; + /** * @var Site */ @@ -129,13 +132,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $start = $this->getUpdates($this->module!=='all'?$this->module:null); $updates = update_resolve_dependencies($start); $dependencyMap = []; + $allowUpdate = false; foreach ($updates as $function => $update) { $dependencyMap[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : []; } if (!$this->checkUpdates($start, $updates)) { if ($this->module === 'all') { - $this->getIo()->warning( + $this->getIo()->info( sprintf( $this->trans( 'commands.update.execute.messages.no-pending-updates' @@ -143,7 +147,7 @@ protected function execute(InputInterface $input, OutputInterface $output) ) ); } else { - $this->getIo()->warning( + $this->getIo()->info( sprintf( $this->trans( 'commands.update.execute.messages.no-module-updates' @@ -152,27 +156,34 @@ protected function execute(InputInterface $input, OutputInterface $output) ) ); } + $this->getIo()->info(''); } else { + $this->showUpdateTable($updates, $this->trans('commands.update.execute.messages.pending-updates')); + + $allowUpdate = $this->getIo()->confirm( + $this->trans('commands.update.execute.questions.update'), + true + ); + try { - $this->runUpdates( - $updates - ); + if($allowUpdate) { + $this->runUpdates( + $updates + ); + } } catch (\Exception $e) { watchdog_exception('update', $e); $this->getIo()->error($e->getMessage()); return 1; } } - // Post Updates are only safe to run after all schemas have been updated. - if (!$this->getUpdates()) { - $this->runPostUpdates($postUpdates); - - $this->chainQueue->addCommand('update:entities'); - } + $postUpdates = $this->runPostUpdates(); - $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']); + if($postUpdates || $allowUpdate) { + $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']); + } return 0; } @@ -259,13 +270,29 @@ private function runUpdates( */ private function runPostUpdates() { - $postUpdates = $this->postUpdateRegistry->getPendingUpdateInformation(); - + if(!$postUpdates = $this->postUpdateRegistry->getPendingUpdateInformation()) { + $this->getIo()->info( + $this->trans('commands.update.execute.messages.no-pending-post-updates') + ); + return 0; + } + + $this->showPostUpdateTable($postUpdates, $this->trans('commands.update.execute.messages.pending-post-updates')); + + $allowPostUpdate = $this->getIo()->confirm( + $this->trans('commands.update.execute.questions.post-update'), + true + ); + + if(!$allowPostUpdate) { + return 0; + } + foreach ($postUpdates as $module => $updates) { foreach ($updates['pending'] as $updateName => $update) { $this->getIo()->info( sprintf( - $this->trans('commands.update.execute.messages.executing-update'), + $this->trans('commands.update.execute.messages.executing-post-update'), $updateName, $module ) @@ -285,6 +312,8 @@ private function runPostUpdates() } } + $this->chainQueue->addCommand('update:entities'); + return true; } @@ -309,6 +338,7 @@ protected function getUpdateList() { $start = []; $updates = update_get_update_list(); + foreach ($updates as $module => $update) { $start[$module] = $update['start']; }