diff --git a/.github/action_upgrade.sh b/.github/action_upgrade.sh index 4c60f9cb0..6ad94d1e5 100755 --- a/.github/action_upgrade.sh +++ b/.github/action_upgrade.sh @@ -15,20 +15,20 @@ if [[ $CHANNEL == "local" ]]; then fi fi - SKIP_BACKUP=$(docker exec -u www-data prestashop_autoupgrade ls admin-dev/autoupgrade/backup/ | wc -l) - if [[ "$SKIP_BACKUP" > 1 ]]; then - SKIP_BACKUP=1 - else - SKIP_BACKUP=0 - fi - docker exec -u www-data prestashop_autoupgrade mkdir admin-dev/autoupgrade/download docker exec -u www-data prestashop_autoupgrade curl -L $ARCHIVE_URL -o admin-dev/autoupgrade/download/prestashop.zip docker exec -u www-data prestashop_autoupgrade curl -L $XML_URL -o modules/autoupgrade/download/prestashop.xml - echo "{\"channel\":\"local\",\"archive_prestashop\":\"prestashop.zip\",\"archive_num\":\"${VERSION}\", \"archive_xml\":\"prestashop.xml\", \"PS_AUTOUP_CHANGE_DEFAULT_THEME\":${UPDATE_THEME}, \"skip_backup\": ${SKIP_BACKUP}}" > modules/autoupgrade/config.json - docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --action="compareReleases" --config-file-path="modules/autoupgrade/config.json" admin-dev + + FILE_COUNT=$(docker exec -u www-data prestashop_autoupgrade ls admin-dev/autoupgrade/backup/ | wc -l) + if [[ "$FILE_COUNT" == 0 ]]; then + docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console backup:create admin-dev + fi + + echo "{\"channel\":\"local\",\"archive_prestashop\":\"prestashop.zip\",\"archive_num\":\"${VERSION}\", \"archive_xml\":\"prestashop.xml\", \"PS_AUTOUP_CHANGE_DEFAULT_THEME\":${UPDATE_THEME}" > modules/autoupgrade/config.json + docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --action="CompareReleases" --config-file-path="modules/autoupgrade/config.json" admin-dev docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --config-file-path="modules/autoupgrade/config.json" admin-dev fi -docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --action="compareReleases" admin-dev +docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console backup:create admin-dev +docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start --action="CompareReleases" admin-dev docker exec -u www-data prestashop_autoupgrade php modules/autoupgrade/bin/console update:start admin-dev diff --git a/.github/workflows/ui-test.yml b/.github/workflows/ui-test.yml index de65368f9..0a417f94e 100644 --- a/.github/workflows/ui-test.yml +++ b/.github/workflows/ui-test.yml @@ -62,7 +62,11 @@ jobs: run: | docker exec -t prestashop php -v bin/console prestashop:module install autoupgrade - - name: Upgrade + - name: Backup + run: | + docker exec -t prestashop php modules/autoupgrade/bin/console backup:create admin-dev + + - name: Update run: | docker exec -t prestashop php modules/autoupgrade/bin/console update:start --channel=${{ matrix.UPGRADE_CHANNEL }} admin-dev docker exec -t prestashop chmod 777 -R /var/www/html/var/cache diff --git a/.github/workflows/upgrade.yml b/.github/workflows/upgrade.yml index 51b24bfe1..71743a47b 100644 --- a/.github/workflows/upgrade.yml +++ b/.github/workflows/upgrade.yml @@ -10,8 +10,7 @@ jobs: matrix: from: ['1.7.0.6', '1.7.6.9', '1.7.6.1', '1.7.7.0', '8.0.0', '8.1.0'] ps-versions: - - channel: minor - - channel: major + - channel: online runs-on: ubuntu-latest name: Upgrade steps: diff --git a/README.md b/README.md index 12139fc62..5886f0792 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,12 @@ The requirements can be reviewed to confirm the shop is safe to update: $ php bin/console update:check ``` +A backup of the shop is created with: + +``` +$ php bin/console backup:create --config-file-path=[/path/to/config.json] +``` + The update process can be launched with: ``` diff --git a/bin/console b/bin/console index a541cef4b..1703cd1e6 100755 --- a/bin/console +++ b/bin/console @@ -27,6 +27,7 @@ */ use PrestaShop\Module\AutoUpgrade\Commands\CheckRequirementsCommand; +use PrestaShop\Module\AutoUpgrade\Commands\CreateBackupCommand; use PrestaShop\Module\AutoUpgrade\Commands\RestoreCommand; use PrestaShop\Module\AutoUpgrade\Commands\UpdateCommand; use Symfony\Component\Console\Application; @@ -46,6 +47,7 @@ $application = new Application(); $application->add(new UpdateCommand()); $application->add(new RestoreCommand()); $application->add(new CheckRequirementsCommand()); +$application->add(new CreateBackupCommand()); try { $application->run(); diff --git a/classes/Commands/CreateBackupCommand.php b/classes/Commands/CreateBackupCommand.php new file mode 100644 index 000000000..1b0b4fb74 --- /dev/null +++ b/classes/Commands/CreateBackupCommand.php @@ -0,0 +1,72 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ + +namespace PrestaShop\Module\AutoUpgrade\Commands; + +use Exception; +use PrestaShop\Module\AutoUpgrade\Task\ExitCode; +use PrestaShop\Module\AutoUpgrade\Task\Runner\AllBackupTasks; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class CreateBackupCommand extends AbstractCommand +{ + /** @var string */ + protected static $defaultName = 'backup:create'; + + protected function configure(): void + { + $this + ->setDescription('Create backup.') + ->setHelp('This command triggers the creation of the files and database backup.') + ->addOption('config-file-path', null, InputOption::VALUE_REQUIRED, 'Configuration file location.') + ->addArgument('admin-dir', InputArgument::REQUIRED, 'The admin directory name.'); + } + + /** + * @throws Exception + */ + protected function execute(InputInterface $input, OutputInterface $output): ?int + { + try { + $this->setupContainer($input, $output); + + $controller = new AllBackupTasks($this->upgradeContainer); + $controller->init(); + $exitCode = $controller->run(); + $this->logger->debug('Process completed with exit code: ' . $exitCode); + + return $exitCode; + } catch (Exception $e) { + $this->logger->error('An error occurred during the backup process: ' . $e->getMessage()); + + return ExitCode::FAIL; + } + } +} diff --git a/classes/Progress/CompletionCalculator.php b/classes/Progress/CompletionCalculator.php index a3b47d355..b49d64129 100644 --- a/classes/Progress/CompletionCalculator.php +++ b/classes/Progress/CompletionCalculator.php @@ -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\Rollback\Restore; -use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreComplete; -use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreDatabase; -use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreFiles; +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\Update\CleanDatabase; use PrestaShop\Module\AutoUpgrade\Task\Update\Download; use PrestaShop\Module\AutoUpgrade\Task\Update\Unzip; @@ -47,10 +47,6 @@ class CompletionCalculator { - public function __construct() - { - } - /** * The key baseWithoutBackup exists while the backup and upgrade are on the same workflow * diff --git a/classes/Task/Backup/BackupDatabase.php b/classes/Task/Backup/BackupDatabase.php index b0f8d5b17..2f1691a4c 100644 --- a/classes/Task/Backup/BackupDatabase.php +++ b/classes/Task/Backup/BackupDatabase.php @@ -29,7 +29,6 @@ use Exception; use PDO; -use PrestaShop\Module\AutoUpgrade\Analytics; use PrestaShop\Module\AutoUpgrade\Exceptions\UpgradeException; use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames; use PrestaShop\Module\AutoUpgrade\Progress\Backlog; @@ -51,15 +50,6 @@ class BackupDatabase extends AbstractTask */ public function run(): int { - if (!$this->container->getUpgradeConfiguration()->shouldBackupFilesAndDatabase()) { - $this->stepDone = true; - $this->container->getState()->setDbStep(0); - $this->logger->info($this->translator->trans('Database backup skipped. Now upgrading files...')); - $this->next = 'upgradeFiles'; - - return ExitCode::SUCCESS; - } - $this->stepDone = false; $this->next = TaskName::TASK_BACKUP_DATABASE; $start_time = time(); @@ -248,7 +238,7 @@ public function run(): int } if ($tablesToBackup->getRemainingTotal()) { - $this->next = 'backupDb'; + $this->next = TaskName::TASK_BACKUP_DATABASE; $this->stepDone = false; if ($numberOfSyncedTables) { $this->logger->info($this->translator->trans('Database backup: %s table(s) left...', [$tablesToBackup->getRemainingTotal() + !empty($row)])); @@ -265,10 +255,8 @@ public function run(): int // reset dbStep at the end of this step $this->container->getState()->setDbStep(0); - $this->logger->info($this->translator->trans('Database backup done in filename %s. Now upgrading files...', [$this->container->getState()->getBackupName()])); - $this->next = 'upgradeFiles'; - - $this->container->getAnalytics()->track('Backup Succeeded', Analytics::WITH_BACKUP_PROPERTIES); + $this->logger->info($this->translator->trans('Database backup done in filename %s.', [$this->container->getState()->getBackupName()])); + $this->next = TaskName::TASK_BACKUP_COMPLETE; return ExitCode::SUCCESS; } diff --git a/classes/Task/Restore/Restore.php b/classes/Task/Restore/Restore.php index 5d7d8c2f8..e5e2da16b 100644 --- a/classes/Task/Restore/Restore.php +++ b/classes/Task/Restore/Restore.php @@ -25,7 +25,7 @@ * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ -namespace PrestaShop\Module\AutoUpgrade\Task\Rollback; +namespace PrestaShop\Module\AutoUpgrade\Task\Restore; use Exception; use PrestaShop\Module\AutoUpgrade\Analytics; @@ -59,7 +59,7 @@ public function run(): int $restoreDbFilenames = $this->container->getState()->getRestoreDbFilenames(); if (empty($restoreName)) { - $this->next = TaskName::TASK_NO_RESTORE_FOUND; + $this->next = TaskName::TASK_RESTORE_EMPTY; return ExitCode::SUCCESS; } diff --git a/classes/Task/Restore/RestoreComplete.php b/classes/Task/Restore/RestoreComplete.php index 722416a00..f4f30f05d 100644 --- a/classes/Task/Restore/RestoreComplete.php +++ b/classes/Task/Restore/RestoreComplete.php @@ -25,7 +25,7 @@ * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ -namespace PrestaShop\Module\AutoUpgrade\Task\Rollback; +namespace PrestaShop\Module\AutoUpgrade\Task\Restore; use PrestaShop\Module\AutoUpgrade\Analytics; use PrestaShop\Module\AutoUpgrade\Task\AbstractTask; diff --git a/classes/Task/Restore/RestoreDatabase.php b/classes/Task/Restore/RestoreDatabase.php index 19fdce9ab..3caf17660 100644 --- a/classes/Task/Restore/RestoreDatabase.php +++ b/classes/Task/Restore/RestoreDatabase.php @@ -25,7 +25,7 @@ * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ -namespace PrestaShop\Module\AutoUpgrade\Task\Rollback; +namespace PrestaShop\Module\AutoUpgrade\Task\Restore; use Exception; use PrestaShop\Module\AutoUpgrade\Backup\BackupFinder; diff --git a/classes/Task/Restore/NoRestoreFound.php b/classes/Task/Restore/RestoreEmpty.php similarity index 94% rename from classes/Task/Restore/NoRestoreFound.php rename to classes/Task/Restore/RestoreEmpty.php index 8500d14b4..348c64208 100644 --- a/classes/Task/Restore/NoRestoreFound.php +++ b/classes/Task/Restore/RestoreEmpty.php @@ -25,14 +25,14 @@ * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ -namespace PrestaShop\Module\AutoUpgrade\Task\Rollback; +namespace PrestaShop\Module\AutoUpgrade\Task\Restore; use PrestaShop\Module\AutoUpgrade\Task\AbstractTask; use PrestaShop\Module\AutoUpgrade\Task\ExitCode; use PrestaShop\Module\AutoUpgrade\Task\TaskName; use PrestaShop\Module\AutoUpgrade\Task\TaskType; -class NoRestoreFound extends AbstractTask +class RestoreEmpty extends AbstractTask { const TASK_TYPE = TaskType::TASK_TYPE_RESTORE; diff --git a/classes/Task/Restore/RestoreFiles.php b/classes/Task/Restore/RestoreFiles.php index 6341e8a2e..4e64ec7c6 100644 --- a/classes/Task/Restore/RestoreFiles.php +++ b/classes/Task/Restore/RestoreFiles.php @@ -25,7 +25,7 @@ * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ -namespace PrestaShop\Module\AutoUpgrade\Task\Rollback; +namespace PrestaShop\Module\AutoUpgrade\Task\Restore; use Exception; use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames; diff --git a/classes/Task/Runner/AllBackupTasks.php b/classes/Task/Runner/AllBackupTasks.php index 772fd71ca..1791597b5 100644 --- a/classes/Task/Runner/AllBackupTasks.php +++ b/classes/Task/Runner/AllBackupTasks.php @@ -36,7 +36,7 @@ */ class AllBackupTasks extends ChainedTasks { - const initialTask = TaskName::TASK_BACKUP_FILES; + const initialTask = TaskName::TASK_BACKUP_INITIALIZATION; /** * @var string diff --git a/classes/Task/Runner/AllUpdateTasks.php b/classes/Task/Runner/AllUpdateTasks.php index 6946a7f55..441dd8604 100644 --- a/classes/Task/Runner/AllUpdateTasks.php +++ b/classes/Task/Runner/AllUpdateTasks.php @@ -64,7 +64,7 @@ public function setOptions(array $options): void if (!empty($options['channel'])) { $config = [ 'channel' => $options['channel'], - ]); + ]; $this->container->getUpgradeConfiguration()->validate($config); $this->container->getUpgradeConfiguration()->merge($config); } diff --git a/classes/Task/TaskName.php b/classes/Task/TaskName.php index 09b1c5b3a..7fd144a92 100644 --- a/classes/Task/TaskName.php +++ b/classes/Task/TaskName.php @@ -40,7 +40,7 @@ class TaskName // RESTORE const TASK_RESTORE = 'Restore'; - const TASK_NO_RESTORE_FOUND = 'NoRestoreFound'; + const TASK_RESTORE_EMPTY = 'RestoreEmpty'; const TASK_RESTORE_DATABASE = 'RestoreDatabase'; const TASK_RESTORE_FILES = 'RestoreFiles'; const TASK_RESTORE_COMPLETE = 'RestoreComplete'; diff --git a/classes/Task/Update/Unzip.php b/classes/Task/Update/Unzip.php index bda3836c9..89f1a3235 100644 --- a/classes/Task/Update/Unzip.php +++ b/classes/Task/Update/Unzip.php @@ -126,7 +126,7 @@ public function run(): int } $this->next = TaskName::TASK_UPDATE_FILES; - $this->logger->info($this->translator->trans('File extraction complete. Now upgrading files...')); + $this->logger->info($this->translator->trans('File extraction complete. Now updating files...')); $this->container->getAnalytics()->track('Backup Launched', Analytics::WITH_BACKUP_PROPERTIES); diff --git a/classes/UpgradeTools/TaskRepository.php b/classes/UpgradeTools/TaskRepository.php index 8c545d9fc..2d17ff01e 100644 --- a/classes/UpgradeTools/TaskRepository.php +++ b/classes/UpgradeTools/TaskRepository.php @@ -36,11 +36,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\Rollback\NoRestoreFound; -use PrestaShop\Module\AutoUpgrade\Task\Rollback\Restore; -use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreComplete; -use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreDatabase; -use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreFiles; +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\TaskName; use PrestaShop\Module\AutoUpgrade\Task\Update\CleanDatabase; use PrestaShop\Module\AutoUpgrade\Task\Update\Download; @@ -68,8 +68,8 @@ public static function get(string $step, UpgradeContainer $container): AbstractT // RESTORE case TaskName::TASK_RESTORE: return new Restore($container); - case TaskName::TASK_NO_RESTORE_FOUND: - return new NoRestoreFound($container); + case TaskName::TASK_RESTORE_EMPTY: + return new RestoreEmpty($container); case TaskName::TASK_RESTORE_DATABASE: return new RestoreDatabase($container); case TaskName::TASK_RESTORE_FILES: diff --git a/tests/unit/Progress/CompletionCalculatorTest.php b/tests/unit/Progress/CompletionCalculatorTest.php index 90ec876c1..a3bdbc525 100644 --- a/tests/unit/Progress/CompletionCalculatorTest.php +++ b/tests/unit/Progress/CompletionCalculatorTest.php @@ -26,7 +26,7 @@ use PHPUnit\Framework\TestCase; use PrestaShop\Module\AutoUpgrade\Progress\Backlog; use PrestaShop\Module\AutoUpgrade\Progress\CompletionCalculator; -use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreFiles; +use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreFiles; use PrestaShop\Module\AutoUpgrade\Task\Runner\SingleTask; use PrestaShop\Module\AutoUpgrade\Task\Update\UpdateDatabase; use PrestaShop\Module\AutoUpgrade\Task\Update\UpdateFiles; diff --git a/tests/unit/Task/Upgrade/BackupDbTest.php b/tests/unit/Task/Upgrade/BackupDbTest.php index 63f19cc3d..c93949c90 100644 --- a/tests/unit/Task/Upgrade/BackupDbTest.php +++ b/tests/unit/Task/Upgrade/BackupDbTest.php @@ -26,17 +26,20 @@ */ use PHPUnit\Framework\TestCase; -use PrestaShop\Module\AutoUpgrade\Task\Upgrade\BackupDb; +use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupDatabase; use PrestaShop\Module\AutoUpgrade\UpgradeContainer; -class BackupDbTest extends TestCase +class BackupDatabaseTest extends TestCase { + /** + * @throws Exception + */ public function testTablesAreIgnored() { define('_DB_PREFIX_', 'doge_'); $container = new UpgradeContainer(__DIR__, __DIR__ . '/..'); - $task = new BackupDb($container); + $task = new BackupDatabase($container); $tables = json_decode(file_get_contents(__DIR__ . '/../../../fixtures/listOfTablesInDb.json'), true); $expectedTables = $this->getExpectedTablesToSync();