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

[database] Clean up mri_scan_type #9304

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

maximemulder
Copy link
Contributor

@maximemulder maximemulder commented Jun 26, 2024

Summary of changes

  • Add missing foreign keys.
  • Rename Scan_type foreign key fields to MriScanTypeID.
  • Remove the default value of 0 for mri_protocol.MriScanTypeID (as this is a non-nullable foreign key, I don't think this should ever happen).
  • Adapt the existing queries and scripts in LORIS.

Testing instructions

  1. Apply the patch
  2. Reinstall Raisinbread
  3. Test the associated LORIS feature

As we unfortunately have a very loose typing in LORIS, I may have missed a few bugs when adapting the scripts, but we're early on the next version development cycle so I guess it's the right time to do such changes.

Alternatives

  • The foreign key fields could just be named ScanTypeID (it's obvious it's an "MRI" scan type).
  • Should the files.AcquisitionTypeID also be renamed to files.MriScanTypeID ?
  • Should mri_scan_type.Scan_type be renamed to mri_scan_type.Name ?

Related issues

@maximemulder maximemulder changed the title [cleanup] Improve mri_scan_type database [database] Clean-up mri_scan_type Jun 26, 2024
@maximemulder
Copy link
Contributor Author

I would like to have some opinions on the alternatives before merging. Also I don't know who should review it so I'll just ping @ridz1208 and feel free to give your opinion or re-assign it.

@maximemulder maximemulder marked this pull request as ready for review June 26, 2024 17:46
@maximemulder
Copy link
Contributor Author

After the MRI meeting today, we agreed on the following TODOs before reviewing/merging this PR :

@maximemulder
Copy link
Contributor Author

Okay, I tested in LORIS as well as LORIS-MRI (imaging pipeline: dcm2mnc -> defacing -> mnc2bids) and everythings seem to wok, PR is ready for review/mege @cmadjar @nicolasbrossard. The PR is basically a SQL patch and renaming the fields in SQL queries (and some auto-formatting, feel free to ignore whitespace changes).

Copy link
Collaborator

@cmadjar cmadjar left a comment

Choose a reason for hiding this comment

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

@maximemulder my initial review on the LORIS side for this.

One generic comment based on the spacing I see in your PR. In LORIS, we use the following convention: 1 tab = 4 spaces. I think you need to change your editor configuration to reflect that convention.

I also added some comments for @driusan and @nicolasbrossard to benefit from their wisdom on some of the changes :)

