From 43477a82850c056740e08d4198ebf6d263b16ae8 Mon Sep 17 00:00:00 2001 From: Adam Zimmermann Date: Mon, 24 Jul 2023 20:05:15 -0500 Subject: [PATCH 1/3] Add lost configuration checking to deploy command. GH-5712 --- src/Commands/core/DeployCommands.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Commands/core/DeployCommands.php b/src/Commands/core/DeployCommands.php index c09410f377..a322c70006 100644 --- a/src/Commands/core/DeployCommands.php +++ b/src/Commands/core/DeployCommands.php @@ -9,6 +9,7 @@ use Consolidation\SiteProcess\ProcessManager; use Drush\Attributes as CLI; use Drush\Commands\DrushCommands; +use Drush\Commands\config\ConfigCommands; use Drush\Commands\config\ConfigImportCommands; use Drush\Commands\core\DeployHookCommands; use Drush\Drush; @@ -35,10 +36,36 @@ public function deploy(): void $redispatchOptions = Drush::redispatchOptions(); $manager = $this->processManager(); + // Prepare custom options for checking configuration state. + $configStatusOptions = $redispatchOptions; + $configStatusOptions['format'] = 'json'; + // Record the state of configuration before database updates. + $process = $manager->drush($self, ConfigCommands::STATUS, [], $configStatusOptions); + $process->mustRun(); + $originalConfigDiff = $process->getOutputAsJson(); + $this->logger()->notice("Database updates start."); $process = $manager->drush($self, UpdateDBCommands::UPDATEDB, [], $redispatchOptions); $process->mustRun($process->showRealtime()); + // Record the state of configuration after database updates. + $process = $manager->drush($self, ConfigCommands::STATUS, [], $configStatusOptions); + $process->mustRun(); + $newConfigDiff = $process->getOutputAsJson(); + + // Check for new changes to active configuration that would be lost + // during the subsequent configuration import. + if ($originalConfigDiff !== $newConfigDiff) { + $configDiff = array_diff( + array_keys($newConfigDiff), + array_keys($originalConfigDiff), + ); + $this->logger()->warning(dt('The following config changes will be lost during config import: :config', [ + ':config' => implode(', ', $configDiff), + ])); + throw new \RuntimeException('Update hooks altered config that is about to be reverted during config import. Aborting.'); + } + $this->logger()->success("Config import start."); $process = $manager->drush($self, ConfigImportCommands::IMPORT, [], $redispatchOptions); $process->mustRun($process->showRealtime()); From 8fd9b154e949282de3cb78f8c74ef7624e077de1 Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Thu, 21 Sep 2023 09:14:23 -0400 Subject: [PATCH 2/3] ignore-mismatched-config option --- src/Commands/core/DeployCommands.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Commands/core/DeployCommands.php b/src/Commands/core/DeployCommands.php index a322c70006..c6e571dad5 100644 --- a/src/Commands/core/DeployCommands.php +++ b/src/Commands/core/DeployCommands.php @@ -26,11 +26,12 @@ final class DeployCommands extends DrushCommands implements SiteAliasManagerAwar * Run several commands after performing a code deployment. */ #[CLI\Command(name: self::DEPLOY)] + #[CLI\Option(name: 'ignore-mismatched-config', description: 'Proceed with config:import even if changes made by update hooks will be reverted.')] #[CLI\Usage(name: 'drush deploy -v -y', description: 'Run updates with verbose logging and accept all prompts.')] #[CLI\Version(version: '10.3')] #[CLI\Topics(topics: [DocsCommands::DEPLOY])] #[CLI\Bootstrap(level: DrupalBootLevels::FULL)] - public function deploy(): void + public function deploy(array $options = ['ignore-mismatched-config' => false]): void { $self = $this->siteAliasManager()->getSelf(); $redispatchOptions = Drush::redispatchOptions(); @@ -63,7 +64,9 @@ public function deploy(): void $this->logger()->warning(dt('The following config changes will be lost during config import: :config', [ ':config' => implode(', ', $configDiff), ])); - throw new \RuntimeException('Update hooks altered config that is about to be reverted during config import. Aborting.'); + if (!$options['ignore-mismatched-config']) { + throw new \RuntimeException('Update hooks altered config that is about to be reverted during config import. Aborting.'); + } } $this->logger()->success("Config import start."); From 947994d303acb3158bfedd429b0dfef975c264b7 Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Sat, 23 Sep 2023 07:04:03 -0400 Subject: [PATCH 3/3] Guidance in error message. --- src/Commands/core/DeployCommands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/core/DeployCommands.php b/src/Commands/core/DeployCommands.php index c6e571dad5..fb2273e403 100644 --- a/src/Commands/core/DeployCommands.php +++ b/src/Commands/core/DeployCommands.php @@ -65,7 +65,7 @@ public function deploy(array $options = ['ignore-mismatched-config' => false]): ':config' => implode(', ', $configDiff), ])); if (!$options['ignore-mismatched-config']) { - throw new \RuntimeException('Update hooks altered config that is about to be reverted during config import. Aborting.'); + throw new \RuntimeException('Update hooks altered config that is about to be reverted during config import. Use --ignore-mismatched-config to bypass this error. Aborting.'); } }