Skip to content

Commit

Permalink
Merge pull request PrestaShop#1105 from ga-devfront/feat/use-config-f…
Browse files Browse the repository at this point in the history
…or-state

[NEW UI] use config for restore state
  • Loading branch information
tblivet authored Jan 9, 2025
2 parents 51d7764 + d5654a8 commit 9f178b3
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 26 deletions.
3 changes: 2 additions & 1 deletion classes/Commands/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

use Exception;
use InvalidArgumentException;
use PrestaShop\Module\AutoUpgrade\Parameters\RestoreConfiguration;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\Runner\AllRestoreTasks;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -79,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$this->upgradeContainer->getFileStorage()->cleanAllRestoreFiles();
$controller = new AllRestoreTasks($this->upgradeContainer);
$controller->setOptions([
'backup' => $backup,
RestoreConfiguration::BACKUP_NAME => $backup,
]);
$controller->init();
$exitCode = $controller->run();
Expand Down
4 changes: 2 additions & 2 deletions classes/Progress/CompletionCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupFiles;
use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupInitialization;
use PrestaShop\Module\AutoUpgrade\Task\Restore\Restore;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreComplete;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreInitialization;
use PrestaShop\Module\AutoUpgrade\Task\Update\CleanDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Update\Download;
use PrestaShop\Module\AutoUpgrade\Task\Update\Unzip;
Expand Down Expand Up @@ -72,7 +72,7 @@ private static function getPercentages(): array
UpdateComplete::class => 100,

// Restore
Restore::class => 0,
RestoreInitialization::class => 0,
RestoreFiles::class => 33,
RestoreDatabase::class => 66,
RestoreComplete::class => 100,
Expand Down
14 changes: 7 additions & 7 deletions classes/State/AbstractState.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ abstract class AbstractState
protected $disableSave = false;

/** @var FileStorage */
private $fileConfigurationStorage;
private $fileStorage;

public function __construct(FileStorage $fileConfigurationStorage)
public function __construct(FileStorage $fileStorage)
{
$this->fileConfigurationStorage = $fileConfigurationStorage;
$this->fileStorage = $fileStorage;
}

