Skip to content

d9 compatiblity #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: 8.x-1.x
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
language: php

php:
- 7.1
- 7.3
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we need this file at all?


matrix:
fast_finish: true

dist: trusty
services:
- mysql

# Skip tests for tags.
if: tag IS blank
Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This can probably go.

Suggested change
- 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"
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
2 changes: 1 addition & 1 deletion database_sanitize.info.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
database_sanitize.commands:
class: \Drupal\database_sanitize\Commands\DatabaseSanitizeCommands
arguments: ['@database_sanitize']
tags:
- { name: drush.command }
4 changes: 2 additions & 2 deletions drush/tests/DatabaseSanitizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?php', '', file_get_contents(__DIR__ . '/assets/psr4-autoloader.php'));
$autoloader_real_content .= $autoloader_psr4_content;
Expand All @@ -68,7 +68,7 @@ public function setAutoloader() {
*/
public function setUp() {
// Install the standard install profile.
$sites = $this->setUpDrupal(1, TRUE, UNISH_DRUPAL_MAJOR_VERSION);
$sites = $this->setUpDrupal(1, TRUE);
$this->webRoot = $this->webroot();
$this->siteOptions = [
'root' => $this->webRoot,
Expand Down
120 changes: 120 additions & 0 deletions src/Commands/DatabaseSanitizeCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Drupal\database_sanitize\Commands;

use Drupal\database_sanitize\DatabaseSanitize;
use Drush\Commands\DrushCommands;

/**
* Drush commands for Database Sanitize.
*/
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.
*
* 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;
}

}
15 changes: 9 additions & 6 deletions src/DatabaseSanitize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions travis.php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
memory_limit = -1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we won't use Travis then we can remove this as well.