Skip to content
Permalink
Browse files

--exclude-cache option for database:dump command (#4019)

  • Loading branch information...
GuidoRobertone authored and enzolutions committed Apr 29, 2019
1 parent 3f87907 commit ca39d83c73967eae5a1393a2bdfa21681d187480
Showing with 96 additions and 21 deletions.
  1. +1 −1 config/services/database.yml
  2. +95 −20 src/Command/Database/DumpCommand.php
@@ -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:
@@ -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
{
@@ -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();
}
@@ -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']);
}
@@ -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(
@@ -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) {

0 comments on commit ca39d83

Please sign in to comment.
You can’t perform that action at this time.