/** @return UpgradeFileNames::STATE_*_FILENAME */
Expand All @@ -51,7 +51,7 @@ abstract protected function getFileNameForPersistentStorage(): string;
*/
public function load()
{
$state = $this->fileConfigurationStorage->load($this->getFileNameForPersistentStorage());
$state = $this->fileStorage->load($this->getFileNameForPersistentStorage());

$this->importFromArray($state);
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public function importFromEncodedData(string $encodedData): self
public function save(): bool
{
if (!$this->disableSave) {
return $this->fileConfigurationStorage->save($this->export(), $this->getFileNameForPersistentStorage());
return $this->fileStorage->save($this->export(), $this->getFileNameForPersistentStorage());
}

return true;
Expand All @@ -95,7 +95,7 @@ public function save(): bool
public function export(): array
{
$state = get_object_vars($this);
foreach (['fileConfigurationStorage', 'disableSave'] as $keyToRemove) {
foreach (['fileStorage', 'disableSave'] as $keyToRemove) {
unset($state[$keyToRemove]);
}

Expand All @@ -104,6 +104,6 @@ public function export(): array

public function isInitialized(): bool
{
return $this->fileConfigurationStorage->exists($this->getFileNameForPersistentStorage());
return $this->fileStorage->exists($this->getFileNameForPersistentStorage());
}
}
17 changes: 15 additions & 2 deletions classes/State/RestoreState.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace PrestaShop\Module\AutoUpgrade\State;

use PrestaShop\Module\AutoUpgrade\Parameters\RestoreConfiguration;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;

class RestoreState extends AbstractState
Expand All @@ -38,7 +39,7 @@ class RestoreState extends AbstractState
*/
protected $restoreName;
/**
* @var string
* @var string|null
*/
protected $restoreFilesFilename;
/**
Expand All @@ -54,6 +55,18 @@ class RestoreState extends AbstractState
*/
protected $dbStep = 0;

public function initDefault(RestoreConfiguration $restoreConfiguration): void
{
$this->disableSave = true;

$this->setRestoreName($restoreConfiguration->getBackupName());
$this->setRestoreFilesFilename(null);
$this->setRestoreDbFilenames([]);

$this->disableSave = false;
$this->save();
}

protected function getFileNameForPersistentStorage(): string
{
return UpgradeFileNames::STATE_RESTORE_FILENAME;
Expand Down Expand Up @@ -90,7 +103,7 @@ public function getRestoreFilesFilename(): ?string
return $this->restoreFilesFilename;
}

public function setRestoreFilesFilename(string $restoreFilesFilename): self
public function setRestoreFilesFilename(?string $restoreFilesFilename): self
{
$this->restoreFilesFilename = $restoreFilesFilename;
$this->save();
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Restore/RestoreDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function run(): int

if (empty($content)) {
$this->logger->error($this->translator->trans('Database backup is empty.'));
$this->next = TaskName::TASK_RESTORE;
$this->next = TaskName::TASK_RESTORE_INITIALIZATION;

return ExitCode::FAIL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/**
* First step executed during a rollback.
*/
class Restore extends AbstractTask
class RestoreInitialization extends AbstractTask
{
const TASK_TYPE = TaskType::TASK_TYPE_RESTORE;

Expand All @@ -49,7 +49,9 @@ class Restore extends AbstractTask
*/
public function run(): int
{
$restoreConfiguration = $this->container->getRestoreConfiguration();
$state = $this->container->getRestoreState();
$state->initDefault($restoreConfiguration);

$state->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
Expand Down
11 changes: 7 additions & 4 deletions classes/Task/Runner/AllRestoreTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
class AllRestoreTasks extends ChainedTasks
{
const initialTask = TaskName::TASK_RESTORE;
const initialTask = TaskName::TASK_RESTORE_INITIALIZATION;

/**
* @var string
Expand All @@ -48,12 +48,15 @@ class AllRestoreTasks extends ChainedTasks
* > data: Loads an encoded array of data coming from another request.
*
* @param array<string, string> $options
*
* @throws \Exception
*/
public function setOptions(array $options): void
{
if (!empty($options['backup'])) {
$this->container->getRestoreState()->setRestoreName($options['backup']);
}
$restoreConfiguration = $this->container->getRestoreConfiguration();
$restoreConfiguration->merge($options);

$this->container->getConfigurationStorage()->save($restoreConfiguration);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions classes/Task/Runner/ChainedTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected function checkIfRestartRequested(AjaxResponse $response): bool

private function setupLogging(): void
{
$initializationSteps = [TaskName::TASK_BACKUP_INITIALIZATION, TaskName::TASK_UPDATE_INITIALIZATION, TaskName::TASK_RESTORE];
$initializationSteps = [TaskName::TASK_BACKUP_INITIALIZATION, TaskName::TASK_UPDATE_INITIALIZATION, TaskName::TASK_RESTORE_INITIALIZATION];

if (in_array($this->step, $initializationSteps)) {
$this->container->getWorkspace()->createFolders();
Expand All @@ -123,7 +123,7 @@ private function setupLogging(): void
case TaskName::TASK_BACKUP_INITIALIZATION:
$this->container->getLogsState()->setActiveBackupLogFromDateTime($timestamp);
break;
case TaskName::TASK_RESTORE:
case TaskName::TASK_RESTORE_INITIALIZATION:
$this->container->getLogsState()->setActiveRestoreLogFromDateTime($timestamp);
break;
case TaskName::TASK_UPDATE_INITIALIZATION:
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/TaskName.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TaskName
const TASK_BACKUP_COMPLETE = 'BackupComplete';

// RESTORE
const TASK_RESTORE = 'Restore';
const TASK_RESTORE_INITIALIZATION = 'RestoreInitialization';
const TASK_RESTORE_EMPTY = 'RestoreEmpty';
const TASK_RESTORE_DATABASE = 'RestoreDatabase';
const TASK_RESTORE_FILES = 'RestoreFiles';
Expand Down
6 changes: 3 additions & 3 deletions classes/UpgradeTools/TaskRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
use PrestaShop\Module\AutoUpgrade\Task\Miscellaneous\CompareReleases;
use PrestaShop\Module\AutoUpgrade\Task\Miscellaneous\UpdateConfig;
use PrestaShop\Module\AutoUpgrade\Task\NullTask;
use PrestaShop\Module\AutoUpgrade\Task\Restore\Restore;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreComplete;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreEmpty;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreInitialization;
use PrestaShop\Module\AutoUpgrade\Task\TaskName;
use PrestaShop\Module\AutoUpgrade\Task\Update\CleanDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Update\Download;
Expand All @@ -63,8 +63,8 @@ public static function get(string $step, UpgradeContainer $container): AbstractT
return new UpdateConfig($container);

// RESTORE
case TaskName::TASK_RESTORE:
return new Restore($container);
case TaskName::TASK_RESTORE_INITIALIZATION:
return new RestoreInitialization($container);
case TaskName::TASK_RESTORE_EMPTY:
return new RestoreEmpty($container);
case TaskName::TASK_RESTORE_DATABASE:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/UpgradeContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupFiles;
use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupInitialization;
use PrestaShop\Module\AutoUpgrade\Task\Restore\Restore;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreComplete;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreEmpty;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreInitialization;
use PrestaShop\Module\AutoUpgrade\Task\Update\CleanDatabase;
use PrestaShop\Module\AutoUpgrade\Task\Update\Download;
use PrestaShop\Module\AutoUpgrade\Task\Update\Unzip;
Expand Down Expand Up @@ -115,7 +115,7 @@ public function stateRelatedToTaskProvider(): array
[BackupFiles::class, BackupState::class],
[BackupInitialization::class, BackupState::class],

[Restore::class, RestoreState::class],
[RestoreInitialization::class, RestoreState::class],
[RestoreComplete::class, RestoreState::class],
[RestoreDatabase::class, RestoreState::class],
[RestoreEmpty::class, RestoreState::class],
Expand Down

0 comments on commit 9f178b3

Please sign in to comment.