Skip to content
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

[Ref] [Import] Cleanup cleanup on old tables for form re-submission #23276

Merged
merged 1 commit into from
May 4, 2022
Merged
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
6 changes: 1 addition & 5 deletions CRM/Contact/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public function postProcess() {
$this->createUserJob();
}
else {
$this->flushDataSource();
$this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
}
// Setup the params array
Expand Down Expand Up @@ -209,11 +210,6 @@ public function postProcess() {

CRM_Core_Session::singleton()->set('dateTypes', $storeParams['dateFormats']);

//hack to prevent multiple tables.
$this->_params['import_table_name'] = $this->get('importTableName');
if (!$this->_params['import_table_name']) {
$this->_params['import_table_name'] = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE));
}
$this->instantiateDataSource();

// We should have the data in the DB now, parse it
Expand Down
2 changes: 1 addition & 1 deletion CRM/Import/DataSource/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function postProcess(&$params, &$db, &$form) {
$result = self::_CsvToTable(
$file,
$firstRowIsColumnHeader,
CRM_Utils_Array::value('import_table_name', $params),
NULL,
CRM_Utils_Array::value('fieldSeparator', $params, ',')
);

Expand Down
2 changes: 1 addition & 1 deletion CRM/Import/DataSource/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function formRule($fields, $files, $form) {
*/
public function postProcess(&$params, &$db, &$form) {
$importJob = new CRM_Contact_Import_ImportJob(
CRM_Utils_Array::value('import_table_name', $params),
NULL,
$params['sqlQuery'], TRUE
);

Expand Down
29 changes: 29 additions & 0 deletions CRM/Import/Forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,35 @@ protected function buildDataSourceFields(): void {
}
}

/**
* Flush datasource on re-submission of the form.
*
* If the form has been re-submitted the datasource might have changed.
* We tell the dataSource class to remove any tables (and potentially files)
* created last form submission.
*
* If the DataSource in use is unchanged (ie still CSV or still SQL)
* we also pass in the new variables. In theory it could decide that they
* have not actually changed and it doesn't need to do any cleanup.
*
* In practice the datasource classes blast away as they always have for now
* - however, the sql class, for example, might realise the fields it cares
* about are unchanged and not flush the table.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
protected function flushDataSource(): void {
// If the form has been resubmitted the datasource might have changed.
// We give the datasource a chance to clean up any tables it might have
// created. If we are still using the same type of datasource (e.g still
// an sql query
$oldDataSource = $this->getUserJobSubmittedValues()['dataSource'];
$oldDataSourceObject = new $oldDataSource($this->getUserJobID());
$newParams = $this->getSubmittedValue('dataSource') === $oldDataSource ? $this->getSubmittedValues() : [];
$oldDataSourceObject->purge($newParams);
}

/**
* Get the relevant datasource object.
*
Expand Down
1 change: 0 additions & 1 deletion tests/phpunit/CRM/Contact/Import/Form/DataSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public function testBuildForm(): void {
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function testDataSources(): void {
$this->createLoggedInUser();
Expand Down