migrations ref

This commit is contained in:
Yuri Kuznetsov
2024-04-05 11:01:02 +03:00
parent 0b19bd4bb5
commit 455bb2a298
5 changed files with 147 additions and 50 deletions
@@ -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;
}
}
@@ -0,0 +1,65 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2024 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* 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;
}
}
@@ -43,7 +43,7 @@ class StepsProvider
/**
* @return string[]
*/
public function get(): array
public function getAfterUpgrade(): array
{
$list = $this->fileManager->getDirList($this->dir);
@@ -0,0 +1,72 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2024 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* 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;
}
}
@@ -56,6 +56,6 @@ class StepsProviderTest extends TestCase
'8.1',
'8.2',
'8.2.2',
], $provider->get());
], $provider->getAfterUpgrade());
}
}