Skip to content

Commit 26185ac

Browse files
MAGETWO-84125: 12064: Database Rollback not working with magento 2.1.9? #12108
2 parents 8997335 + be14c70 commit 26185ac

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

lib/internal/Magento/Framework/Setup/BackupRollback.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ public function dbRollback($rollbackFile)
242242
$dbRollback->setTime($time[0]);
243243
$this->log->log('DB rollback is starting...');
244244
$dbRollback->setResourceModel($this->objectManager->create(\Magento\Backup\Model\ResourceModel\Db::class));
245+
if ($dbRollback->getBackupFilename() !== $rollbackFile) {
246+
$correctName = $this->getCorrectFileNameWithoutPrefix($dbRollback, $rollbackFile);
247+
$dbRollback->setName($correctName);
248+
}
245249
$dbRollback->rollback();
246250
$this->log->log('DB rollback filename: ' . $dbRollback->getBackupFilename());
247251
$this->log->log('DB rollback path: ' . $dbRollback->getBackupPath());
@@ -329,4 +333,25 @@ public function getDBDiskSpace()
329333
$dbBackup = $this->objectManager->create(\Magento\Framework\Backup\Db::class);
330334
return $dbBackup->getDBSize();
331335
}
336+
337+
/**
338+
* Get correct file name without prefix.
339+
*
340+
* @param \Magento\Framework\Backup\Db $dbRollback
341+
* @param string $rollbackFile
342+
*
343+
* @return string
344+
*/
345+
private function getCorrectFileNameWithoutPrefix(\Magento\Framework\Backup\Db $dbRollback, $rollbackFile)
346+
{
347+
$namePrefix = $dbRollback->getTime() . '_' . $dbRollback->getType();
348+
//delete prefix.
349+
$fileNameWithoutPrefix = str_replace($namePrefix, '', $rollbackFile);
350+
//change '_' to ' '.
351+
$fileNameWithoutPrefix = str_replace('_', ' ', $fileNameWithoutPrefix);
352+
//delete file extension.
353+
$fileNameWithoutPrefix = pathinfo($fileNameWithoutPrefix, PATHINFO_FILENAME);
354+
355+
return $fileNameWithoutPrefix;
356+
}
332357
}

lib/internal/Magento/Framework/Setup/Test/Unit/BackupRollbackTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public function testMediaRollback()
181181
public function testDbBackup()
182182
{
183183
$this->setupDbBackupRollback();
184+
$this->database->expects($this->once())->method('getBackupFilename')->willReturn('RollbackFile_A.gz');
184185
$this->database->expects($this->once())->method('create');
185186
$this->file->expects($this->once())->method('isExists')->willReturn(false);
186187
$this->file->expects($this->once())->method('createDirectory');
@@ -190,12 +191,20 @@ public function testDbBackup()
190191
public function testDbRollback()
191192
{
192193
$this->setupDbBackupRollback();
194+
193195
$this->database->expects($this->once())->method('rollback');
196+
$this->database->expects($this->exactly(2))->method('getBackupFilename')
197+
->willReturnOnConsecutiveCalls('test', '1510140748_db_test_backup');
198+
$this->database->expects($this->once())->method('getTime')->willReturn(1510140748);
199+
$this->database->expects($this->once())->method('getType')->willReturn('db');
200+
$this->database->expects($this->once())->method('setName')->with(' test backup');
201+
194202
$this->file->expects($this->once())
195203
->method('isExists')
196-
->with($this->path . '/backups/12345_db.sql')
204+
->with($this->path . '/backups/1510140748_db_test_backup.sql')
197205
->willReturn(true);
198-
$this->model->dbRollback('12345_db.sql');
206+
207+
$this->model->dbRollback('1510140748_db_test_backup.sql');
199208
}
200209

201210
private function setupCodeBackupRollback()
@@ -226,9 +235,6 @@ private function setupDbBackupRollback()
226235
->method('setBackupExtension');
227236
$this->database->expects($this->once())
228237
->method('setTime');
229-
$this->database->expects($this->once())
230-
->method('getBackupFilename')
231-
->willReturn('RollbackFile_A.gz');
232238
$this->database->expects($this->atLeastOnce())
233239
->method('getBackupPath')
234240
->willReturn('pathToFile/12345_db.sql');

0 commit comments

Comments
 (0)