Skip to content

Commit

Permalink
Merge pull request #25 from MatthiasDeWinter/master
Browse files Browse the repository at this point in the history
Add option to use custom unix_socket
  • Loading branch information
freekmurze committed Apr 13, 2015
2 parents 1462e2b + 8fa0632 commit 5682696
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ return [
'mysql' => [
'dump_command_path' => '',
],

];


Expand Down
6 changes: 5 additions & 1 deletion src/BackupHandlers/Database/DatabaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct()

public function getDatabase(array $realConfig)
{

try {
$this->buildMySQL($realConfig);
} catch (Exception $e) {
Expand All @@ -30,13 +31,16 @@ protected function buildMySQL(array $config)
{
$port = isset($config['port']) ? $config['port'] : 3306;

$socket = isset($config['unix_socket']) ? $config['unix_socket'] : '';

$this->database = new Databases\MySQLDatabase(
$this->console,
$config['database'],
$config['username'],
$config['password'],
$config['host'],
$port
$port,
$socket
);
}
}
26 changes: 22 additions & 4 deletions src/BackupHandlers/Database/Databases/MySQLDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MySQLDatabase implements DatabaseInterface
protected $password;
protected $host;
protected $port;
protected $socket;

/**
* @param Console $console
Expand All @@ -20,14 +21,15 @@ class MySQLDatabase implements DatabaseInterface
* @param $host
* @param $port
*/
public function __construct(Console $console, $database, $user, $password, $host, $port)
public function __construct(Console $console, $database, $user, $password, $host, $port, $socket)
{
$this->console = $console;
$this->database = $database;
$this->user = $user;
$this->password = $password;
$this->host = $host;
$this->port = $port;
$this->socket = $socket;
}

/**
Expand All @@ -52,11 +54,12 @@ public function dump($destinationFile)
);
$temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];

$command = sprintf('%smysqldump --defaults-extra-file=%s --skip-comments --skip-extended-insert %s > %s',
$command = sprintf('%smysqldump --defaults-extra-file=%s --skip-comments --skip-extended-insert %s > %s %s',
$this->getDumpCommandPath(),
escapeshellarg($temporaryCredentialsFile),
escapeshellarg($this->database),
escapeshellarg($destinationFile)
escapeshellarg($destinationFile),
escapeshellarg($this->getSocketArgument())
);

return $this->console->run($command);
Expand All @@ -79,6 +82,21 @@ public function getFileExtension()
*/
protected function getDumpCommandPath()
{
return Config::get('laravel-backup.mysql.dump_command_path');
return config('laravel-backup.mysql.dump_command_path');
}

/**
* Set the socket if one is specified in the configuration
*
* @return string
*/
protected function getSocketArgument()
{
if($this->socket != '')
{
return '--socket=' . $this->socket;
}

return '';
}
}
4 changes: 3 additions & 1 deletion tests/DatabaseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public function testMySQL()
'port' => '3307',
];

$socket = '/var/run/mysqld/mysqld.sock';

$databaseBuilder = new DatabaseBuilder();
$database = $databaseBuilder->getDatabase($config);
$database = $databaseBuilder->getDatabase($config, $socket);

$this->assertInstanceOf('Spatie\Backup\BackupHandlers\Database\Databases\MySQLDatabase', $database);
}
Expand Down
17 changes: 16 additions & 1 deletion tests/database/MySQLDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function setUp()
$this->console = m::mock('Spatie\Backup\Console');

$this->database = new MySQLDatabase(
$this->console, 'testDatabase', 'testUser', 'password', 'localhost', '3306'
$this->console, 'testDatabase', 'testUser', 'password', 'localhost', '3306', '/var/run/mysqld/mysqld.sock'
);
}

Expand Down Expand Up @@ -45,4 +45,19 @@ public function testDump()
$this->database->dump('testfile.sql')
);
}

public function testCustomSocket()
{
$this->database = new MySQLDatabase(
$this->console, 'testDatabase', 'testUser', 'password', 'localhost', '3306', 'customSocket.sock'
);
$this->console->shouldReceive('run')
->with("/mysqldump --defaults-extra-file='(.*)' --skip-comments --skip-extended-insert 'testDatabase' > 'testfile.sql' '--socket=customSocket.sock'/")
->once()
->andReturn(true);

$this->assertTrue(
$this->database->dump('testfile.sql')
);
}
}

0 comments on commit 5682696

Please sign in to comment.