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
Expand Up @@ -22,7 +22,7 @@ services:
- { name: drupal.command } - { name: drupal.command }
console.database_dump: console.database_dump:
class: Drupal\Console\Command\Database\DumpCommand class: Drupal\Console\Command\Database\DumpCommand
arguments: ['@app.root', '@console.shell_process'] arguments: ['@app.root', '@console.shell_process', '@database']
tags: tags:
- { name: drupal.command } - { name: drupal.command }
console.database_log_clear: console.database_log_clear:
Expand Down
115 changes: 95 additions & 20 deletions src/Command/Database/DumpCommand.php
Expand Up @@ -14,6 +14,7 @@
use Drupal\Console\Core\Command\Command; use Drupal\Console\Core\Command\Command;
use Drupal\Console\Command\Shared\ConnectTrait; use Drupal\Console\Command\Shared\ConnectTrait;
use Drupal\Console\Core\Utils\ShellProcess; use Drupal\Console\Core\Utils\ShellProcess;
use Drupal\Core\Database\Connection;


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


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


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


$databaseConnection = $this->escapeConnection($this->resolveConnection($database, $target)); $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) { if (!$file) {
$date = new \DateTime(); $date = new \DateTime();
$file = sprintf( $file = sprintf(
Expand All @@ -102,26 +140,63 @@ protected function execute(InputInterface $input, OutputInterface $output)


$command = null; $command = null;


if ($databaseConnection['driver'] == 'mysql') { if ($databaseConnection['driver'] == 'mysql') {
$command = sprintf( $command = sprintf(
'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" "%s" > "%s"', 'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" "%s" > "%s"',
$databaseConnection['username'], $databaseConnection['username'],
$databaseConnection['password'], $databaseConnection['password'],
$databaseConnection['host'], $databaseConnection['host'],
$databaseConnection['port'], $databaseConnection['port'],
$databaseConnection['database'], $databaseConnection['database'],
$file $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') { } elseif ($databaseConnection['driver'] == 'pgsql') {
$command = sprintf( $command = sprintf(
'PGPASSWORD="%s" pg_dumpall -w -U "%s" -h "%s" -p "%s" -l "%s" -f "%s"', 'PGPASSWORD="%s" pg_dumpall -w -U "%s" -h "%s" -p "%s" -l "%s" -f "%s"',
$databaseConnection['password'], $databaseConnection['password'],
$databaseConnection['username'], $databaseConnection['username'],
$databaseConnection['host'], $databaseConnection['host'],
$databaseConnection['port'], $databaseConnection['port'],
$databaseConnection['database'], $databaseConnection['database'],
$file $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) { if ($learning) {
Expand Down

0 comments on commit ca39d83

Please sign in to comment.