diff --git a/config/services/database.yml b/config/services/database.yml index 023188edd..59a2c2f95 100644 --- a/config/services/database.yml +++ b/config/services/database.yml @@ -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: diff --git a/src/Command/Database/DumpCommand.php b/src/Command/Database/DumpCommand.php index 59393f3b3..a67f4fe0a 100644 --- a/src/Command/Database/DumpCommand.php +++ b/src/Command/Database/DumpCommand.php @@ -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) {