Skip to content

Commit

Permalink
Cleanup cleanup on old tables for form re-submission
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed May 4, 2022
1 parent 5d11856 commit 1163561
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
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

0 comments on commit 1163561

Please sign in to comment.