Skip to content

Commit

Permalink
fix: make the process more extensible, avoid touching anything (#141)
Browse files Browse the repository at this point in the history
* fix: make the process more extensible, avoid touching anything

* fix: I'm assuming that in some cases, exiting the method is closing the file resource. Setting the tmpfile earlier should mean that this won't happen.
  • Loading branch information
jpswade authored Jan 27, 2021
1 parent 6a90048 commit 98194c7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 22 deletions.
17 changes: 13 additions & 4 deletions src/Databases/MongoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public function dumpToFile(string $dumpFile)
{
$this->guardAgainstIncompleteCredentials();

$command = $this->getDumpCommand($dumpFile);

$process = Process::fromShellCommandline($command, null, null, null, $this->timeout);
$process = $this->getProcess($dumpFile);

$process->run();

Expand All @@ -43,7 +41,7 @@ public function dumpToFile(string $dumpFile)
* @throws \Spatie\DbDumper\Exceptions\CannotStartDump
* @return void
*/
protected function guardAgainstIncompleteCredentials()
public function guardAgainstIncompleteCredentials()
{
foreach (['dbName', 'host'] as $requiredProperty) {
if (strlen($this->$requiredProperty) === 0) {
Expand Down Expand Up @@ -119,4 +117,15 @@ public function getDumpCommand(string $filename): string

return $this->echoToFile(implode(' ', $command), $filename);
}

/**
* @param string $dumpFile
* @return Process
*/
public function getProcess(string $dumpFile): Process
{
$command = $this->getDumpCommand($dumpFile);

return Process::fromShellCommandline($command, null, null, null, $this->timeout);
}
}
42 changes: 36 additions & 6 deletions src/Databases/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class MySql extends DbDumper
/** @var bool */
protected $createTables = true;

/** @var false|resource */
private $tempFileHandle;

public function __construct()
{
$this->port = 3306;
Expand Down Expand Up @@ -191,12 +194,9 @@ public function dumpToFile(string $dumpFile)
$this->guardAgainstIncompleteCredentials();

$tempFileHandle = tmpfile();
fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
$temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];

$command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);
$this->setTempFileHandle($tempFileHandle);

$process = Process::fromShellCommandline($command, null, null, null, $this->timeout);
$process = $this->getProcess($dumpFile);

$process->run();

Expand Down Expand Up @@ -320,7 +320,7 @@ public function getContentsOfCredentialsFile(): string
return implode(PHP_EOL, $contents);
}

protected function guardAgainstIncompleteCredentials()
public function guardAgainstIncompleteCredentials()
{
foreach (['userName', 'host'] as $requiredProperty) {
if (strlen($this->$requiredProperty) === 0) {
Expand All @@ -332,4 +332,34 @@ protected function guardAgainstIncompleteCredentials()
throw CannotStartDump::emptyParameter('dbName');
}
}

/**
* @param string $dumpFile
* @return Process
*/
public function getProcess(string $dumpFile): Process
{
fwrite($this->getTempFileHandle(), $this->getContentsOfCredentialsFile());
$temporaryCredentialsFile = stream_get_meta_data($this->getTempFileHandle())['uri'];

$command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);

return Process::fromShellCommandline($command, null, null, null, $this->timeout);
}

/**
* @return false|resource
*/
public function getTempFileHandle()
{
return $this->tempFileHandle;
}

/**
* @param false|resource $tempFileHandle
*/
public function setTempFileHandle($tempFileHandle)
{
$this->tempFileHandle = $tempFileHandle;
}
}
46 changes: 38 additions & 8 deletions src/Databases/PostgreSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class PostgreSql extends DbDumper
/** @var bool */
protected $createTables = true;

/** @var false|resource */
private $tempFileHandle;

public function __construct()
{
$this->port = 5432;
Expand Down Expand Up @@ -41,15 +44,10 @@ public function dumpToFile(string $dumpFile)
{
$this->guardAgainstIncompleteCredentials();

$command = $this->getDumpCommand($dumpFile);

$tempFileHandle = tmpfile();
fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
$temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
$this->setTempFileHandle($tempFileHandle);

$envVars = $this->getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile);

$process = Process::fromShellCommandline($command, null, $envVars, null, $this->timeout);
$process = $this->getProcess($dumpFile);

$process->run();

Expand Down Expand Up @@ -110,7 +108,7 @@ public function getContentsOfCredentialsFile(): string
return implode(':', $contents);
}

protected function guardAgainstIncompleteCredentials()
public function guardAgainstIncompleteCredentials()
{
foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
if (empty($this->$requiredProperty)) {
Expand All @@ -136,4 +134,36 @@ public function doNotCreateTables()

return $this;
}

/**
* @param string $dumpFile
* @return Process
*/
public function getProcess(string $dumpFile): Process
{
$command = $this->getDumpCommand($dumpFile);

fwrite($this->getTempFileHandle(), $this->getContentsOfCredentialsFile());
$temporaryCredentialsFile = stream_get_meta_data($this->getTempFileHandle())['uri'];

$envVars = $this->getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile);

return Process::fromShellCommandline($command, null, $envVars, null, $this->timeout);
}

/**
* @return false|resource
*/
public function getTempFileHandle()
{
return $this->tempFileHandle;
}

/**
* @param false|resource $tempFileHandle
*/
public function setTempFileHandle($tempFileHandle)
{
$this->tempFileHandle = $tempFileHandle;
}
}
15 changes: 12 additions & 3 deletions src/Databases/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class Sqlite extends DbDumper
*/
public function dumpToFile(string $dumpFile)
{
$command = $this->getDumpCommand($dumpFile);

$process = Process::fromShellCommandline($command, null, null, null, $this->timeout);
$process = $this->getProcess($dumpFile);

$process->run();

Expand Down Expand Up @@ -48,4 +46,15 @@ public function getDumpCommand(string $dumpFile): string

return $this->echoToFile($command, $dumpFile);
}

/**
* @param string $dumpFile
* @return Process
*/
public function getProcess(string $dumpFile): Process
{
$command = $this->getDumpCommand($dumpFile);

return Process::fromShellCommandline($command, null, null, null, $this->timeout);
}
}
2 changes: 1 addition & 1 deletion src/DbDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public function addExtraOptionAfterDbName(string $extraOptionAfterDbName)

abstract public function dumpToFile(string $dumpFile);

protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile)
public function checkIfDumpWasSuccessFul(Process $process, string $outputFile)
{
if (! $process->isSuccessful()) {
throw DumpFailed::processDidNotEndSuccessfully($process);
Expand Down

0 comments on commit 98194c7

Please sign in to comment.