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

Add foreign key to unit fields to cleanup if unit is deleted #16836

Open
wants to merge 4 commits into
base: 11.x
Choose a base branch
from

Conversation

matthiashamacher
Copy link
Contributor

Changes in this pull request

Resolves #3376

Copy link

github-actions bot commented Mar 23, 2024

Review Checklist

  • Target branch (11.2 for bug fixes, others 11.x)
  • Tests (if it's testable code, there should be a test for it - get help)
  • Docs (every functionality needs to be documented, see here)
  • Migration incl. install.sql (e.g. if the database schema changes, ...)
  • Upgrade notes (deprecations, important information, migration hints, ...)
  • Label
  • Milestone

Signed-off-by: Matthias Hamacher <[email protected]>
Signed-off-by: Matthias Hamacher <[email protected]>
Copy link

sonarcloud bot commented Apr 7, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@mcop1 mcop1 self-requested a review April 12, 2024 07:48
@mcop1 mcop1 self-assigned this Apr 12, 2024
@mcop1 mcop1 removed their request for review April 12, 2024 07:48
@mcop1 mcop1 added this to the 11.3.0 milestone Apr 12, 2024
Copy link
Contributor

@mcop1 mcop1 left a comment

Choose a reason for hiding this comment

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

Hello @matthiashamacher ,

thanks for the pr, I like the idea. However, I think we could do some improvements here. What do you think about my comments?

Comment on lines +174 to +192

if (($value instanceof DataObject\ClassDefinition\Data\QuantityValue
|| $value instanceof DataObject\ClassDefinition\Data\QuantityValueRange)
&& $fkey === 'unit'
) {
try {
$this->db->executeQuery(
sprintf(
'ALTER TABLE `%s` ADD CONSTRAINT `%s` FOREIGN KEY (`%s`)
REFERENCES `quantityvalue_units` (`id`) ON DELETE SET NULL',
$objectDatastoreTable,
self::getForeignKeyName($objectDatastoreTable, $key . '__' . $fkey),
$key . '__' . $fkey
)
);
} catch (DriverException $e) {
// Ignore if the foreign key already exists
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Any chance we could somehow extract that into a method? It seems to be same code over and over again.

WE introduced a indexExists method here, instead of just executing the statement and ignoring the error. Maybe we could do the same for foreign keys?

Comment on lines +123 to +131
if (str_contains($value, 'unit') === true) {
$this->db->executeQuery(
sprintf(
'ALTER TABLE `%s` DROP FOREIGN KEY %s',
$table,
self::getForeignKeyName($table, $value)
)
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If we introduce a foreignKeyExists method, we could also use it here, right?

And I am still not sure about the str_contains check. Could there be some side effects, if there are columns that just happen to have unit in their name?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QuantityValue: clean up data on deletion of units
3 participants