Skip to content
Merged
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
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@
},
"require-dev": {
"drush/drush": "~8.1.17"
},
"extra": {
"drush": {
"services": {
"drush.services.yml": "^9"
}
}
}
}
5 changes: 5 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
database_sanitize.commands:
class: \Drupal\database_sanitize\Commands\DatabaseSanitizeCommands
tags:
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets inject here the database_sanitize service:
arguments: ['@database_sanitize']

Then add a constructor like on http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php

Copy link
Author

Choose a reason for hiding this comment

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

good idea - I probably won't get time to work on this over the next few days, so if you have time please could you take this forward?

- { name: drush.command }
115 changes: 115 additions & 0 deletions src/Commands/DatabaseSanitizeCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Drupal\database_sanitize\Commands;

use Drush\Commands\DrushCommands;

/**
* A Drush commandfile.
*
* In addition to this file, you need a drush.services.yml
* in root of your module, and a composer.json file that provides the name
* of the services file to use.
*
* See these files for an example of injecting Drupal services:
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml
Copy link
Contributor

Choose a reason for hiding this comment

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

This docblock needs updating to describe the class itself.

*/
class DatabaseSanitizeCommands extends DrushCommands {

/**
* 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 sanitizeAnalyze(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 = \Drupal::service('database_sanitize')->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 a database.sanitize.yml file.
*
* Generate database.sanitize.yml file for tables not specified on sanitize
* YML files.
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't correct, we do not generate a database.sanitize.yml file here, we just generate the content for it.

*
* @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 sanitizeGenerate(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');
}

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 = \Drupal::service('database_sanitize')->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;
}

}