Skip to content

Commit

Permalink
Merge pull request #31 from FabioBatSilva/mysql-cache-2.5
Browse files Browse the repository at this point in the history
Mysql cache 2.5
  • Loading branch information
robocoder committed May 16, 2014
2 parents 5bfe388 + 9bcbae8 commit fb17a0a
Show file tree
Hide file tree
Showing 9 changed files with 631 additions and 47 deletions.
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);
}
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);
}
}
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 src/VIPSoft/DoctrineDataFixturesExtension/Service/BackupService.php
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);
}
}
Loading

0 comments on commit fb17a0a

Please sign in to comment.