GdprSqlMysql.php in General Data Protection Regulation 3.0.x
File
modules/gdpr_dump/src/Sql/GdprSqlMysql.php
View source
<?php
namespace Drupal\gdpr_dump\Sql;
use Consolidation\SiteProcess\Util\Escape;
use Drupal\gdpr_dump\Form\SettingsForm;
use Drupal\gdpr_dump\Service\GdprSqlDump;
use Drush\Drush;
use Drush\Sql\SqlMysql;
use function array_flip;
use function array_key_exists;
use function array_keys;
use function array_merge;
use function implode;
use function str_replace;
use function trim;
class GdprSqlMysql extends SqlMysql {
protected $gdprDumpConfig;
protected $tablesToSkip = [];
protected $tablesToAnonymize = [];
public function __construct($dbSpec, $options) {
parent::__construct($dbSpec, $options);
$this->gdprDumpConfig = \Drupal::config(SettingsForm::GDPR_DUMP_CONF_KEY);
$this->tablesToAnonymize = $this->gdprDumpConfig
->get('mapping') ?? [];
$this->tablesToSkip = array_keys($this->gdprDumpConfig
->get('empty_tables') ?? []);
}
public function dump() {
$file = $this
->getOption('result-file');
$fileSuffix = '';
$tableSelection = $this
->getExpandedTableSelection($this
->getOptions(), $this
->listTables());
$file = $this
->dumpFile($file);
$cmd = trim($this
->dumpCmd($tableSelection));
$renames = trim($this
->createRenameCommands($tableSelection));
if (!empty($renames)) {
$cmd = '{ ' . $cmd . ' ; ' . $renames . ' }';
}
$pipefail = '';
if ($this
->getOption('gzip')) {
$pipefail = $this
->getConfig()
->get('sh.pipefail', 'bash -c "set -o pipefail; {{cmd}}"');
$cmd .= ' | gzip -f';
$fileSuffix .= '.gz';
}
if ($file) {
$file .= $fileSuffix;
$cmd .= ' > ' . Escape::shellArg($file);
}
$cmd = $this
->addPipeFail($cmd, $pipefail);
$process = Drush::shell($cmd, NULL, $this
->getEnv());
$process
->disableOutput();
$process
->run($process
->showRealtime());
return $process
->isSuccessful() ? $file : FALSE;
}
protected function createRenameCommands(array $tableSelection) {
$skipTables = array_merge($tableSelection['skip'], $tableSelection['structure']);
$skipTables = array_flip($skipTables);
$skipTables += $this->tablesToSkip;
$command = '';
foreach (array_keys($this->tablesToAnonymize) as $table) {
if (array_key_exists($table, $skipTables)) {
continue;
}
$clone = GdprSqlDump::GDPR_TABLE_PREFIX . $table;
$rename = "RENAME TABLE \\`{$clone}\\` TO \\`{$table}\\`;";
if (Drush::verbose() || $this
->getConfig()
->simulate()) {
Drush::service('logger')
->info("Adding rename command: '{$rename}'");
}
$command .= " ( echo \"{$rename}\" ); ";
}
return $command;
}
public function dumpCmd($tableSelection) {
$multipleCommands = FALSE;
$skipTables = $tableSelection['skip'];
$structureTables = $tableSelection['structure'];
$structureTables = array_merge($this->tablesToSkip, $structureTables);
$tables = $tableSelection['tables'];
$ignores = [];
$skipTables = array_merge($structureTables, $skipTables);
$skipTables = array_merge(array_keys($this->tablesToAnonymize), $skipTables);
$dataOnly = $this
->getOption('data-only');
$orderedDump = $this
->getOption('ordered-dump');
$exec = 'mysqldump ';
$onlyDbName = str_replace('--database=', ' ', $this
->creds());
$exec .= $onlyDbName;
$extra = ' --no-autocommit --single-transaction --opt -Q';
if ($dataOnly === TRUE) {
$extra .= ' --no-create-info';
}
if ($orderedDump === TRUE) {
$extra .= ' --skip-extended-insert --order-by-primary';
}
if ($option = $this
->getOption('extra-dump')) {
$extra .= " {$option}";
}
$exec .= $extra;
if (!empty($tables)) {
$exec .= ' ' . implode(' ', $tables);
}
else {
$dbSpec = $this
->getDbSpec();
foreach ($skipTables as $table) {
$ignores[] = '--ignore-table=' . $dbSpec['database'] . '.' . $table;
$multipleCommands = TRUE;
}
$exec .= ' ' . implode(' ', $ignores);
if (!empty($structureTables)) {
$exec .= ' && mysqldump ' . $onlyDbName . " --no-data {$extra} " . implode(' ', $structureTables);
$multipleCommands = TRUE;
}
}
return $multipleCommands ? "({$exec})" : $exec;
}
}