From aa2612bd45bc2c7f71088551bf9785dc0d368ee5 Mon Sep 17 00:00:00 2001 From: Malcolm Young Date: Tue, 4 Dec 2018 17:47:39 +0000 Subject: [PATCH 01/13] Issue #3017760: initial auto-generation of drush 9 commands --- composer.json | 8 +++- drush.services.yml | 5 +++ src/Commands/DatabaseSanitizeCommands.php | 54 +++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 drush.services.yml create mode 100644 src/Commands/DatabaseSanitizeCommands.php diff --git a/composer.json b/composer.json index 9cb4818..16ff89a 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,11 @@ "require": { "edisonlabs/merge-yaml": "~1" }, - "require-dev": { - "drush/drush": "~8.1.17" + "extra": { + "drush": { + "services": { + "drush.services.yml": "^9" + } + } } } diff --git a/drush.services.yml b/drush.services.yml new file mode 100644 index 0000000..64cad3e --- /dev/null +++ b/drush.services.yml @@ -0,0 +1,5 @@ +services: + database_sanitize.commands: + class: \Drupal\database_sanitize\Commands\DatabaseSanitizeCommands + tags: + - { name: drush.command } diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php new file mode 100644 index 0000000..e01174d --- /dev/null +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -0,0 +1,54 @@ + null, 'list' => null]) { + // See bottom of https://weitzman.github.io/blog/port-to-drush9 for details on what to change when porting a + // legacy command. + } + + /** + * Generates a database.sanitize.yml file 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 + */ + public function sanitizeGenerate(array $options = ['file' => null, 'machine-name' => null]) { + // See bottom of https://weitzman.github.io/blog/port-to-drush9 for details on what to change when porting a + // legacy command. + } + +} From 4dbc823e92c06ce91616ed52ff0db9eb9c46409b Mon Sep 17 00:00:00 2001 From: Malcolm Young Date: Tue, 4 Dec 2018 17:53:28 +0000 Subject: [PATCH 02/13] fix coding standards issues mentioned by codesniffer --- src/Commands/DatabaseSanitizeCommands.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php index e01174d..d6c9c33 100644 --- a/src/Commands/DatabaseSanitizeCommands.php +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -20,7 +20,9 @@ class DatabaseSanitizeCommands extends DrushCommands { /** * 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. + * @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 @@ -29,7 +31,7 @@ class DatabaseSanitizeCommands extends DrushCommands { * @command db:sanitize-analyze * @aliases dbsa,db-sanitize-analyze */ - public function sanitizeAnalyze(array $options = ['file' => null, 'list' => null]) { + public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL]) { // See bottom of https://weitzman.github.io/blog/port-to-drush9 for details on what to change when porting a // legacy command. } @@ -37,7 +39,9 @@ public function sanitizeAnalyze(array $options = ['file' => null, 'list' => null /** * Generates a database.sanitize.yml file for tables not specified on sanitize YML files. * - * @param array $options An associative array of options whose values come from cli, aliases, config, etc. + * @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 @@ -46,7 +50,7 @@ public function sanitizeAnalyze(array $options = ['file' => null, 'list' => null * @command db:sanitize-generate * @aliases dbsg,db-sanitize-generate */ - public function sanitizeGenerate(array $options = ['file' => null, 'machine-name' => null]) { + public function sanitizeGenerate(array $options = ['file' => NULL, 'machine-name' => NULL]) { // See bottom of https://weitzman.github.io/blog/port-to-drush9 for details on what to change when porting a // legacy command. } From e55727b66ca1c7004e213546a07f09f1ae994817 Mon Sep 17 00:00:00 2001 From: Malcolm Young Date: Tue, 4 Dec 2018 18:02:35 +0000 Subject: [PATCH 03/13] basic conversion of dbsa command --- src/Commands/DatabaseSanitizeCommands.php | 29 +++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php index d6c9c33..30376ea 100644 --- a/src/Commands/DatabaseSanitizeCommands.php +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -18,10 +18,12 @@ class DatabaseSanitizeCommands extends DrushCommands { /** - * Compares existing database.sanitize.yml files on the site installation against existing database tables. + * 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. + * An associative array of options whose values come from cli, aliases, + * config, etc. * * @option file * The full path to a sanitize YML file. @@ -30,10 +32,33 @@ class DatabaseSanitizeCommands extends DrushCommands { * * @command db:sanitize-analyze * @aliases dbsa,db-sanitize-analyze + * @return void + * @throws \Exception */ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL]) { // See bottom of https://weitzman.github.io/blog/port-to-drush9 for details on what to change when porting a // legacy command. + if (empty($options['file'])) { + $options['file'] = $this->io()->ask('Please provide the full path to a sanitize YML file'); + } + + $file = $options['file']; + if (!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'), 'ok'); + 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)); + } } /** From c084954111c201441068d63d1313011a3cf016ba Mon Sep 17 00:00:00 2001 From: Malcolm Young Date: Tue, 4 Dec 2018 18:04:01 +0000 Subject: [PATCH 04/13] basic conversion of dbsg command --- src/Commands/DatabaseSanitizeCommands.php | 28 ++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php index 30376ea..4527fc3 100644 --- a/src/Commands/DatabaseSanitizeCommands.php +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -76,8 +76,30 @@ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL * @aliases dbsg,db-sanitize-generate */ public function sanitizeGenerate(array $options = ['file' => NULL, 'machine-name' => NULL]) { - // See bottom of https://weitzman.github.io/blog/port-to-drush9 for details on what to change when porting a - // legacy command. - } + $machine_name = $options['machine-name']; + if (empty($machine_name)) { + throw new \Exception(dt('You must specify a machine-name')); + } + + $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'), 'ok'); + 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; } } From a6d599589e8ac2fd1194c6f8da25f21a70788f4a Mon Sep 17 00:00:00 2001 From: Malcolm Young Date: Tue, 4 Dec 2018 18:08:38 +0000 Subject: [PATCH 05/13] coding standards fixes --- src/Commands/DatabaseSanitizeCommands.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php index 4527fc3..75aae77 100644 --- a/src/Commands/DatabaseSanitizeCommands.php +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -18,6 +18,8 @@ class DatabaseSanitizeCommands extends DrushCommands { /** + * Analyze existing yml files. + * * Compares existing database.sanitize.yml files on the site installation * against existing database tables. * @@ -32,12 +34,10 @@ class DatabaseSanitizeCommands extends DrushCommands { * * @command db:sanitize-analyze * @aliases dbsa,db-sanitize-analyze - * @return void + * * @throws \Exception */ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL]) { - // See bottom of https://weitzman.github.io/blog/port-to-drush9 for details on what to change when porting a - // legacy command. if (empty($options['file'])) { $options['file'] = $this->io()->ask('Please provide the full path to a sanitize YML file'); } @@ -62,10 +62,14 @@ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL } /** - * Generates a database.sanitize.yml file for tables not specified on sanitize YML files. + * Generates a database.sanitize.yml file. + * + * Generate database.sanitize.yml file for tables not specified on sanitize + * YML files. * * @param array $options - * An associative array of options whose values come from cli, aliases, config, etc. + * An associative array of options whose values come from cli, aliases, + * config, etc. * * @option file * The full path to a sanitize YML file. @@ -74,6 +78,9 @@ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL * * @command db:sanitize-generate * @aliases dbsg,db-sanitize-generate + * + * @return array|void + * @throws \Exception */ public function sanitizeGenerate(array $options = ['file' => NULL, 'machine-name' => NULL]) { $machine_name = $options['machine-name']; @@ -100,6 +107,7 @@ public function sanitizeGenerate(array $options = ['file' => NULL, 'machine-name ]; } - return $content; } + return $content; + } } From e0085140de02874795236dbf9c73db198414c42e Mon Sep 17 00:00:00 2001 From: Malcolm Young Date: Tue, 4 Dec 2018 18:19:53 +0000 Subject: [PATCH 06/13] coding standards fixes --- src/Commands/DatabaseSanitizeCommands.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php index 75aae77..627c909 100644 --- a/src/Commands/DatabaseSanitizeCommands.php +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -79,7 +79,9 @@ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL * @command db:sanitize-generate * @aliases dbsg,db-sanitize-generate * - * @return array|void + * @return array + * Array of sanitization entries - TODO: more info. + * * @throws \Exception */ public function sanitizeGenerate(array $options = ['file' => NULL, 'machine-name' => NULL]) { @@ -92,7 +94,7 @@ public function sanitizeGenerate(array $options = ['file' => NULL, 'machine-name $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'), 'ok'); - return; + return []; } $content = [ From faf216e81d5c35253c475aec85062e9c9133673b Mon Sep 17 00:00:00 2001 From: Malcolm Young Date: Tue, 4 Dec 2018 18:25:10 +0000 Subject: [PATCH 07/13] get options from I/O --- src/Commands/DatabaseSanitizeCommands.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php index 627c909..97db556 100644 --- a/src/Commands/DatabaseSanitizeCommands.php +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -50,7 +50,7 @@ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL $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'), 'ok'); + $this->logger()->info(dt('All database tables are already specified in sanitize YML files')); return; } @@ -87,13 +87,17 @@ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL public function sanitizeGenerate(array $options = ['file' => NULL, 'machine-name' => NULL]) { $machine_name = $options['machine-name']; if (empty($machine_name)) { - throw new \Exception(dt('You must specify a 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'), 'ok'); + $this->logger()->info(dt('All database tables are already specified in sanitize YML files')); return []; } From 1b3157c0366fe35dff41bb5a37b34a27c7862077 Mon Sep 17 00:00:00 2001 From: Malcolm Young Date: Tue, 4 Dec 2018 18:26:17 +0000 Subject: [PATCH 08/13] add drush back in dev dependencies --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 16ff89a..aed0514 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,9 @@ "require": { "edisonlabs/merge-yaml": "~1" }, + "require-dev": { + "drush/drush": "~8.1.17" + }, "extra": { "drush": { "services": { From 495b9e0c9040428a7d249d87dfe8934aa4ef6d47 Mon Sep 17 00:00:00 2001 From: Malcolm Young Date: Tue, 4 Dec 2018 20:13:21 +0000 Subject: [PATCH 09/13] make file optional in analyze command --- src/Commands/DatabaseSanitizeCommands.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php index 97db556..998a75c 100644 --- a/src/Commands/DatabaseSanitizeCommands.php +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -38,12 +38,8 @@ class DatabaseSanitizeCommands extends DrushCommands { * @throws \Exception */ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL]) { - if (empty($options['file'])) { - $options['file'] = $this->io()->ask('Please provide the full path to a sanitize YML file'); - } - $file = $options['file']; - if (!file_exists($file)) { + if (!empty($file) && !file_exists($file)) { throw new \Exception(dt('File @file does not exist', ['@file' => $file])); } From e6ef174ab359b114bf3f222ee2340b306e6b6004 Mon Sep 17 00:00:00 2001 From: Vijaya Chandran Mani Date: Thu, 13 Dec 2018 12:33:09 +0000 Subject: [PATCH 10/13] #3017760 - Support drush 9 - Address review comments from #9 --- drush.services.yml | 1 + src/Commands/DatabaseSanitizeCommands.php | 40 +++++++++++++---------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/drush.services.yml b/drush.services.yml index 64cad3e..acaa9fb 100644 --- a/drush.services.yml +++ b/drush.services.yml @@ -1,5 +1,6 @@ services: database_sanitize.commands: class: \Drupal\database_sanitize\Commands\DatabaseSanitizeCommands + arguments: ['@database_sanitize'] tags: - { name: drush.command } diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php index 998a75c..77b0362 100644 --- a/src/Commands/DatabaseSanitizeCommands.php +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -2,21 +2,30 @@ namespace Drupal\database_sanitize\Commands; +use Drupal\database_sanitize\DatabaseSanitize; 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 + * Class DatabaseSanitizeCommands */ 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. * @@ -37,13 +46,13 @@ class DatabaseSanitizeCommands extends DrushCommands { * * @throws \Exception */ - public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL]) { + 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 = \Drupal::service('database_sanitize')->getUnspecifiedTables($file); + $missing_tables = $this->sanitizer->getUnspecifiedTables($file); if (!$missing_tables) { $this->logger()->info(dt('All database tables are already specified in sanitize YML files')); @@ -58,10 +67,7 @@ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL } /** - * Generates a database.sanitize.yml file. - * - * Generate database.sanitize.yml file for tables not specified on sanitize - * YML files. + * Generate Sanitization entries. * * @param array $options * An associative array of options whose values come from cli, aliases, @@ -80,7 +86,7 @@ public function sanitizeAnalyze(array $options = ['file' => NULL, 'list' => NULL * * @throws \Exception */ - public function sanitizeGenerate(array $options = ['file' => NULL, 'machine-name' => NULL]) { + 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'); @@ -91,7 +97,7 @@ public function sanitizeGenerate(array $options = ['file' => NULL, 'machine-name } $yml_file_path = $options['file']; - $missing_tables = \Drupal::service('database_sanitize')->getUnspecifiedTables($yml_file_path); + $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 []; From 8a3196f2c36a839947443fc832a7a42d44eba978 Mon Sep 17 00:00:00 2001 From: Manuel Garcia Date: Thu, 13 Dec 2018 18:32:40 +0000 Subject: [PATCH 11/13] Docblock update suggested by @manuee Co-Authored-By: vijaycs85 --- src/Commands/DatabaseSanitizeCommands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php index 77b0362..cb02ece 100644 --- a/src/Commands/DatabaseSanitizeCommands.php +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -6,7 +6,7 @@ use Drush\Commands\DrushCommands; /** - * Class DatabaseSanitizeCommands + * Drush commands for Database Sanitize. */ class DatabaseSanitizeCommands extends DrushCommands { From 59081d1678bb9eb523211a8927316b8c09244b87 Mon Sep 17 00:00:00 2001 From: Manuel Garcia Date: Thu, 13 Dec 2018 18:33:01 +0000 Subject: [PATCH 12/13] Docblock update. Co-Authored-By: vijaycs85 --- src/Commands/DatabaseSanitizeCommands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php index cb02ece..09af7bf 100644 --- a/src/Commands/DatabaseSanitizeCommands.php +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -67,7 +67,7 @@ public function analyze(array $options = ['file' => NULL, 'list' => NULL]) { } /** - * Generate Sanitization entries. + * 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, From 5e2aec7c1b392bc0691f9bd997badb05846f3da6 Mon Sep 17 00:00:00 2001 From: Suzy Masri Date: Thu, 4 Jun 2020 16:55:15 +0300 Subject: [PATCH 13/13] Bumped Drush to 10.2.2 + Upgraded merge-yaml to 2.0.0-alpha1 --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index aed0514..4593f4c 100644 --- a/composer.json +++ b/composer.json @@ -5,15 +5,15 @@ "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": "^9" + "drush.services.yml": "^10" } } }