Skip to content

Commit

Permalink
Move entity:updates command to LegacyCommands. Its unused in Drupal 9+ (
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman committed Jan 18, 2022
1 parent d2d1877 commit 1bc10c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 136 deletions.
12 changes: 12 additions & 0 deletions src/Commands/LegacyCommands.php
Expand Up @@ -4,6 +4,18 @@

class LegacyCommands extends DrushCommands
{
/**
* Drupal removed its automatic entity-updates API in 8.7. See https://www.drupal.org/node/3034742.
*
* @command entity:updates
* @aliases entup,entity-updates
* @hidden
* @obsolete
*/
public function entityUpdates(): void
{
}

/**
* The core:init command was removed. Please edit your .bashrc manually.
*
Expand Down
138 changes: 2 additions & 136 deletions src/Commands/core/UpdateDBCommands.php
Expand Up @@ -10,7 +10,6 @@
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Drupal\Core\Utility\Error;
use Drupal\Core\Entity\EntityStorageException;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Drush\Exceptions\UserAbortException;
Expand All @@ -29,25 +28,19 @@ class UpdateDBCommands extends DrushCommands implements SiteAliasManagerAwareInt
*
* @command updatedb
* @option cache-clear Clear caches upon completion.
* @option entity-updates Run automatic entity schema updates at the end of any update hooks. Not supported in Drupal >= 8.7.0.
* @option post-updates Run post updates after hook_update_n and entity updates.
* @bootstrap full
* @topics docs:deploy
* @kernel update
* @aliases updb
*/
public function updatedb($options = ['cache-clear' => true, 'entity-updates' => false, 'post-updates' => true]): int
public function updatedb($options = ['cache-clear' => true, 'post-updates' => true]): int
{
$this->cache_clear = $options['cache-clear'];
require_once DRUPAL_ROOT . '/core/includes/install.inc';
require_once DRUPAL_ROOT . '/core/includes/update.inc';
drupal_load_updates();

if ($options['entity-updates'] && version_compare(drush_drupal_version(), '8.7.0', '>=')) {
$this->logger()->warning(dt('Drupal removed its automatic entity-updates API in 8.7. See https://www.drupal.org/node/3034742.'));
$options['entity-updates'] = false;
}

// Disables extensions that have a lower Drupal core major version, or too high of a PHP requirement.
// Those are rare, and this function does a full rebuild. So commenting it out for now.
// update_fix_compatibility();
Expand All @@ -60,8 +53,6 @@ public function updatedb($options = ['cache-clear' => true, 'entity-updates' =>
}

$status_options = [
// @see https://github.com/drush-ops/drush/pull/3855.
'no-entity-updates' => !$options['entity-updates'],
'no-post-updates' => !$options['post-updates'],
'strict' => 0,
];
Expand Down Expand Up @@ -92,46 +83,10 @@ public function updatedb($options = ['cache-clear' => true, 'entity-updates' =>
return $success ? self::EXIT_SUCCESS : self::EXIT_FAILURE;
}

/**
* Apply pending entity schema updates.
*
* @command entity:updates
* @option cache-clear Set to 0 to suppress normal cache clearing; the caller should then clear if needed.
* @bootstrap full
* @kernel update
* @aliases entup,entity-updates
* @usage drush updatedb:status --entity-updates | grep entity-update
* Use updatedb:status to detect pending updates.
*
*/
public function entityUpdates($options = ['cache-clear' => true]): void
{
if ($this->getConfig()->simulate()) {
throw new \Exception(dt('entity-updates command does not support --simulate option.'));
}

// @todo - Do same check for updatedb as well.
if (version_compare(drush_drupal_version(), '8.7.0', '>=')) {
throw new \Exception(dt('Drupal removed its automatic entity-updates API in 8.7. See https://www.drupal.org/node/3034742.'));
}

if ($this->entityUpdatesMain() === false) {
throw new \Exception('Entity updates not run.');
}

if ($options['cache-clear']) {
$process = $this->processManager()->drush($this->siteAliasManager()->getSelf(), 'cache-rebuild');
$process->mustrun();
}

$this->logger()->success(dt('Finished performing updates.'));
}

/**
* List any pending database updates.
*
* @command updatedb:status
* @option entity-updates Show entity schema updates.
* @option post-updates Show post updates.
* @bootstrap full
* @kernel update
Expand All @@ -145,7 +100,7 @@ public function entityUpdates($options = ['cache-clear' => true]): void
* @filter-default-field type
* @return RowsOfFields
*/
public function updatedbStatus($options = ['format' => 'table', 'entity-updates' => true, 'post-updates' => true])
public function updatedbStatus($options = ['format' => 'table', 'post-updates' => true])
{
require_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
drupal_load_updates();
Expand Down Expand Up @@ -422,16 +377,6 @@ public function updateBatch($options): bool
}
}

// Perform entity definition updates, which will update storage
// schema if needed. If module update functions need to work with specific
// entity schema they should call the entity update service for the specific
// update themselves.
// @see \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface::applyEntityUpdate()
// @see \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface::applyFieldUpdate()
if ($options['entity-updates'] && \Drupal::entityDefinitionUpdateManager()->needsUpdates()) {
$operations[] = [[$this, 'updateEntityDefinitions'], []];
}

// Lastly, apply post update hooks if specified.
if ($options['post-updates']) {
$post_updates = \Drupal::service('update.post_update_registry')->getPendingUpdateFunctions();
Expand Down Expand Up @@ -485,26 +430,6 @@ public static function restoreMaintMode($status): void
\Drupal::service('state')->set('system.maintenance_mode', $status);
}

/**
* Apply entity schema updates.
*/
public function updateEntityDefinitions(&$context): void
{
try {
\Drupal::entityDefinitionUpdateManager()->applyupdates();
} catch (EntityStorageException $e) {
watchdog_exception('update', $e);
$variables = Error::decodeException($e);
unset($variables['backtrace']);
// The exception message is run through
// \Drupal\Component\Utility\SafeMarkup::checkPlain() by
// \Drupal\Core\Utility\Error::decodeException().
$ret['#abort'] = ['success' => false, 'query' => t('%type: !message in %function (line %line of %file).', $variables)];
$context['results']['core']['update_entity_definitions'] = $ret;
$context['results']['#abort'][] = 'update_entity_definitions';
}
}

// Copy of protected \Drupal\system\Controller\DbUpdateController::getModuleUpdates.
public function getUpdateList(): array
{
Expand Down Expand Up @@ -573,23 +498,6 @@ public function getUpdatedbStatus(array $options): array
}
}

// Append row(s) for pending entity definition updates.
if ($options['entity-updates']) {
foreach (
\Drupal::entityDefinitionUpdateManager()
->getChangeSummary() as $entity_type_id => $changes
) {
foreach ($changes as $change) {
$return[] = [
'module' => dt('@type entity type', ['@type' => $entity_type_id]),
'update_id' => '',
'description' => strip_tags($change),
'type' => 'entity-update'
];
}
}
}

// Pending hook_post_update_X() implementations.
$post_updates = \Drupal::service('update.post_update_registry')->getPendingUpdateInformation();
if ($options['post-updates']) {
Expand All @@ -612,48 +520,6 @@ public function getUpdatedbStatus(array $options): array
return [$return, $start];
}

/**
* Apply pending entity schema updates.
*/
public function entityUpdatesMain(): void
{
$change_summary = \Drupal::entityDefinitionUpdateManager()->getChangeSummary();
if (!empty($change_summary)) {
$this->output()->writeln(dt('The following updates are pending:'));
$this->io()->newLine();

foreach ($change_summary as $entity_type_id => $changes) {
$this->output()->writeln($entity_type_id . ' entity type : ');
foreach ($changes as $change) {
$this->output()->writeln(strip_tags($change), 2);
}
}

if (!$this->io()->confirm(dt('Do you wish to run all pending updates?'))) {
throw new UserAbortException();
}

$operations[] = [[$this, 'updateEntityDefinitions'], []];


$batch['operations'] = $operations;
$batch += [
'title' => 'Updating',
'init_message' => 'Starting updates',
'error_message' => 'An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.',
'finished' => [$this, 'updateFinished'],
];
batch_set($batch);

// See updateFinished() for the restore of maint mode.
$this->maintenanceModeOriginalState = \Drupal::service('state')->get('system.maintenance_mode');
\Drupal::service('state')->set('system.maintenance_mode', true);
drush_backend_batch_process();
} else {
$this->logger()->success(dt("No entity schema updates required"));
}
}

/**
* Log messages for any requirements warnings/errors.
*/
Expand Down

0 comments on commit 1bc10c9

Please sign in to comment.