-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from FabioBatSilva/mysql-cache-2.5
Mysql cache 2.5
- Loading branch information
Showing
9 changed files
with
631 additions
and
47 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/VIPSoft/DoctrineDataFixturesExtension/Service/Backup/BackupInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* @copyright 2014 Anthon Pang | ||
* @license MIT | ||
*/ | ||
|
||
namespace VIPSoft\DoctrineDataFixturesExtension\Service\Backup; | ||
|
||
/** | ||
* Backup platform interface | ||
* | ||
* @author Fabio B. Silva <fabio.bat.silva@gmail.com> | ||
*/ | ||
interface BackupInterface | ||
{ | ||
/** | ||
* Create a backup file for the given database | ||
* | ||
* @param string $database | ||
* @param string $file | ||
*/ | ||
public function create($database, $file, array $params); | ||
|
||
/** | ||
* Restore the backup file into the given database | ||
* | ||
* @param string $database | ||
* @param string $file | ||
*/ | ||
public function restore($database, $file, array $params); | ||
} |
100 changes: 100 additions & 0 deletions
100
src/VIPSoft/DoctrineDataFixturesExtension/Service/Backup/MysqlDumpBackup.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
/** | ||
* @copyright 2014 Anthon Pang | ||
* @license MIT | ||
*/ | ||
|
||
namespace VIPSoft\DoctrineDataFixturesExtension\Service\Backup; | ||
|
||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* Mysql dump backup | ||
* | ||
* @author Fabio B. Silva <fabio.bat.silva@gmail.com> | ||
*/ | ||
class MysqlDumpBackup implements BackupInterface | ||
{ | ||
private $mysqldumpBin = 'mysqldump'; | ||
private $mysqlBin = 'mysql'; | ||
|
||
/** | ||
* @param string $bin | ||
*/ | ||
public function setMysqldumpBin($bin) | ||
{ | ||
$this->mysqldumpBin = $bin; | ||
} | ||
|
||
/** | ||
* @param string $bin | ||
*/ | ||
public function setMysqlBin($bin) | ||
{ | ||
$this->mysqlBin = $bin; | ||
} | ||
|
||
/** | ||
* @param string $command | ||
* | ||
* @return integer | ||
* | ||
* @throws \RuntimeException | ||
*/ | ||
protected function runCommand($command) | ||
{ | ||
$process = new Process($command); | ||
|
||
$process->run(); | ||
|
||
if ( ! $process->isSuccessful()) { | ||
throw new \RuntimeException($process->getErrorOutput()); | ||
} | ||
|
||
return $process->getExitCode(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create($database, $file, array $params) | ||
{ | ||
$command = sprintf("%s %s > %s", $this->mysqldumpBin, escapeshellarg($database), escapeshellarg($file)); | ||
|
||
if (isset($params['host'])) { | ||
$command .= sprintf(" -h%s", escapeshellarg($params['host'])); | ||
} | ||
|
||
if (isset($params['user'])) { | ||
$command .= sprintf(" -u%s", escapeshellarg($params['user'])); | ||
} | ||
|
||
if (isset($params['password'])) { | ||
$command .= sprintf(" -p%s", escapeshellarg($params['password'])); | ||
} | ||
|
||
$this->runCommand($command); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function restore($database, $file, array $params) | ||
{ | ||
$command = sprintf("%s %s < %s", $this->mysqlBin, escapeshellarg($database), escapeshellarg($file)); | ||
|
||
if (isset($params['host'])) { | ||
$command .= sprintf(" -h%s", escapeshellarg($params['host'])); | ||
} | ||
|
||
if (isset($params['user'])) { | ||
$command .= sprintf(" -u%s", escapeshellarg($params['user'])); | ||
} | ||
|
||
if (isset($params['password'])) { | ||
$command .= sprintf(" -p%s", escapeshellarg($params['password'])); | ||
} | ||
|
||
$this->runCommand($command); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/VIPSoft/DoctrineDataFixturesExtension/Service/Backup/SqliteCopyBackup.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* @copyright 2014 Anthon Pang | ||
* @license MIT | ||
*/ | ||
|
||
namespace VIPSoft\DoctrineDataFixturesExtension\Service\Backup; | ||
|
||
/** | ||
* Sqlite copy backup | ||
* | ||
* @author Fabio B. Silva <fabio.bat.silva@gmail.com> | ||
*/ | ||
class SqliteCopyBackup implements BackupInterface | ||
{ | ||
/** | ||
* Get path to .db file | ||
* | ||
* @return string | ||
*/ | ||
private function getDatabaseFile(array $params) | ||
{ | ||
if ( ! isset($params['path'])) { | ||
throw new \RuntimeException("Invalid sqlite path config"); | ||
} | ||
|
||
return $params['path']; | ||
} | ||
|
||
/** | ||
* Makes a copy of the file source to dest. | ||
* | ||
* @param $source string | ||
* @param $target string | ||
*/ | ||
public function copy($source, $dest) | ||
{ | ||
if ( ! copy($source, $target)) { | ||
throw new \RuntimeException("Unable to copy '$source' to '$dest'"); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create($database, $file, array $params) | ||
{ | ||
$this->copy($this->getDatabaseFile($params), $file); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function restore($database, $file, array $params) | ||
{ | ||
$this->copy($file, $this->getDatabaseFile($params)); | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
src/VIPSoft/DoctrineDataFixturesExtension/Service/BackupService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
/** | ||
* @copyright 2014 Anthon Pang | ||
* @license MIT | ||
*/ | ||
|
||
namespace VIPSoft\DoctrineDataFixturesExtension\Service; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use VIPSoft\DoctrineDataFixturesExtension\Service\Backup\BackupInterface; | ||
|
||
/** | ||
* Data Backup Service | ||
* | ||
* @author Fabio B. Silva <fabio.bat.silva@gmail.com> | ||
*/ | ||
class BackupService | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $cacheDir; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $platformBackupMap; | ||
|
||
/** | ||
* @param string $cacheDir | ||
*/ | ||
public function setCacheDir($cacheDir) | ||
{ | ||
$this->cacheDir = $cacheDir; | ||
} | ||
|
||
/** | ||
* @param array $map | ||
*/ | ||
public function setPlatformBackupMap(array $map) | ||
{ | ||
foreach ($map as $key => $value) { | ||
$this->setPlatformBackup($key, $value); | ||
}; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getPlatformBackupMap() | ||
{ | ||
return $this->platformBackupMap; | ||
} | ||
|
||
/** | ||
* @param string $platformName | ||
* @param VIPSoft\DoctrineDataFixturesExtension\Service\Backup\BackupInterface $backup | ||
*/ | ||
public function setPlatformBackup($platformName, BackupInterface $backup) | ||
{ | ||
$this->platformBackupMap[$platformName] = $backup; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return \VIPSoft\DoctrineDataFixturesExtension\Service\Backup\BackupInterface | ||
*/ | ||
public function getPlatformBackup($name) | ||
{ | ||
$map = $this->getPlatformBackupMap(); | ||
$item = isset($map[$name]) ? $map[$name] : null; | ||
|
||
if ($item === null) { | ||
throw new \RuntimeException('Unsupported platform '. $name); | ||
} | ||
|
||
return $item; | ||
} | ||
|
||
/** | ||
* @param string $hash | ||
* | ||
* @return string | ||
*/ | ||
protected function getBackupFile($hash) | ||
{ | ||
return $this->cacheDir . DIRECTORY_SEPARATOR .'test_' . $hash; | ||
} | ||
|
||
/** | ||
* Check if there is a backup | ||
* @param string $hash | ||
* | ||
* @return boolean | ||
*/ | ||
public function hasBackup($hash) | ||
{ | ||
return file_exists($this->getBackupFile($hash)); | ||
} | ||
|
||
/** | ||
* Create a backup for the given connection / hash | ||
* | ||
* @param \Doctrine\DBAL\Connection $connection | ||
* @param string $hash | ||
*/ | ||
public function createBackup(Connection $connection, $hash) | ||
{ | ||
$platform = $connection->getDatabasePlatform(); | ||
$filename = $this->getBackupFile($hash); | ||
$database = $connection->getDatabase(); | ||
$params = $connection->getParams(); | ||
$platformName = $platform->getName(); | ||
|
||
$this->getPlatformBackup($platformName)->create($database, $filename, $params); | ||
} | ||
|
||
/** | ||
* Restore the backup for the given connection / hash | ||
* | ||
* @param \Doctrine\DBAL\Connection $connection | ||
* @param string $hash | ||
*/ | ||
public function restoreBackup(Connection $connection, $hash) | ||
{ | ||
$platform = $connection->getDatabasePlatform(); | ||
$filename = $this->getBackupFile($hash); | ||
$database = $connection->getDatabase(); | ||
$params = $connection->getParams(); | ||
$platformName = $platform->getName(); | ||
|
||
$this->getPlatformBackup($platformName)->restore($database, $filename, $params); | ||
} | ||
} |
Oops, something went wrong.