Skip to content

Commit

Permalink
4020 fix update execute command (#4027)
Browse files Browse the repository at this point in the history
* [update:execute] Fixed the post update

* [debug:update] Create a trait to show tables

* [update:execute] Ask first to execute an update
  • Loading branch information
hjuarez20 authored and enzolutions committed May 10, 2019
1 parent 241d793 commit 404a026
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 68 deletions.
60 changes: 7 additions & 53 deletions src/Command/Debug/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,8 @@

class UpdateCommand extends Command
{
use UpdateTrait;

/**
* @var Site
*/
Expand Down Expand Up @@ -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'));
}
}

Expand Down Expand Up @@ -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);
}
}
73 changes: 73 additions & 0 deletions src/Command/Shared/UpdateTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* @file
* Contains Drupal\Console\Command\Shared\ThemeRegionTrait.
*/

namespace Drupal\Console\Command\Shared;

trait UpdateTrait
{
/**
* @param $updates
* @param $messageKey
* @return mixed
*/
public function showUpdateTable($updates, $messageKey)
{
if(!$updates) {
return 1;
}

$this->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);
}
}
60 changes: 45 additions & 15 deletions src/Command/Update/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +21,8 @@

class ExecuteCommand extends Command
{
use UpdateTrait;

/**
* @var Site
*/
Expand Down Expand Up @@ -129,21 +132,22 @@ 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'
)
)
);
} else {
$this->getIo()->warning(
$this->getIo()->info(
sprintf(
$this->trans(
'commands.update.execute.messages.no-module-updates'
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
)
Expand All @@ -285,6 +312,8 @@ private function runPostUpdates()
}
}

$this->chainQueue->addCommand('update:entities');

return true;
}

Expand All @@ -309,6 +338,7 @@ protected function getUpdateList()
{
$start = [];
$updates = update_get_update_list();

foreach ($updates as $module => $update) {
$start[$module] = $update['start'];
}
Expand Down

0 comments on commit 404a026

Please sign in to comment.