diff --git a/.travis.yml b/.travis.yml index dde9e12..595d5eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,12 +4,13 @@ language: php php: - - 7.1 + - 7.3 matrix: fast_finish: true -dist: trusty +services: + - mysql # Skip tests for tags. if: tag IS blank @@ -30,11 +31,14 @@ before_install: install: - composer global require drupal/coder --prefer-dist -vvv || exit 1 - phpcs --config-set installed_paths "$COMPOSER_VENDOR_PATH/drupal/coder/coder_sniffer" + - phpenv config-add travis.php.ini + - php -ini | grep memory_limit before_script: - composer install -vvv - - cd "$TRAVIS_BUILD_DIR/vendor/drush/drush"; composer install - + - cd "$TRAVIS_BUILD_DIR/vendor/"; rm -rf drush; git clone --branch 10.2.2 https://github.com/drush-ops/drush.git drush + - cd "$TRAVIS_BUILD_DIR/vendor/drush"; composer install + script: - phpcs --standard=Drupal --ignore=vendor/* --warning-severity=0 . - - php "$TRAVIS_BUILD_DIR/vendor/drush/drush/vendor/bin/phpunit" --configuration "$TRAVIS_BUILD_DIR/vendor/drush/drush/tests" "$TRAVIS_BUILD_DIR/drush/tests" + - php "$TRAVIS_BUILD_DIR/vendor/drush/vendor/bin/phpunit" --configuration "$TRAVIS_BUILD_DIR/vendor/drush/tests" "$TRAVIS_BUILD_DIR/drush/tests" diff --git a/composer.json b/composer.json index 9cb4818..104ec89 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" }, "require-dev": { - "drush/drush": "~8.1.17" + "drush/drush": "~10.2.2" + }, + "extra": { + "drush": { + "services": { + "drush.services.yml": "^9" + } + } } } diff --git a/database_sanitize.info.yml b/database_sanitize.info.yml index 201b63d..295aa63 100644 --- a/database_sanitize.info.yml +++ b/database_sanitize.info.yml @@ -1,4 +1,4 @@ name: 'Database Sanitize' type: module description: 'Provides drush commands for yaml sanitization files checks and generation.' -core: 8.x +core_version_requirement: ^8 || ^9 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/drush/tests/DatabaseSanitizeTest.php b/drush/tests/DatabaseSanitizeTest.php index ff8244f..8b3ff0a 100644 --- a/drush/tests/DatabaseSanitizeTest.php +++ b/drush/tests/DatabaseSanitizeTest.php @@ -56,7 +56,7 @@ class DatabaseSanitizeCase extends CommandUnishTestCase { * This function needs to be called after setUpDrupal(). */ public function setAutoloader() { - $autoloader_real_path = $this->webRoot . '/vendor/composer/autoload_real.php'; + $autoloader_real_path = $this->webRoot . '/../vendor/composer/autoload_real.php'; $autoloader_real_content = file_get_contents($autoloader_real_path); $autoloader_psr4_content = str_replace('setUpDrupal(1, TRUE, UNISH_DRUPAL_MAJOR_VERSION); + $sites = $this->setUpDrupal(1, TRUE); $this->webRoot = $this->webroot(); $this->siteOptions = [ 'root' => $this->webRoot, diff --git a/src/Commands/DatabaseSanitizeCommands.php b/src/Commands/DatabaseSanitizeCommands.php new file mode 100644 index 0000000..0c7f037 --- /dev/null +++ b/src/Commands/DatabaseSanitizeCommands.php @@ -0,0 +1,120 @@ +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) { + \Drupal::logger('database_sanitize')->log('success', (dt('All database tables are already specified in sanitize YML files'))); + return; + } + + \Drupal::logger('database_sanitize')->log('warning', (dt('There are @count tables not defined on sanitize YML files', ['@count' => count($missing_tables)]))); + + if (!empty($options['list'])) { + \Drupal::logger('database_sanitize')->log('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 Consolidation\OutputFormatters\Formatters\YamlFormatter + * + * @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) { + \Drupal::logger('database_sanitize')->log('success', (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; + } + +} diff --git a/src/DatabaseSanitize.php b/src/DatabaseSanitize.php index d8449eb..f6871b0 100644 --- a/src/DatabaseSanitize.php +++ b/src/DatabaseSanitize.php @@ -177,6 +177,7 @@ public function getDatabaseSanitizeYmlFileContent() { */ public function getUnspecifiedTables($yml_file_path = NULL) { if ($yml_file_path) { + if (!file_exists($yml_file_path)) { throw new \Exception("File does not exist $yml_file_path"); } @@ -199,13 +200,13 @@ public function getUnspecifiedTables($yml_file_path = NULL) { } catch (ParseException $exception) { $message = $exception->getMessage(); - $this->logger->error("Unable to parse the sanitize YAML file. @message", ['@message' => $message]); + \Drupal::logger('database_sanitize')->log('error', (dt("Unable to parse the sanitize YAML file. @message", ['@message' => $message]))); return $db_tables; } if (is_null($parsed_file) || !array_key_exists('sanitize', $parsed_file)) { - $this->logger->error("The 'sanitize' key is not defined"); + \Drupal::logger('database_sanitize')->log('error', (dt("The 'sanitize' key is not defined"))); return $db_tables; } @@ -218,12 +219,12 @@ public function getUnspecifiedTables($yml_file_path = NULL) { foreach ($parsed_file['sanitize'] as $machine_name => $tables) { foreach ($tables as $table_name => $definition) { if (is_array($definition) && !array_key_exists('description', $definition)) { - $this->logger->warning('Table \'@table_name\' defined by \'@machine_name\' does not specify a \'description\' key', ['@table_name' => $table_name, '@machine_name' => $machine_name]); + \Drupal::logger('database_sanitize')->log('warning', (dt('Table \'@table_name\' defined by \'@machine_name\' does not specify a \'description\' key', ['@table_name' => $table_name, '@machine_name' => $machine_name]))); continue; } if (is_array($definition) && !array_key_exists('query', $definition)) { - $this->logger->warning('Table \'@table_name\' defined by \'@machine_name\' does not specify a \'query\' key', ['@table_name' => $table_name, '@machine_name' => $machine_name]); + \Drupal::logger('database_sanitize')->log('warning', (dt('Table \'@table_name\' defined by \'@machine_name\' does not specify a \'query\' key', ['@table_name' => $table_name, '@machine_name' => $machine_name]))); continue; } @@ -248,10 +249,12 @@ public function getUnspecifiedTables($yml_file_path = NULL) { $missing = array_diff($db_tables, $yml_tables); if (is_array($missing) && empty($missing)) { - $this->logger->info('All database tables are already specified in sanitize YML files'); + \Drupal::logger('database_sanitize')->log('success', (dt('All database tables are already specified in sanitize YML files'))); return []; - } + } else { + \Drupal::logger('database_sanitize')->log('warning', (dt('There are @count tables not defined on sanitize YML files', ['@count' => count($missing)]))); + } sort($missing); diff --git a/travis.php.ini b/travis.php.ini new file mode 100644 index 0000000..7999e96 --- /dev/null +++ b/travis.php.ini @@ -0,0 +1 @@ +memory_limit = -1