-
Notifications
You must be signed in to change notification settings - Fork 170
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
DKAN-4291 add mysql empty row removal option #4292
Draft
swirtSJW
wants to merge
2
commits into
2.x
Choose a base branch
from
DKAN-4291-add-mysql-empty-rows-option
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
modules/datastore/modules/datastore_mysql_import/datastore_mysql_import.links.menu.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
datastore_mysql_import.settings_form: | ||
title: MySQl Import settings | ||
description: Setting for the MySQL Importer. | ||
parent: system.admin_dkan | ||
route_name: datastore.mysql_import.settings | ||
weight: 15 |
7 changes: 7 additions & 0 deletions
7
modules/datastore/modules/datastore_mysql_import/datastore_mysql_import.routing.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
datastore.mysql_import.settings: | ||
path: '/admin/dkan/datastore/mysql_import' | ||
defaults: | ||
_title: 'MySQL Import Settings' | ||
_form: 'Drupal\datastore_mysql_import\Form\DatastoreMysqlImportSettingsForm' | ||
requirements: | ||
_permission: 'administer site configuration' |
76 changes: 76 additions & 0 deletions
76
...es/datastore/modules/datastore_mysql_import/src/Form/DatastoreMysqlImportSettingsForm.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Drupal\datastore_mysql_import\Form; | ||
|
||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Form\ConfigFormBase; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Datastore MySQL Import settings form. | ||
* | ||
* @package Drupal\datastore\Form | ||
* @codeCoverageIgnore | ||
*/ | ||
class DatastoreMysqlImportSettingsForm extends ConfigFormBase { | ||
|
||
|
||
/** | ||
* Constructs form. | ||
* | ||
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | ||
* The factory for configuration objects. | ||
*/ | ||
public function __construct(ConfigFactoryInterface $config_factory) { | ||
parent::__construct($config_factory); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static( | ||
$container->get('config.factory'), | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFormId() { | ||
return 'datastore_mysql_import_settings_form'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getEditableConfigNames() { | ||
return ['datastore.mysql_import.settings']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(array $form, FormStateInterface $form_state) { | ||
$config = $this->config('datastore.mysql_import.settings'); | ||
$form['remove_empty_rows'] = [ | ||
'#type' => 'checkbox', | ||
'#title' => $this->t('Enable removal of empty rows in dataset.'), | ||
'#description' => $this->t('Unlike the chunk harvester, which ignores empty rows in a CSV, the MySQL importer will import empty rows.'), | ||
'#default_value' => $config->get('remove_empty_rows'), | ||
]; | ||
return parent::buildForm($form, $form_state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitForm(array &$form, FormStateInterface $form_state) { | ||
$this->config('datastore.mysql_import.settings') | ||
->set('remove_empty_rows', $form_state->getValue('remove_empty_rows')) | ||
->save(); | ||
parent::submitForm($form, $form_state); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,7 @@ protected function runIt() { | |
Database::setActiveConnection(); | ||
|
||
$this->setStatus(Result::DONE); | ||
$this->runOptionalPostImportCleanup($this->dataStorage->getTableName(), $spec); | ||
return NULL; | ||
} | ||
|
||
|
@@ -128,7 +129,7 @@ protected function getColsFromFile(string $file_path, string $delimiter): array | |
$f = fopen($file_path, 'r'); | ||
|
||
// Ensure the file could be successfully opened. | ||
if ($f === FALSE) { | ||
if (!isset($f) || $f === FALSE) { | ||
throw new FileException(sprintf('Failed to open resource file "%s".', $file_path)); | ||
} | ||
|
||
|
@@ -141,9 +142,8 @@ protected function getColsFromFile(string $file_path, string $delimiter): array | |
|
||
// Close the resource file, since it is no longer needed. | ||
fclose($f); | ||
// Ensure the columns of the resource file were successfully read. $columns | ||
// could be array, FALSE, or NULL. | ||
if (!$columns) { | ||
// Ensure the columns of the resource file were successfully read. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2.18 cruft |
||
if (!isset($columns) || $columns === FALSE) { | ||
throw new FileException(sprintf('Failed to read columns from resource file "%s".', $file_path)); | ||
} | ||
|
||
|
@@ -257,4 +257,42 @@ protected function getSqlStatement(string $file_path, string $table_name, array | |
]); | ||
} | ||
|
||
/** | ||
* Performs optional cleanup steps on imported data. | ||
* | ||
* @param string $table_name | ||
* The name of the table to clean. | ||
* @param array $spec | ||
* The sanitized headers for the imported data. | ||
*/ | ||
protected function runOptionalPostImportCleanup(string $table_name, array $spec): void { | ||
// @todo this should use dependency injection, but this is not a service. | ||
$config = \Drupal::config('datastore.mysql_import.settings'); | ||
if ($config->get('remove_empty_rows')) { | ||
$this->removeEmptyRows($table_name, $spec); | ||
} | ||
} | ||
|
||
/** | ||
* Remove empty rows from the imported data. | ||
* | ||
* @param string $table_name | ||
* The name of the table to remove empty rows from. | ||
* @param array $fields | ||
* The sanitized headers for the imported data. | ||
*/ | ||
protected function removeEmptyRows(string $table_name, array $fields): void { | ||
$connection = \Drupal::database(); | ||
$query = $connection->delete($table_name); | ||
foreach ($fields as $field) { | ||
// Build query to look for records with all empty values for all fields. | ||
$query->condition($field, '', '='); | ||
} | ||
$num_deleted = $query->execute(); | ||
if ($num_deleted > 0) { | ||
// @todo We should log how many were deleted... no logger :( | ||
|
||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is 2.18 cruft that snuck back in due to me working in 2.18. I will fix this and the others of similar sneakiness.