Skip to content

Commit

Permalink
Merge pull request #36 from DrillSergeant/refactor-stashcommands-to-s…
Browse files Browse the repository at this point in the history
…impledbal

TASK: Refactor stash commands to use SimpleDBAL for proper argument escaping
  • Loading branch information
mficzel authored Sep 13, 2019
2 parents 119bed9 + 82c540a commit b0d3b45
Showing 1 changed file with 51 additions and 19 deletions.
70 changes: 51 additions & 19 deletions Classes/Command/StashCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
use Neos\Flow\Annotations as Flow;
use Neos\Utility\Files as FileUtils;
use Neos\Flow\Core\Bootstrap;
use Sitegeist\MagicWand\DBAL\SimpleDBAL;

/**
* @Flow\Scope("singleton")
*/
class StashCommandController extends AbstractCommandController
{
/**
* @Flow\Inject
* @var SimpleDBAL
*/
protected $dbal;

/**
* Creates a new stash entry with the given name.
*
Expand Down Expand Up @@ -48,6 +55,15 @@ public function createCommand($name)

$this->addSecret($this->databaseConfiguration['user']);
$this->addSecret($this->databaseConfiguration['password']);
$this->addSecret(escapeshellcmd($this->databaseConfiguration['password']));
$this->addSecret(escapeshellarg(escapeshellcmd($this->databaseConfiguration['password'])));

################################################
# Fallback to default MySQL port if not given. #
################################################
if (!isset($this->databaseConfiguration['port'])) {
$this->databaseConfiguration['port'] = $this->dbal->getDefaultPort($this->databaseConfiguration['driver']);
}

######################
# Write Manifest #
Expand All @@ -72,15 +88,16 @@ public function createCommand($name)
######################

$this->renderHeadLine('Backup Database');

$this->executeLocalShellCommand(
'mysqldump --single-transaction --add-drop-table --host="%s" --user="%s" --password="%s" %s > %s',
[
$this->dbal->buildDumpCmd(
$this->databaseConfiguration['driver'],
$this->databaseConfiguration['host'],
(int)$this->databaseConfiguration['port'],
$this->databaseConfiguration['user'],
$this->databaseConfiguration['password'],
$this->databaseConfiguration['dbname'],
$databaseDestination
]
$this->databaseConfiguration['dbname']
) . ' > ' . $databaseDestination
);

###############################
Expand Down Expand Up @@ -289,6 +306,15 @@ protected function restoreStashEntry($source, $name, $force = false, $preserve =

$this->addSecret($this->databaseConfiguration['user']);
$this->addSecret($this->databaseConfiguration['password']);
$this->addSecret(escapeshellcmd($this->databaseConfiguration['password']));
$this->addSecret(escapeshellarg(escapeshellcmd($this->databaseConfiguration['password'])));

################################################
# Fallback to default MySQL port if not given. #
################################################
if (!isset($this->databaseConfiguration['port'])) {
$this->databaseConfiguration['port'] = $this->dbal->getDefaultPort($this->databaseConfiguration['driver']);
}

########################
# Drop and Recreate DB #
Expand All @@ -297,21 +323,26 @@ protected function restoreStashEntry($source, $name, $force = false, $preserve =
if ($keepDb == false) {
$this->renderHeadLine('Drop and Recreate DB');

$emptyLocalDbSql = 'DROP DATABASE `'
. $this->databaseConfiguration['dbname']
. '`; CREATE DATABASE `'
. $this->databaseConfiguration['dbname']
. '` collate utf8_unicode_ci;';
$emptyLocalDbSql = $this->dbal->flushDbSql(
$this->databaseConfiguration['driver'],
$this->databaseConfiguration['dbname']
);

$this->executeLocalShellCommand(
'echo %s | mysql --host=%s --user=%s --password=%s',
'echo %s | %s',
[
escapeshellarg($emptyLocalDbSql),
$this->databaseConfiguration['host'],
$this->databaseConfiguration['user'],
$this->databaseConfiguration['password']
$this->dbal->buildCmd(
$this->databaseConfiguration['driver'],
$this->databaseConfiguration['host'],
(int)$this->databaseConfiguration['port'],
$this->databaseConfiguration['user'],
$this->databaseConfiguration['password'],
$this->databaseConfiguration['dbname']
)
]
);

} else {
$this->renderHeadLine('Skipped (Drop and Recreate DB)');
}
Expand All @@ -321,15 +352,16 @@ protected function restoreStashEntry($source, $name, $force = false, $preserve =
######################

$this->renderHeadLine('Restore Database');

$this->executeLocalShellCommand(
'mysql --host="%s" --user="%s" --password="%s" %s < %s',
[
$this->dbal->buildCmd(
$this->databaseConfiguration['driver'],
$this->databaseConfiguration['host'],
(int)$this->databaseConfiguration['port'],
$this->databaseConfiguration['user'],
$this->databaseConfiguration['password'],
$this->databaseConfiguration['dbname'],
$source . '/database.sql'
]
$this->databaseConfiguration['dbname']
) . ' < ' . $source . '/database.sql'
);

################################
Expand Down

0 comments on commit b0d3b45

Please sign in to comment.