Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
"type": "drupal-module",
"minimum-stability": "dev",
"require": {
"edisonlabs/merge-yaml": "~1"
"edisonlabs/merge-yaml": "2.0.0-alpha1"
},
"require-dev": {
"drush/drush": "~8.1.17"
"drush/drush": "~10.2.2"
},
"extra": {
"drush": {
"services": {
"drush.services.yml": "^10"
}
}
}
}
6 changes: 6 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
database_sanitize.commands:
class: \Drupal\database_sanitize\Commands\DatabaseSanitizeCommands
arguments: ['@database_sanitize']
tags:
- { name: drush.command }
121 changes: 121 additions & 0 deletions src/Commands/DatabaseSanitizeCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Drupal\database_sanitize\Commands;

use Drupal\database_sanitize\DatabaseSanitize;
use Drush\Commands\DrushCommands;

/**
* Drush commands for Database Sanitize.
*/
class DatabaseSanitizeCommands extends DrushCommands {

/**
* The sanitizer service instance.
*
* @var \Drupal\database_sanitize\DatabaseSanitize
*/
protected $sanitizer;

/**
* DatabaseSanitizeCommands constructor.
*
* @param \Drupal\database_sanitize\DatabaseSanitize $sanitizer
*/
public function __construct(DatabaseSanitize $sanitizer) {
$this->sanitizer = $sanitizer;
}

/**
* Analyze existing yml files.
*
* Compares existing database.sanitize.yml files on the site installation
* against existing database tables.
*
* @param array $options
* An associative array of options whose values come from cli, aliases,
* config, etc.
*
* @option file
* The full path to a sanitize YML file.
* @option list
* List the table names.
*
* @command db:sanitize-analyze
* @aliases dbsa,db-sanitize-analyze
*
* @throws \Exception
*/
public function analyze(array $options = ['file' => NULL, 'list' => NULL]) {
$file = $options['file'];
if (!empty($file) && !file_exists($file)) {
throw new \Exception(dt('File @file does not exist', ['@file' => $file]));
}

$missing_tables = $this->sanitizer->getUnspecifiedTables($file);

if (!$missing_tables) {
$this->logger()->info(dt('All database tables are already specified in sanitize YML files'));
return;
}

$this->logger()->warning(dt('There are @count tables not defined on sanitize YML files', ['@count' => count($missing_tables)]));

if (!empty($options['list'])) {
$this->logger()->warning(implode("\n", $missing_tables));
}
}

/**
* Generates Sanitization entries for tables not specified on sanitize YML files..
*
* @param array $options
* An associative array of options whose values come from cli, aliases,
* config, etc.
*
* @option file
* The full path to a sanitize YML file.
* @option machine-name
* The machine name to export the tables under.
*
* @command db:sanitize-generate
* @aliases dbsg,db-sanitize-generate
*
* @return array
* Array of sanitization entries - TODO: more info.
*
* @throws \Exception
*/
public function generate(array $options = ['file' => NULL, 'machine-name' => NULL]) {
$machine_name = $options['machine-name'];
if (empty($machine_name)) {
$machine_name = $this->io()->ask('Please provide the machine name to export the tables under');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually never triggers because the parameter is mandatory on the command.

}

if (empty($options['file'])) {
$options['file'] = $this->io()->ask('Please provide the full path to a sanitize YML file');
}

$yml_file_path = $options['file'];
$missing_tables = $this->sanitizer->getUnspecifiedTables($yml_file_path);
if (!$missing_tables) {
$this->logger()->info(dt('All database tables are already specified in sanitize YML files'));
return [];
}

$content = [
'sanitize' => [
$machine_name => [],
],
];
foreach ($missing_tables as $table) {
$content['sanitize'][$machine_name][$table] = [
'description' => "Sanitization entry for {$table}. Generated by drush db-sanitize-generate.",
'query' => "TRUNCATE TABLE {$table}",
];
}

return $content;
}

}