diff --git a/composer.json b/composer.json index 9cb4818..aed0514 100644 --- a/composer.json +++ b/composer.json @@ -9,5 +9,12 @@ }, "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..998a75c --- /dev/null +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -0,0 +1,115 @@ + 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. + * + * @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; + } + +}