Skip to content

Commit

Permalink
--exclude-cache option for database:dump command (#4019)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuidoRobertone authored and enzolutions committed Apr 29, 2019
1 parent 3f87907 commit ca39d83
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 21 deletions.
2 changes: 1 addition & 1 deletion config/services/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ services:
- { name: drupal.command }
console.database_dump:
class: Drupal\Console\Command\Database\DumpCommand
arguments: ['@app.root', '@console.shell_process']
arguments: ['@app.root', '@console.shell_process', '@database']
tags:
- { name: drupal.command }
console.database_log_clear:
Expand Down
115 changes: 95 additions & 20 deletions src/Command/Database/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Command\Shared\ConnectTrait;
use Drupal\Console\Core\Utils\ShellProcess;
use Drupal\Core\Database\Connection;

class DumpCommand extends Command
{
Expand All @@ -25,19 +26,26 @@ class DumpCommand extends Command
* @var ShellProcess
*/
protected $shellProcess;
/**
* @var Connection
*/
protected $database;

/**
* DumpCommand constructor.
*
* @param $appRoot
* @param ShellProcess $shellProcess
* @param Connection $database
*/
public function __construct(
$appRoot,
ShellProcess $shellProcess
ShellProcess $shellProcess,
Connection $database
) {
$this->appRoot = $appRoot;
$this->shellProcess = $shellProcess;
$this->database = $database;
parent::__construct();
}

Expand Down Expand Up @@ -73,6 +81,12 @@ protected function configure()
InputOption::VALUE_NONE,
$this->trans('commands.database.dump.options.gz')
)
->addOption(
'exclude-cache',
null,
InputOption::VALUE_NONE,
$this->trans('commands.database.dump.options.exclude.cache')
)
->setHelp($this->trans('commands.database.dump.help'))
->setAliases(['dbdu']);
}
Expand All @@ -87,9 +101,33 @@ protected function execute(InputInterface $input, OutputInterface $output)
$file = $input->getOption('file');
$learning = $input->getOption('learning');
$gz = $input->getOption('gz');
$excludeCache = $input->getOption('exclude-cache');

$databaseConnection = $this->escapeConnection($this->resolveConnection($database, $target));

if ($excludeCache) {
$query = '';
if ($databaseConnection['driver'] == 'mysql') {
$query = "SHOW TABLES LIKE 'cache_%'";
} elseif ($databaseConnection['driver'] == 'pgsql') {
$query = "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema' AND tablename LIKE 'cache_%'";
}

$result = $this->database
->query($query)
->fetchAll();

$excludeTables = [];
foreach ($result as $record) {
$table = array_values(json_decode(json_encode($record), true));
if ($databaseConnection['driver'] == 'mysql') {
$excludeTables[] = $databaseConnection['database'] . '.' . $table[0];
} elseif ($databaseConnection['driver'] == 'pgsql') {
$excludeTables[] = 'public' . '.' . $table[0];
}
}
}

if (!$file) {
$date = new \DateTime();
$file = sprintf(
Expand All @@ -102,26 +140,63 @@ protected function execute(InputInterface $input, OutputInterface $output)

$command = null;

if ($databaseConnection['driver'] == 'mysql') {
$command = sprintf(
'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" "%s" > "%s"',
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database'],
$file
);
if ($databaseConnection['driver'] == 'mysql') {
$command = sprintf(
'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" "%s" > "%s"',
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database'],
$file
);

if ($excludeCache) {
$ignoreTable = '';
foreach ($excludeTables as $table) {
$ignoreTable .= "--ignore-table=\"{$table}\" ";
}

$command = sprintf(
'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" %s "%s"> "%s"',
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port'],
$ignoreTable,
$databaseConnection['database'],
$file
);

}
} elseif ($databaseConnection['driver'] == 'pgsql') {
$command = sprintf(
'PGPASSWORD="%s" pg_dumpall -w -U "%s" -h "%s" -p "%s" -l "%s" -f "%s"',
$databaseConnection['password'],
$databaseConnection['username'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database'],
$file
);
$command = sprintf(
'PGPASSWORD="%s" pg_dumpall -w -U "%s" -h "%s" -p "%s" -l "%s" -f "%s"',
$databaseConnection['password'],
$databaseConnection['username'],
$databaseConnection['host'],
$databaseConnection['port'],
$databaseConnection['database'],
$file
);

if ($excludeCache) {
$ignoreTable = '';
foreach ($excludeTables as $table) {
$ignoreTable .= "-T \"{$table}\" ";
}

$command = sprintf(
'PGPASSWORD="%s" pg_dump -w -U "%s" -h "%s" -p "%s" -f "%s" %s-d "%s"',
$databaseConnection['password'],
$databaseConnection['username'],
$databaseConnection['host'],
$databaseConnection['port'],
$file,
$ignoreTable,
$databaseConnection['database']
);
}
}

if ($learning) {
Expand Down

0 comments on commit ca39d83

Please sign in to comment.