-
-
Notifications
You must be signed in to change notification settings - Fork 761
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 #60 from Jamesking56/feature/postgresql-support
PostgreSQL support
- Loading branch information
Showing
6 changed files
with
216 additions
and
8 deletions.
There are no files selected for viewing
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
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
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,91 @@ | ||
<?php | ||
|
||
namespace Spatie\Backup\BackupHandlers\Database\Databases; | ||
|
||
use Spatie\Backup\Console; | ||
|
||
class PgSQLDatabase implements DatabaseInterface | ||
{ | ||
protected $console; | ||
protected $database; | ||
protected $schema; | ||
protected $username; | ||
protected $password; | ||
protected $host; | ||
protected $port; | ||
|
||
/** | ||
* @param Console $console | ||
* @param $database | ||
* @param string $schema | ||
* @param $username | ||
* @param $password | ||
* @param string $host | ||
* @param int $port | ||
* @param string $socket | ||
*/ | ||
public function __construct(Console $console, $database, $schema, $username, $password, $host, $port) | ||
{ | ||
$this->console = $console; | ||
$this->database = $database; | ||
$this->schema = $schema; | ||
$this->username = $username; | ||
$this->password = $password; | ||
$this->host = $host; | ||
$this->port = $port; | ||
} | ||
|
||
/** | ||
* Create a database dump. | ||
* | ||
* @param $destinationFile | ||
* | ||
* @return bool | ||
*/ | ||
public function dump($destinationFile) | ||
{ | ||
$command = sprintf('export PGHOST && %spg_dump '.(!$this->useCopy() ? '--inserts' : '').' --schema=%s %s > %s', | ||
$this->getDumpCommandPath(), | ||
escapeshellarg($this->schema), | ||
escapeshellarg($this->database), | ||
escapeshellarg($destinationFile) | ||
); | ||
|
||
$env = [ | ||
'PGHOST' => $this->host, | ||
'PGUSER' => $this->username, | ||
'PGPASSWORD' => $this->password, | ||
'PGPORT' => $this->port | ||
]; | ||
|
||
return $this->console->run($command, config('laravel-backup.pgsql.timeoutInSeconds'), $env); | ||
} | ||
|
||
/** | ||
* Return the file extension of a dump file (sql, ...). | ||
* | ||
* @return string | ||
*/ | ||
public function getFileExtension() | ||
{ | ||
return 'sql'; | ||
} | ||
|
||
/** | ||
* Get the path to the pgsql_dump. | ||
* | ||
* @return string | ||
*/ | ||
protected function getDumpCommandPath() | ||
{ | ||
return config('laravel-backup.pgsql.dump_command_path'); | ||
} | ||
|
||
/** | ||
* Determine if COPY should be used instead of INSERT. | ||
*/ | ||
protected function useCopy() | ||
{ | ||
return config('laravel-backup.pgsql.use_copy'); | ||
} | ||
} |
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
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
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,60 @@ | ||
<?php | ||
|
||
use Mockery as m; | ||
use Spatie\Backup\BackupHandlers\Database\Databases\PgSQLDatabase; | ||
|
||
class PgSQLDatabaseTest extends Orchestra\Testbench\TestCase | ||
{ | ||
protected $console; | ||
protected $database; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->console = m::mock('Spatie\Backup\Console'); | ||
|
||
$this->database = new PgSQLDatabase( | ||
$this->console, | ||
'testDatabase', | ||
'public', | ||
'testUser', | ||
'password', | ||
'localhost', | ||
'5432' | ||
); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
m::close(); | ||
} | ||
|
||
public function testFileExtension() | ||
{ | ||
$this->assertEquals( | ||
'sql', $this->database->getFileExtension() | ||
); | ||
} | ||
|
||
public function testDump() | ||
{ | ||
$this->console->shouldReceive('run') | ||
->with(m::on(function ($parameter) { | ||
$pattern = "/pg_dump --inserts --schema='public' 'testDatabase' > 'testfile.sql'/"; | ||
|
||
return preg_match($pattern, $parameter) == true; | ||
}), null, [ | ||
'PGHOST' => 'localhost', | ||
'PGUSER' => 'testUser', | ||
'PGPASSWORD' => 'password', | ||
'PGPORT' => '5432' | ||
]) | ||
->once() | ||
->andReturn(true); | ||
|
||
$this->assertTrue( | ||
$this->database->dump('testfile.sql') | ||
); | ||
} | ||
} |