From 455bb2a29807c327f8511efa8e0dcb1a040dda18 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 5 Apr 2024 11:01:02 +0300 Subject: [PATCH] migrations ref --- .../Espo/Core/Upgrades/Migration/Runner.php | 56 +++------------ .../Core/Upgrades/Migration/StepRunner.php | 65 +++++++++++++++++ .../Core/Upgrades/Migration/StepsProvider.php | 2 +- .../Migration/VersionDataProvider.php | 72 +++++++++++++++++++ .../Upgrades/Migration/StepsProviderTest.php | 2 +- 5 files changed, 147 insertions(+), 50 deletions(-) create mode 100644 application/Espo/Core/Upgrades/Migration/StepRunner.php create mode 100644 application/Espo/Core/Upgrades/Migration/VersionDataProvider.php diff --git a/application/Espo/Core/Upgrades/Migration/Runner.php b/application/Espo/Core/Upgrades/Migration/Runner.php index 7e703530b3..1b017f6bf5 100644 --- a/application/Espo/Core/Upgrades/Migration/Runner.php +++ b/application/Espo/Core/Upgrades/Migration/Runner.php @@ -30,29 +30,23 @@ namespace Espo\Core\Upgrades\Migration; use Espo\Core\Console\IO; -use Espo\Core\Utils\Config; -use Espo\Core\Utils\File\Manager; use RuntimeException; -use Symfony\Component\Process\PhpExecutableFinder; -use Symfony\Component\Process\Process; class Runner { - private string $defaultConfigPath = 'application/Espo/Resources/defaults/config.php'; - public function __construct( - private Config $config, - private Manager $fileManager, private StepsExtractor $stepsExtractor, - private StepsProvider $stepsProvider + private StepsProvider $stepsProvider, + private VersionDataProvider $versionDataProvider, + private StepRunner $stepRunner ) {} public function run(IO $io): void { - $version = $this->config->get('version'); - $targetVersion = $this->getTargetVersion(); + $version = $this->versionDataProvider->getPreviousVersion(); + $targetVersion = $this->versionDataProvider->getTargetVersion(); - $fullList = $this->stepsProvider->get(); + $fullList = $this->stepsProvider->getAfterUpgrade(); $steps = $this->stepsExtractor->extract($version, $targetVersion, $fullList); if ($steps === []) { @@ -70,17 +64,11 @@ class Runner private function runVersionStep(IO $io, string $step): void { - $phpExecutablePath = $this->getPhpExecutablePath(); - - $command = "command.php migration-version-step --step=$step"; - $io->write(" $step..."); - $process = new Process([$phpExecutablePath, $command]); - $process->setTimeout(null); - $process->run(); + $isSuccessful = $this->stepRunner->run($step); - if ($process->isSuccessful()) { + if ($isSuccessful) { $io->writeLine(" DONE"); return; @@ -90,32 +78,4 @@ class Runner throw new RuntimeException(); } - - private function getPhpExecutablePath(): string - { - $phpExecutablePath = $this->config->get('phpExecutablePath'); - - if (!$phpExecutablePath) { - $phpExecutablePath = (new PhpExecutableFinder)->find(); - } - - return $phpExecutablePath; - } - - private function getTargetVersion(): string - { - $data = $this->fileManager->getPhpContents($this->defaultConfigPath); - - if (!is_array($data)) { - throw new RuntimeException("No default config."); - } - - $version = $data['version'] ?? null; - - if (!$version || !is_string($version)) { - throw new RuntimeException("No or bad 'version' parameter in default config."); - } - - return $version; - } } diff --git a/application/Espo/Core/Upgrades/Migration/StepRunner.php b/application/Espo/Core/Upgrades/Migration/StepRunner.php new file mode 100644 index 0000000000..851af6189c --- /dev/null +++ b/application/Espo/Core/Upgrades/Migration/StepRunner.php @@ -0,0 +1,65 @@ +. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU Affero General Public License version 3. + * + * In accordance with Section 7(b) of the GNU Affero General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. + ************************************************************************/ + +namespace Espo\Core\Upgrades\Migration; + +use Espo\Core\Utils\Config; +use Symfony\Component\Process\PhpExecutableFinder; +use Symfony\Component\Process\Process; + +class StepRunner +{ + public function __construct( + private Config $config + ) {} + + public function run(string $step): bool + { + $phpExecutablePath = $this->getPhpExecutablePath(); + + $command = "command.php migration-version-step --step=$step"; + + $process = new Process([$phpExecutablePath, $command]); + $process->setTimeout(null); + $process->run(); + + return $process->isSuccessful(); + } + + private function getPhpExecutablePath(): string + { + $phpExecutablePath = $this->config->get('phpExecutablePath'); + + if (!$phpExecutablePath) { + $phpExecutablePath = (new PhpExecutableFinder)->find(); + } + + return $phpExecutablePath; + } +} diff --git a/application/Espo/Core/Upgrades/Migration/StepsProvider.php b/application/Espo/Core/Upgrades/Migration/StepsProvider.php index 91dc9054ac..e0b2270dce 100644 --- a/application/Espo/Core/Upgrades/Migration/StepsProvider.php +++ b/application/Espo/Core/Upgrades/Migration/StepsProvider.php @@ -43,7 +43,7 @@ class StepsProvider /** * @return string[] */ - public function get(): array + public function getAfterUpgrade(): array { $list = $this->fileManager->getDirList($this->dir); diff --git a/application/Espo/Core/Upgrades/Migration/VersionDataProvider.php b/application/Espo/Core/Upgrades/Migration/VersionDataProvider.php new file mode 100644 index 0000000000..937ba9334b --- /dev/null +++ b/application/Espo/Core/Upgrades/Migration/VersionDataProvider.php @@ -0,0 +1,72 @@ +. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU Affero General Public License version 3. + * + * In accordance with Section 7(b) of the GNU Affero General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. + ************************************************************************/ + +namespace Espo\Core\Upgrades\Migration; + +use Espo\Core\Utils\Config; +use Espo\Core\Utils\File\Manager; +use RuntimeException; + +class VersionDataProvider +{ + private string $defaultConfigPath = 'application/Espo/Resources/defaults/config.php'; + + public function __construct( + private Config $config, + private Manager $fileManager + ) {} + + public function getPreviousVersion(): string + { + $version = $this->config->get('version'); + + if (!is_string($version) || !$version) { + throw new RuntimeException("No or bad 'version' in config."); + } + + return $version; + } + + public function getTargetVersion(): string + { + $data = $this->fileManager->getPhpContents($this->defaultConfigPath); + + if (!is_array($data)) { + throw new RuntimeException("No default config."); + } + + $version = $data['version'] ?? null; + + if (!$version || !is_string($version)) { + throw new RuntimeException("No or bad 'version' parameter in default config."); + } + + return $version; + } +} diff --git a/tests/unit/Espo/Core/Upgrades/Migration/StepsProviderTest.php b/tests/unit/Espo/Core/Upgrades/Migration/StepsProviderTest.php index 30680f06e3..69801988a6 100644 --- a/tests/unit/Espo/Core/Upgrades/Migration/StepsProviderTest.php +++ b/tests/unit/Espo/Core/Upgrades/Migration/StepsProviderTest.php @@ -56,6 +56,6 @@ class StepsProviderTest extends TestCase '8.1', '8.2', '8.2.2', - ], $provider->get()); + ], $provider->getAfterUpgrade()); } }