Skip to content

Commit

Permalink
Merge pull request #956 from M0rgan01/database-step-split
Browse files Browse the repository at this point in the history
Split SQL query execution
  • Loading branch information
Quetzacoalt91 authored Oct 29, 2024
2 parents 1d0b39b + 8c29345 commit 64d25c5
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 283 deletions.
10 changes: 10 additions & 0 deletions classes/Parameters/UpgradeFileNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ class UpgradeFileNames
*/
const FILES_TO_UPGRADE_LIST = 'filesToUpgrade.list';

/**
* during updateDatabase process,
* this files contains the list of queries left to execute in a serialized array.
* (this file is deleted in init() method if you reload the page).
*
* @var string
*/
const SQL_TO_EXECUTE_LIST = 'sqlToExecute.list';

/**
* during upgradeModules process,
* this files contains the list of modules left to upgrade in a serialized array.
Expand Down Expand Up @@ -171,5 +180,6 @@ class UpgradeFileNames
'MODULE_SOURCE_PROVIDER_CACHE_COMPOSER',
'MODULE_SOURCE_PROVIDER_CACHE_MARKETPLACE_API',
'MODULE_SOURCE_PROVIDER_CACHE_DISTRIBUTION_API',
'SQL_TO_EXECUTE_LIST',
];
}
115 changes: 101 additions & 14 deletions classes/Task/Update/UpdateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@

namespace PrestaShop\Module\AutoUpgrade\Task\Update;

use Exception;
use PrestaShop\Module\AutoUpgrade\Exceptions\UpgradeException;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Progress\Backlog;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
Expand All @@ -36,19 +39,44 @@
use PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader\CoreUpgrader17;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader\CoreUpgrader80;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\CoreUpgrader\CoreUpgrader81;
use PrestaShop\Module\AutoUpgrade\VersionUtils;

class UpdateDatabase extends AbstractTask
{
const TASK_TYPE = TaskType::TASK_TYPE_UPDATE;

/** @var CoreUpgrader */
private $coreUpgrader;

public function run(): int
{
$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

try {
$this->getCoreUpgrader()->doUpgrade();
if (!$this->container->getFileConfigurationStorage()->exists(UpgradeFileNames::SQL_TO_EXECUTE_LIST)) {
$this->warmUp();
$originVersion = $this->container->getState()->getOriginVersion();
$sqlContentList = $this->getCoreUpgrader()->getSqlContentList($originVersion);
$backlog = new Backlog(array_reverse($sqlContentList), count($sqlContentList));
} else {
$this->getCoreUpgrader()->setupUpdateEnvironment();
$backlog = Backlog::fromContents($this->container->getFileConfigurationStorage()->load(UpgradeFileNames::SQL_TO_EXECUTE_LIST));
}

if ($backlog->getRemainingTotal() > 0) {
$this->logger->info($this->translator->trans('Update database in progress. %d queries left', [$backlog->getRemainingTotal()]));

$this->updateDatabase($backlog);

$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->computePercentage($backlog, self::class, UpdateModules::class)
);

$this->next = TaskName::TASK_UPDATE_DATABASE;
$this->stepDone = false;

return ExitCode::SUCCESS;
}
$this->container->getFileConfigurationStorage()->clean(UpgradeFileNames::SQL_TO_EXECUTE_LIST);
$this->getCoreUpgrader()->finalizeCoreUpdate();
} catch (UpgradeException $e) {
$this->next = TaskName::TASK_ERROR;
$this->setErrorFlag();
Expand All @@ -69,26 +97,85 @@ public function run(): int

public function getCoreUpgrader(): CoreUpgrader
{
if (version_compare($this->container->getState()->getInstallVersion(), '8', '<')) {
return new CoreUpgrader17($this->container, $this->logger);
if ($this->coreUpgrader !== null) {
return $this->coreUpgrader;
}

if (version_compare($this->container->getState()->getInstallVersion(), '8.1', '<')) {
return new CoreUpgrader80($this->container, $this->logger);
if (version_compare($this->container->getState()->getInstallVersion(), '8', '<')) {
$this->coreUpgrader = new CoreUpgrader17($this->container, $this->logger);
} elseif (version_compare($this->container->getState()->getInstallVersion(), '8.1', '<')) {
$this->coreUpgrader = new CoreUpgrader80($this->container, $this->logger);
} else {
$this->coreUpgrader = new CoreUpgrader81($this->container, $this->logger);
}

return new CoreUpgrader81($this->container, $this->logger);
return $this->coreUpgrader;
}

public function init(): void
{
$this->logger->info($this->translator->trans('Cleaning file cache'));
$this->container->getCacheCleaner()->cleanFolders();
$this->logger->info($this->translator->trans('Running opcache_reset'));
$this->container->resetOpcache();
if (!$this->container->getFileConfigurationStorage()->exists(UpgradeFileNames::SQL_TO_EXECUTE_LIST)) {
$this->logger->info($this->translator->trans('Cleaning file cache'));
$this->container->getCacheCleaner()->cleanFolders();
$this->logger->info($this->translator->trans('Running opcache_reset'));
$this->container->resetOpcache();
}

// Migrating settings file
$this->container->initPrestaShopAutoloader();
parent::init();
}

/**
* @throws UpgradeException
* @throws Exception
*/
protected function warmUp(): int
{
$this->logger->info($this->container->getTranslator()->trans('Updating database data and structure'));

$this->container->getState()->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

$this->getCoreUpgrader()->writeNewSettings();

$this->logger->info($this->container->getTranslator()->trans('Checking version validity'));
$this->checkVersionIsNewer();

$this->getCoreUpgrader()->setupUpdateEnvironment();

if ($this->container->getUpgradeConfiguration()->shouldDeactivateCustomModules()) {
$this->logger->info($this->container->getTranslator()->trans('Disabling all non native modules'));
$this->getCoreUpgrader()->disableCustomModules();
} else {
$this->logger->info($this->container->getTranslator()->trans('Keeping non native modules enabled'));
}

return ExitCode::SUCCESS;
}

/**
* @throws UpgradeException
*/
protected function checkVersionIsNewer(): void
{
$originVersion = VersionUtils::normalizePrestaShopVersion($this->container->getState()->getOriginVersion());
$installVersion = VersionUtils::normalizePrestaShopVersion($this->container->getState()->getInstallVersion());

$versionCompare = version_compare($installVersion, $originVersion);

if ($versionCompare === -1) {
throw new UpgradeException($this->container->getTranslator()->trans('[ERROR] Version to install is too old.') . ' ' . $this->container->getTranslator()->trans('Current version: %oldversion%. Version to install: %newversion%.', ['%oldversion%' => $originVersion, '%newversion%' => $installVersion]));
} elseif ($versionCompare === 0) {
throw new UpgradeException($this->container->getTranslator()->trans('You already have the %s version.', [$installVersion]));
}
}

protected function updateDatabase(Backlog $backlog): void
{
$sqlContent = $backlog->getNext();
$this->getCoreUpgrader()->runQuery($sqlContent['version'], $sqlContent['query']);
$this->container->getFileConfigurationStorage()->save($backlog->dump(), UpgradeFileNames::SQL_TO_EXECUTE_LIST);
}
}
Loading

0 comments on commit 64d25c5

Please sign in to comment.