diff --git a/composer.json b/composer.json index 9cb4818..4593f4c 100644 --- a/composer.json +++ b/composer.json @@ -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" + } + } } } diff --git a/drush.services.yml b/drush.services.yml new file mode 100644 index 0000000..acaa9fb --- /dev/null +++ b/drush.services.yml @@ -0,0 +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 new file mode 100644 index 0000000..09af7bf --- /dev/null +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -0,0 +1,121 @@ +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'); + } + + 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; + } + +}