Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #107 from yamilovs/master
Browse files Browse the repository at this point in the history
Add support for mysql 5.6.x.
  • Loading branch information
dizda authored Oct 18, 2016
2 parents e79a1dc + b7a1835 commit 2762587
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 35 deletions.
98 changes: 79 additions & 19 deletions Database/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,127 @@
class MySQL extends BaseDatabase
{
const DB_PATH = 'mysql';
const CONFIGURATION_FILE_NAME = 'mysql.cnf';

private $allDatabases;
private $database;
private $auth = '';
private $fileName;
private $ignoreTables = '';
private $params;

/**
* DB Auth.
*
* @param array $params
* @param array $params
* @param string $basePath
*/
public function __construct($params, $basePath)
{
parent::__construct($basePath);
$this->params = $params['mysql'];
}

$params = $params['mysql'];
$this->allDatabases = $params['all_databases'];
$this->database = $params['database'];
$this->auth = '';

if ($this->allDatabases) {
/**
* Prepare a database name and a file dump name for mysqldump command
*/
protected function prepareFileName()
{
if ($this->params['all_databases']) {
$this->database = '--all-databases';
$this->fileName = 'all-databases.sql';
} else {
$this->fileName = $this->database.'.sql';
$this->database = $this->params['database'];
$this->fileName = $this->database . '.sql';
}
}

if (isset($params['ignore_tables'])) {
foreach ($params['ignore_tables'] as $ignoreTable) {
if ($this->allDatabases) {
/**
* Prepare ignore tables attribute for mysqldump command
*/
protected function prepareIgnoreTables()
{
if (isset($this->params['ignore_tables'])) {
foreach ($this->params['ignore_tables'] as $ignoreTable) {
if ($this->params['all_databases']) {
if (false === strpos($ignoreTable, '.')) {
throw new \LogicException(
'When dumping all databases both database and table must be specified when ignoring table'
);
}
$this->ignoreTables .= sprintf('--ignore-table=%s ', $ignoreTable);
} else {
$this->ignoreTables .= sprintf('--ignore-table=%s.%s ', $this->database, $ignoreTable);
$this->ignoreTables .= sprintf('--ignore-table=%s.%s ', $this->params['database'], $ignoreTable);
}
}
}
}

/* if user is set, we add authentification */
if ($params['db_user']) {
$this->auth = sprintf('-u%s', $params['db_user']);
/**
* Prepare mysql configuration file for connection
*/
protected function prepareConfigurationFile()
{
$cnfFile = "[client]\n";
$cnfParams = array();
$configurationMapping = array(
'user' => 'db_user',
'password' => 'db_password',
'host' => 'db_host',
'port' => 'db_port',
);

if ($params['db_password']) {
$this->auth = sprintf("--host=\"%s\" --port=\"%d\" --user=\"%s\" --password=\"%s\"", $params['db_host'], $params['db_port'], $params['db_user'], $params['db_password']);
foreach ($configurationMapping as $key => $param) {
if ($this->params[$param]) {
$cnfParams[$key] = $this->params[$param];
}
}

if (!empty($cnfParams)) {
foreach ($cnfParams as $key => $value) {
$cnfFile .= "$key = \"$value\"\n";
}

$this->filesystem->dumpFile($this->getConfigurationFilePath(), $cnfFile, 0600);
$this->auth = sprintf("--defaults-extra-file=\"%s\"", $this->getConfigurationFilePath());
}
}

/**
* Remove mysql configuration file from backup files
*/
protected function removeConfigurationFile()
{
$this->filesystem->remove($this->getConfigurationFilePath());
}

/**
* Gets mysql configuration file full path
* @return string
*/
protected function getConfigurationFilePath()
{
return $this->dataPath . static::CONFIGURATION_FILE_NAME;
}

/**
* Prepare all necessary configurations for mysqldump command
*/
protected function prepareEnvironment()
{
$this->preparePath();
$this->prepareFileName();
$this->prepareIgnoreTables();
$this->prepareConfigurationFile();
}

/**
* {@inheritdoc}
*/
public function dump()
{
$this->preparePath();
$this->prepareEnvironment();
$this->execute($this->getCommand());
$this->removeConfigurationFile();
}

/**
Expand Down
54 changes: 38 additions & 16 deletions Tests/Database/MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
*/
class MySQLTest extends \PHPUnit_Framework_TestCase
{
protected function checkConfigurationFileExistsAndValid($user, $password, $host, $port)
{
$filePath = '/tmp/backup/mysql/mysql.cnf';
$cnfFileContent = "[client]\n";
$cnfFileContent .= $user ? "user = \"$user\"\n" : "";
$cnfFileContent .= $password ? "password = \"$password\"\n" : "";
$cnfFileContent .= $host ? "host = \"$host\"\n" : "";
$cnfFileContent .= $port ? "port = \"$port\"\n" : "";

$this->assertFileExists($filePath);
$this->assertContains(file_get_contents($filePath), $cnfFileContent);
}

/**
* @test
*/
Expand All @@ -23,9 +36,10 @@ public function shouldDumpAllDatabases()
'db_user' => 'root',
'db_password' => 'test',
),
), '/var/backup/');
), '/tmp/backup/');

$this->assertEquals($mysql->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" --all-databases > '/var/backup/mysql/all-databases.sql'");
$this->assertEquals($mysql->getCommand(), "mysqldump --defaults-extra-file=\"/tmp/backup/mysql/mysql.cnf\" --all-databases > '/tmp/backup/mysql/all-databases.sql'");
$this->checkConfigurationFileExistsAndValid('root', 'test', 'localhost', '3306');
}

/**
Expand All @@ -42,8 +56,11 @@ public function shouldDumpSpecifiedDatabase()
'db_user' => 'root',
'db_password' => 'test',
),
), '/var/backup/');

), '/tmp/backup/');

$this->assertEquals($mysql1->getCommand(), "mysqldump --defaults-extra-file=\"/tmp/backup/mysql/mysql.cnf\" dizbdd > '/tmp/backup/mysql/dizbdd.sql'");
$this->checkConfigurationFileExistsAndValid('root', 'test', 'localhost', '3306');

$mysql2 = new MySQLDummy(array(
'mysql' => array(
'all_databases' => false,
Expand All @@ -53,10 +70,10 @@ public function shouldDumpSpecifiedDatabase()
'db_user' => 'mysql',
'db_password' => 'somepwd',
),
), '/var/backup/');
), '/tmp/backup/');

$this->assertEquals($mysql1->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" dizbdd > '/var/backup/mysql/dizbdd.sql'");
$this->assertEquals($mysql2->getCommand(), "mysqldump --host=\"somehost\" --port=\"2222\" --user=\"mysql\" --password=\"somepwd\" somebdd > '/var/backup/mysql/somebdd.sql'");
$this->assertEquals($mysql2->getCommand(), "mysqldump --defaults-extra-file=\"/tmp/backup/mysql/mysql.cnf\" somebdd > '/tmp/backup/mysql/somebdd.sql'");
$this->checkConfigurationFileExistsAndValid('mysql', 'somepwd', 'somehost', '2222');

// dump specified database with no auth
$mysql = new MySQLDummy(array(
Expand All @@ -68,9 +85,10 @@ public function shouldDumpSpecifiedDatabase()
'db_user' => null,
'db_password' => null,
),
), '/var/backup/');
), '/tmp/backup/');

$this->assertEquals($mysql->getCommand(), 'mysqldump somebdd > \'/var/backup/mysql/somebdd.sql\'');
$this->assertEquals($mysql->getCommand(), 'mysqldump --defaults-extra-file="/tmp/backup/mysql/mysql.cnf" somebdd > \'/tmp/backup/mysql/somebdd.sql\'');
$this->checkConfigurationFileExistsAndValid(null, null, 'somehost', '2222');
}

/**
Expand All @@ -88,9 +106,10 @@ public function shouldDumpAllDatabasesWithNoAuth()
'db_user' => null,
'db_password' => null,
),
), '/var/backup/');
), '/tmp/backup/');

$this->assertEquals($mysql->getCommand(), 'mysqldump --all-databases > \'/var/backup/mysql/all-databases.sql\'');
$this->assertEquals($mysql->getCommand(), 'mysqldump --defaults-extra-file="/tmp/backup/mysql/mysql.cnf" --all-databases > \'/tmp/backup/mysql/all-databases.sql\'');
$this->checkConfigurationFileExistsAndValid(null, null, 'somehost', '2222');
}

/**
Expand All @@ -108,9 +127,10 @@ public function shouldIgnoreSpecifiedTablesForSpecifiedDatabase()
'db_password' => 'test',
'ignore_tables' => array('table1', 'table2'),
),
), '/var/backup/');
), '/tmp/backup/');

$this->assertEquals($mysql->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" dizbdd --ignore-table=dizbdd.table1 --ignore-table=dizbdd.table2 > '/var/backup/mysql/dizbdd.sql'");
$this->assertEquals($mysql->getCommand(), "mysqldump --defaults-extra-file=\"/tmp/backup/mysql/mysql.cnf\" dizbdd --ignore-table=dizbdd.table1 --ignore-table=dizbdd.table2 > '/tmp/backup/mysql/dizbdd.sql'");
$this->checkConfigurationFileExistsAndValid('root', 'test', 'localhost', '3306');
}

/**
Expand All @@ -128,9 +148,10 @@ public function shouldIgnoreSpecifiedTablesForAllDatabase()
'db_password' => 'test',
'ignore_tables' => array('db1.table1', 'db2.table2'),
),
), '/var/backup/');
), '/tmp/backup/');

$this->assertEquals($mysql->getCommand(), "mysqldump --host=\"localhost\" --port=\"3306\" --user=\"root\" --password=\"test\" --all-databases --ignore-table=db1.table1 --ignore-table=db2.table2 > '/var/backup/mysql/all-databases.sql'");
$this->assertEquals($mysql->getCommand(), "mysqldump --defaults-extra-file=\"/tmp/backup/mysql/mysql.cnf\" --all-databases --ignore-table=db1.table1 --ignore-table=db2.table2 > '/tmp/backup/mysql/all-databases.sql'");
$this->checkConfigurationFileExistsAndValid('root', 'test', 'localhost', '3306');
}

/**
Expand All @@ -149,7 +170,7 @@ public function shouldThrowExceptionIfDatabaseIsNotSpecifiedForIgnoredTableDumpi
'db_password' => 'test',
'ignore_tables' => array('table1'),
),
), '/var/backup/');
), '/tmp/backup/');

$mysql->getCommand();
}
Expand All @@ -159,6 +180,7 @@ class MySQLDummy extends MySQL
{
public function getCommand()
{
$this->prepareEnvironment();
return parent::getCommand();
}
}

0 comments on commit 2762587

Please sign in to comment.