Skip to content

Commit

Permalink
[2.3-develop] Forwardport of #12108
Browse files Browse the repository at this point in the history
  • Loading branch information
magento-engcom-team committed Feb 1, 2018
1 parent 3881f04 commit 63958cd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
26 changes: 25 additions & 1 deletion lib/internal/Magento/Framework/Setup/BackupRollback.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Backup\Exception\NotEnoughPermissions;
use Magento\Framework\Backup\Factory;
use Magento\Framework\Backup\Filesystem;
use Magento\Framework\Backup\Filesystem\Helper;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\Driver\File;
Expand Down Expand Up @@ -245,6 +244,10 @@ public function dbRollback($rollbackFile, $keepSourceFile = false)
$this->log->log('DB rollback is starting...');
$dbRollback->setKeepSourceFile($keepSourceFile);
$dbRollback->setResourceModel($this->objectManager->create(\Magento\Backup\Model\ResourceModel\Db::class));
if ($dbRollback->getBackupFilename() !== $rollbackFile) {
$correctName = $this->getCorrectFileNameWithoutPrefix($dbRollback, $rollbackFile);
$dbRollback->setName($correctName);
}
$dbRollback->rollback();
$this->log->log('DB rollback filename: ' . $dbRollback->getBackupFilename());
$this->log->log('DB rollback path: ' . $dbRollback->getBackupPath());
Expand Down Expand Up @@ -332,4 +335,25 @@ public function getDBDiskSpace()
$dbBackup = $this->objectManager->create(\Magento\Framework\Backup\Db::class);
return $dbBackup->getDBSize();
}

/**
* Get correct file name without prefix.
*
* @param \Magento\Framework\Backup\Db $dbRollback
* @param string $rollbackFile
*
* @return string
*/
private function getCorrectFileNameWithoutPrefix(\Magento\Framework\Backup\Db $dbRollback, $rollbackFile)
{
$namePrefix = $dbRollback->getTime() . '_' . $dbRollback->getType();
//delete prefix.
$fileNameWithoutPrefix = str_replace($namePrefix, '', $rollbackFile);
//change '_' to ' '.
$fileNameWithoutPrefix = str_replace('_', ' ', $fileNameWithoutPrefix);
//delete file extension.
$fileNameWithoutPrefix = pathinfo($fileNameWithoutPrefix, PATHINFO_FILENAME);

return $fileNameWithoutPrefix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public function testMediaRollback()
public function testDbBackup()
{
$this->setupDbBackupRollback();
$this->database->expects($this->once())->method('getBackupFilename')->willReturn('RollbackFile_A.gz');
$this->database->expects($this->once())->method('create');
$this->file->expects($this->once())->method('isExists')->willReturn(false);
$this->file->expects($this->once())->method('createDirectory');
Expand All @@ -190,12 +191,20 @@ public function testDbBackup()
public function testDbRollback()
{
$this->setupDbBackupRollback();

$this->database->expects($this->once())->method('rollback');
$this->database->expects($this->exactly(2))->method('getBackupFilename')
->willReturnOnConsecutiveCalls('test', '1510140748_db_test_backup');
$this->database->expects($this->once())->method('getTime')->willReturn(1510140748);
$this->database->expects($this->once())->method('getType')->willReturn('db');
$this->database->expects($this->once())->method('setName')->with(' test backup');

$this->file->expects($this->once())
->method('isExists')
->with($this->path . '/backups/12345_db.sql')
->with($this->path . '/backups/1510140748_db_test_backup.sql')
->willReturn(true);
$this->model->dbRollback('12345_db.sql');

$this->model->dbRollback('1510140748_db_test_backup.sql');
}

private function setupCodeBackupRollback()
Expand Down Expand Up @@ -226,9 +235,6 @@ private function setupDbBackupRollback()
->method('setBackupExtension');
$this->database->expects($this->once())
->method('setTime');
$this->database->expects($this->once())
->method('getBackupFilename')
->willReturn('RollbackFile_A.gz');
$this->database->expects($this->atLeastOnce())
->method('getBackupPath')
->willReturn('pathToFile/12345_db.sql');
Expand Down

0 comments on commit 63958cd

Please sign in to comment.