@@ -675,27 +676,30 @@ CREATE TABLE `mri_protocol` (
KEY `FK_mri_protocol_1` (`ScannerID`),
CONSTRAINT `FK_mri_protocol_1` FOREIGN KEY (`ScannerID`) REFERENCES `mri_scanner` (`ID`),
CONSTRAINT `FK_mri_protocol_2` FOREIGN KEY (`CenterID`) REFERENCES `psc` (`CenterID`),
CONSTRAINT `FK_mri_protocol_scan_type` FOREIGN KEY (`MriScanTypeID`) REFERENCES `mri_scan_type` (`ID`),
Copy link
Collaborator

Choose a reason for hiding this comment

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

seriously, we never had a foreign key for this? Interesting...

@@ -968,7 +972,7 @@ CREATE TABLE `mri_violations_log` (
`CandID` int(6) DEFAULT NULL,
`Visit_label` varchar(255) DEFAULT NULL,
`CheckID` int(11) DEFAULT NULL,
`Scan_type` int(11) unsigned DEFAULT NULL,
`MriScanTypeID` int(11) unsigned DEFAULT NULL,
Copy link
Collaborator

Choose a reason for hiding this comment

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

@nicolasbrossard @driusan I am curious what you think of the DEFAULT NULL for MriScanTypeID of table mri_violations_log. My understanding is that it can never be NULL based on the code since the violations log happen after scan identification. But before we remove the DEFAULT NULL from this table, I'd like your input. :)

Thank you!

Comment on lines 987 to 995
CONSTRAINT `FK_mri_violations_log_scan_type`
FOREIGN KEY (`MriScanTypeID`) REFERENCES `mri_scan_type` (`ID`),
Copy link
Collaborator

Choose a reason for hiding this comment

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

fascinating, another foreign key I was sure was already there. 🤣 Aaah, the things you take for granted...

Comment on lines 692 to 707
`MriProtocolGroupTargetID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`MriProtocolGroupID` INT(4) UNSIGNED NOT NULL,
`ProjectID` INT(10) UNSIGNED DEFAULT NULL,
`CohortID` INT(10) UNSIGNED DEFAULT NULL,
`Visit_label` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`MriProtocolGroupTargetID`),
CONSTRAINT `FK_mri_protocol_group_target_1` FOREIGN KEY (`MriProtocolGroupID`) REFERENCES `mri_protocol_group` (`MriProtocolGroupID`),
CONSTRAINT `FK_mri_protocol_group_target_2` FOREIGN KEY (`ProjectID`) REFERENCES `Project` (`ProjectID`),
CONSTRAINT `FK_mri_protocol_group_target_3` FOREIGN KEY (`CohortID`) REFERENCES `cohort` (`CohortID`),
Copy link
Collaborator

Choose a reason for hiding this comment

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

That change should not show.

FYI - for LORIS code, one tab = 4 spaces so you might want to update your editor to use the same convention.

Comment on lines 701 to 709
CONSTRAINT `UK_mri_protocol_group_target`
UNIQUE (`ProjectID`, `CohortID`, `Visit_label`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@nicolasbrossard is that new unique constraint assumption correct for that table? I don't remember if we said it would be something we would change.

@maximemulder the change with that new constraint is technically not related to the scan type renaming so I would tend to send this in a separate PR to keep things separate.

Comment on lines +20 to +25
ALTER TABLE `mri_protocol_group_target`
ADD CONSTRAINT `UK_mri_protocol_group_target`
UNIQUE (`ProjectID`, `CohortID`, `Visit_label`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment as in the default schema, just tagging this to not forget it at this place:

@nicolasbrossard is that new unique constraint assumption correct for that table? I don't remember if we said it would be something we would change.

@maximemulder the change with that new constraint is technically not related to the scan type renaming so I would tend to send this in a separate PR.

@@ -1486,7 +1486,7 @@ UNLOCK TABLES;
SET SQL_MODE=@OLD_SQL_MODE;

DELETE FROM `mri_scan_type`;
INSERT INTO `mri_scan_type` (ID,Scan_type) VALUES
INSERT INTO `mri_scan_type` (ID,MriScanTypeID) VALUES
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
INSERT INTO `mri_scan_type` (ID,MriScanTypeID) VALUES
INSERT INTO `mri_scan_type` (ID,Name) VALUES

I think the second field field should be Name, not MriScanTypeID

@@ -530,8 +530,9 @@ SET SQL_MODE=@OLD_SQL_MODE;

CREATE TABLE `mri_scan_type` (
`ID` int(11) unsigned NOT NULL auto_increment,
`Scan_type` text NOT NULL,
PRIMARY KEY (`ID`)
`Name` VARCHAR(255) NOT NULL,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
`Name` VARCHAR(255) NOT NULL,
`MriScanTypeName` VARCHAR(255) NOT NULL,

I would tend to name the fields in that table as MriScanTypeID and MriScanTypeName. The first one is so that we can use USING in the queries. The second one is because when looking at the code change, if you need to look for something in the code with the Name field, it is really not specific. However, if we use MriScanTypeName, then you could grep that easily in the code.

Copy link
Contributor Author

@maximemulder maximemulder Aug 1, 2024

Choose a reason for hiding this comment

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

I personally do not like the TableField naming convention as I find it verbose and repetitive (I often use SELECT t.field FROM table AS t in my queries).

The USING and grep arguments do make sense. However, I feel like those are answers to self-inflicted problems that are due to :

  1. How widespread queries are in our codebase (they should ideally be constrained to some database files).
  2. How raw SQL is a lot less searchable than (good) ORMs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

burn 🙃

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@maximemulder maximemulder Aug 1, 2024

Choose a reason for hiding this comment

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

Hhhhm, our current SQL guidelines do not say anything about prefixing fields (outside of the ID) with the table name, so I assume not doing it would be the standard convention. Anyway, I think this is an issue that is broader than this PR, and that it should be discussed at a LORIS meeting. I am fine with blocking until a decision is taken.

I do want to say that I understand both point of views though, so although I have a preference I am not entirely dismissing your version.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I hate to disagree with both of you (joking, I love it) but I only like the ID to have the full table name (because its used in linking and as a reference) I find the full table name redundant in other fields, if we are looking for a query we would look for the table itself in the join clause

Copy link
Contributor Author

@maximemulder maximemulder Aug 19, 2024

Choose a reason for hiding this comment

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

I would tend to name the fields in that table as MriScanTypeID and MriScanTypeName. The first one is so that we can use USING in the queries. The second one is because when looking at the code change, if you need to look for something in the code with the Name field, it is really not specific. However, if we use MriScanTypeName, then you could grep that easily in the code.

After some reflexion I am ok with applying these suggestions. I'll do it if / when I have some time to work again on this PR.

driusan pushed a commit that referenced this pull request Aug 12, 2024
1. Drop the `DEFAULT 0` for foreign key fields
2. Change `DEFAULT '0'` (or `'1'`) to `DEFAULT 0` (or `1`) for other
fields, which is more correct.

Omitted the `mri_protocol.Scan_type` field as it is fixed in #9304
instead.
@maximemulder maximemulder force-pushed the improve-database-mri-scan-type branch 6 times, most recently from b0e1c2b to e1545af Compare September 23, 2024 16:14
@maximemulder maximemulder changed the title [database] Clean-up mri_scan_type [database] Clean up mri_scan_type Sep 23, 2024
@maximemulder maximemulder marked this pull request as draft September 23, 2024 19:58
maximemulder added a commit to maximemulder/Loris that referenced this pull request Sep 25, 2024
1. Drop the `DEFAULT 0` for foreign key fields
2. Change `DEFAULT '0'` (or `'1'`) to `DEFAULT 0` (or `1`) for other
fields, which is more correct.

Omitted the `mri_protocol.Scan_type` field as it is fixed in aces#9304
instead.
@maximemulder maximemulder marked this pull request as ready for review September 26, 2024 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[imaging/database] Clean-up mri_protocol tables
6 participants