From 8b9c8489f6fb8b4f556d804cc41bee2581ed8d5a Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Mon, 13 Apr 2015 09:19:39 +0000 Subject: [PATCH 1/6] add option for custom unix socket --- .../Database/Databases/MySQLDatabase.php | 18 ++++++++++++++++-- src/config/laravel-backup.php | 6 ++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/BackupHandlers/Database/Databases/MySQLDatabase.php b/src/BackupHandlers/Database/Databases/MySQLDatabase.php index 80b9144d..47b7d9b5 100644 --- a/src/BackupHandlers/Database/Databases/MySQLDatabase.php +++ b/src/BackupHandlers/Database/Databases/MySQLDatabase.php @@ -52,11 +52,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); @@ -81,4 +82,17 @@ protected function getDumpCommandPath() { return Config::get('laravel-backup.mysql.dump_command_path'); } + + /** + * Set the socket if one is specified in the configuration + */ + protected function getSocketArgument() + { + if(config('laravel-backup.unix_socket') != '') + { + return '--socket=' . config('laravel-backup.unix_socket'); + } + + return null; + } } diff --git a/src/config/laravel-backup.php b/src/config/laravel-backup.php index 11c810a2..e709f983 100644 --- a/src/config/laravel-backup.php +++ b/src/config/laravel-backup.php @@ -65,4 +65,10 @@ 'mysql' => [ 'dump_command_path' => '', ], + + /* + * Path to the unix socket file. You can leave this empty + * if the socket is located in the default location. + */ + 'unix_socket' => '', ]; From 971bedd48eb7f99fdc86ed0998482569b01dce2a Mon Sep 17 00:00:00 2001 From: Matthias De Winter Date: Mon, 13 Apr 2015 11:20:38 +0200 Subject: [PATCH 2/6] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 91967a01..3811a2cf 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,12 @@ return [ 'dump_command_path' => '', ], + /* + * Path to the unix socket file. You can leave this empty + * if the socket is located in the default location. + */ + 'unix_socket' => '', + ]; From 5d559668319426707e5723e16e783cf41238930c Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Mon, 13 Apr 2015 10:40:40 +0000 Subject: [PATCH 3/6] move unix_socket to database config --- .../Database/DatabaseBuilder.php | 6 +++++- .../Database/Databases/MySQLDatabase.php | 10 ++++++---- tests/DatabaseBuilderTest.php | 4 +++- tests/database/MySQLDatabaseTest.php | 19 +++++++++++++++++-- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/BackupHandlers/Database/DatabaseBuilder.php b/src/BackupHandlers/Database/DatabaseBuilder.php index e8fb4666..0875c556 100644 --- a/src/BackupHandlers/Database/DatabaseBuilder.php +++ b/src/BackupHandlers/Database/DatabaseBuilder.php @@ -17,6 +17,7 @@ public function __construct() public function getDatabase(array $realConfig) { + try { $this->buildMySQL($realConfig); } catch (Exception $e) { @@ -28,6 +29,8 @@ public function getDatabase(array $realConfig) protected function buildMySQL(array $config) { + $socket = isset($config['unix_socket']) ? $config['unix_socket'] : ''; + $port = isset($config['port']) ? $config['port'] : 3306; $this->database = new Databases\MySQLDatabase( @@ -36,7 +39,8 @@ protected function buildMySQL(array $config) $config['username'], $config['password'], $config['host'], - $port + $port, + $socket ); } } diff --git a/src/BackupHandlers/Database/Databases/MySQLDatabase.php b/src/BackupHandlers/Database/Databases/MySQLDatabase.php index 47b7d9b5..7c57240a 100644 --- a/src/BackupHandlers/Database/Databases/MySQLDatabase.php +++ b/src/BackupHandlers/Database/Databases/MySQLDatabase.php @@ -11,6 +11,7 @@ class MySQLDatabase implements DatabaseInterface protected $password; protected $host; protected $port; + protected $socket; /** * @param Console $console @@ -20,7 +21,7 @@ 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; @@ -28,6 +29,7 @@ public function __construct(Console $console, $database, $user, $password, $host $this->password = $password; $this->host = $host; $this->port = $port; + $this->socket = $socket; } /** @@ -80,7 +82,7 @@ public function getFileExtension() */ protected function getDumpCommandPath() { - return Config::get('laravel-backup.mysql.dump_command_path'); + return config('laravel-backup.mysql.dump_command_path'); } /** @@ -88,9 +90,9 @@ protected function getDumpCommandPath() */ protected function getSocketArgument() { - if(config('laravel-backup.unix_socket') != '') + if($this->socket != '') { - return '--socket=' . config('laravel-backup.unix_socket'); + return '--socket=' . $this->socket; } return null; diff --git a/tests/DatabaseBuilderTest.php b/tests/DatabaseBuilderTest.php index e4167433..36ebdc04 100644 --- a/tests/DatabaseBuilderTest.php +++ b/tests/DatabaseBuilderTest.php @@ -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); } diff --git a/tests/database/MySQLDatabaseTest.php b/tests/database/MySQLDatabaseTest.php index 9d4fbc6d..b604f057 100644 --- a/tests/database/MySQLDatabaseTest.php +++ b/tests/database/MySQLDatabaseTest.php @@ -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' ); } @@ -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') + ); + } +} \ No newline at end of file From fe2b80e06990a2283557727f5f8d34eebfc4f50a Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Mon, 13 Apr 2015 10:44:18 +0000 Subject: [PATCH 4/6] add newline --- tests/database/MySQLDatabaseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/database/MySQLDatabaseTest.php b/tests/database/MySQLDatabaseTest.php index b604f057..60b4bffb 100644 --- a/tests/database/MySQLDatabaseTest.php +++ b/tests/database/MySQLDatabaseTest.php @@ -60,4 +60,4 @@ public function testCustomSocket() $this->database->dump('testfile.sql') ); } -} \ No newline at end of file +} From d51a47fec8f1777fe29c6f3f3fae7dd7e9179cf5 Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Mon, 13 Apr 2015 10:45:40 +0000 Subject: [PATCH 5/6] update README and fix config --- README.md | 7 ------- src/config/laravel-backup.php | 6 ------ 2 files changed, 13 deletions(-) diff --git a/README.md b/README.md index 3811a2cf..ff3bf4d0 100644 --- a/README.md +++ b/README.md @@ -108,13 +108,6 @@ return [ 'mysql' => [ 'dump_command_path' => '', ], - - /* - * Path to the unix socket file. You can leave this empty - * if the socket is located in the default location. - */ - 'unix_socket' => '', - ]; diff --git a/src/config/laravel-backup.php b/src/config/laravel-backup.php index e709f983..11c810a2 100644 --- a/src/config/laravel-backup.php +++ b/src/config/laravel-backup.php @@ -65,10 +65,4 @@ 'mysql' => [ 'dump_command_path' => '', ], - - /* - * Path to the unix socket file. You can leave this empty - * if the socket is located in the default location. - */ - 'unix_socket' => '', ]; From 8fa0632ac7d67aa95e85831be9ce7c01d1adf9ac Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Mon, 13 Apr 2015 11:00:23 +0000 Subject: [PATCH 6/6] code style fixes --- src/BackupHandlers/Database/DatabaseBuilder.php | 4 ++-- src/BackupHandlers/Database/Databases/MySQLDatabase.php | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/BackupHandlers/Database/DatabaseBuilder.php b/src/BackupHandlers/Database/DatabaseBuilder.php index 0875c556..3b89d4a4 100644 --- a/src/BackupHandlers/Database/DatabaseBuilder.php +++ b/src/BackupHandlers/Database/DatabaseBuilder.php @@ -29,10 +29,10 @@ public function getDatabase(array $realConfig) protected function buildMySQL(array $config) { - $socket = isset($config['unix_socket']) ? $config['unix_socket'] : ''; - $port = isset($config['port']) ? $config['port'] : 3306; + $socket = isset($config['unix_socket']) ? $config['unix_socket'] : ''; + $this->database = new Databases\MySQLDatabase( $this->console, $config['database'], diff --git a/src/BackupHandlers/Database/Databases/MySQLDatabase.php b/src/BackupHandlers/Database/Databases/MySQLDatabase.php index 7c57240a..893ba119 100644 --- a/src/BackupHandlers/Database/Databases/MySQLDatabase.php +++ b/src/BackupHandlers/Database/Databases/MySQLDatabase.php @@ -87,6 +87,8 @@ protected function getDumpCommandPath() /** * Set the socket if one is specified in the configuration + * + * @return string */ protected function getSocketArgument() { @@ -95,6 +97,6 @@ protected function getSocketArgument() return '--socket=' . $this->socket; } - return null; + return ''; } }