Skip to content

[database] Clean up mri_scan_type #9304

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

Merged
merged 6 commits into from
Jan 7, 2025
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
62 changes: 34 additions & 28 deletions SQL/0000-00-00-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,10 @@ INSERT INTO `mri_scanner` (ID) VALUES (0);
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`)
`MriScanTypeID` int(11) unsigned NOT NULL auto_increment,
`MriScanTypeName` VARCHAR(255) NOT NULL,
PRIMARY KEY (`MriScanTypeID`),
CONSTRAINT `UK_mri_scan_type_name` UNIQUE KEY `MriScanTypeName` (`MriScanTypeName`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8;

INSERT INTO `mri_scan_type` VALUES
Expand Down Expand Up @@ -576,7 +577,7 @@ CREATE TABLE `files` (
`EchoNumber` VARCHAR(20) DEFAULT NULL,
`CoordinateSpace` varchar(255) default NULL,
`OutputType` varchar(255) NOT NULL default '',
`AcquisitionProtocolID` int(10) unsigned default NULL,
`MriScanTypeID` int(10) unsigned default NULL,
`FileType` varchar(12) default NULL,
`InsertedByUserID` varchar(255) NOT NULL default '',
`InsertTime` int(10) unsigned NOT NULL default 0,
Expand All @@ -595,11 +596,11 @@ CREATE TABLE `files` (
KEY `sessionid` (`SessionID`),
KEY `outputtype` (`OutputType`),
KEY `filetype_outputtype` (`FileType`,`OutputType`),
KEY `AcquiIndex` (`AcquisitionProtocolID`,`SessionID`),
KEY `ScanIndex` (`MriScanTypeID`,`SessionID`),
KEY `scannerid` (`ScannerID`),
KEY `tarchivesource` (`TarchiveSource`),
KEY `FK_files_HrrtArchiveID_1` (`HrrtArchiveID`),
CONSTRAINT `FK_files_2` FOREIGN KEY (`AcquisitionProtocolID`) REFERENCES `mri_scan_type` (`ID`),
CONSTRAINT `FK_files_2` FOREIGN KEY (`MriScanTypeID`) REFERENCES `mri_scan_type` (`MriScanTypeID`),
CONSTRAINT `FK_files_1` FOREIGN KEY (`SessionID`) REFERENCES `session` (`ID`),
CONSTRAINT `FK_files_3` FOREIGN KEY (`SourceFileID`) REFERENCES `files` (`FileID`),
CONSTRAINT `FK_files_4` FOREIGN KEY (`ProcessProtocolID`) REFERENCES `mri_processing_protocol` (`ProcessProtocolID`),
Expand Down Expand Up @@ -650,7 +651,7 @@ CREATE TABLE `mri_protocol` (
`ID` int(11) unsigned NOT NULL auto_increment,
`CenterID` integer unsigned DEFAULT NULL,
`ScannerID` int(10) unsigned DEFAULT NULL,
`Scan_type` int(10) unsigned NOT NULL default '0',
`MriScanTypeID` int(10) unsigned NOT NULL,
`TR_min` DECIMAL(10,4) DEFAULT NULL,
`TR_max` DECIMAL(10,4) DEFAULT NULL,
`TE_min` DECIMAL(10,4) DEFAULT NULL,
Expand Down Expand Up @@ -682,27 +683,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` (`MriScanTypeID`),
CONSTRAINT `FK_mri_protocol_group_ID_1` FOREIGN KEY (`MriProtocolGroupID`) REFERENCES `mri_protocol_group` (`MriProtocolGroupID`)
) ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;


INSERT INTO mri_protocol (CenterID,Scan_type,TR_min,TR_max,TE_min,
INSERT INTO mri_protocol (CenterID,MriScanTypeID,TR_min,TR_max,TE_min,
TE_max,time_min,time_max,MriProtocolGroupID) VALUES
(NULL,48,8000,14000,80,130,0,200,(SELECT MriProtocolGroupID FROM mri_protocol_group WHERE Name='Default MRI protocol group')),
(NULL,40,1900,2700,10,30,0,500,(SELECT MriProtocolGroupID FROM mri_protocol_group WHERE Name='Default MRI protocol group')),
(NULL,44,2000,2500,2,5,NULL,NULL,(SELECT MriProtocolGroupID FROM mri_protocol_group WHERE Name='Default MRI protocol group')),
(NULL,45,3000,9000,100,550,NULL,NULL,(SELECT MriProtocolGroupID FROM mri_protocol_group WHERE Name='Default MRI protocol group'));

CREATE TABLE `mri_protocol_group_target` (
`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`)
`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`),
CONSTRAINT `UK_mri_protocol_group_target`
UNIQUE (`ProjectID`, `CohortID`, `Visit_label`)
) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `mri_protocol_group_target` (`MriProtocolGroupID`, `ProjectID`, `CohortID`, `Visit_label`)
Expand Down Expand Up @@ -760,17 +764,17 @@ INSERT INTO `mri_protocol_checks_group` (`Name`) VALUES('Default MRI protocol ch

CREATE TABLE `mri_protocol_checks` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Scan_type` int(11) unsigned DEFAULT NULL,
`MriScanTypeID` int(11) unsigned DEFAULT NULL,
`Severity` enum('warning','exclude') DEFAULT NULL,
`Header` varchar(255) DEFAULT NULL,
`ValidMin` decimal(10,4) DEFAULT NULL,
`ValidMax` decimal(10,4) DEFAULT NULL,
`ValidRegex` varchar(255) DEFAULT NULL,
`MriProtocolChecksGroupID` INT(4) UNSIGNED NOT NULL,
PRIMARY KEY (`ID`),
KEY (`Scan_type`),
CONSTRAINT `FK_mriProtocolChecks_ScanType`
FOREIGN KEY (`Scan_type`) REFERENCES `mri_scan_type` (`ID`),
KEY (`MriScanTypeID`),
CONSTRAINT `FK_mri_protocol_checks_scan_type`
FOREIGN KEY (`MriScanTypeID`) REFERENCES `mri_scan_type` (`MriScanTypeID`),
CONSTRAINT `FK_mri_protocol_checks_group_ID_1`
FOREIGN KEY (`MriProtocolChecksGroupID`) REFERENCES `mri_protocol_checks_group` (`MriProtocolChecksGroupID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Expand Down Expand Up @@ -852,7 +856,7 @@ CREATE TABLE `bids_mri_scan_type_rel` (
`BIDSPhaseEncodingDirectionID` int(3) UNSIGNED DEFAULT NULL,
PRIMARY KEY (`MRIScanTypeID`),
KEY `FK_bids_mri_scan_type_rel` (`MRIScanTypeID`),
CONSTRAINT `FK_bids_mri_scan_type_rel` FOREIGN KEY (`MRIScanTypeID`) REFERENCES `mri_scan_type` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_bids_mri_scan_type_rel` FOREIGN KEY (`MRIScanTypeID`) REFERENCES `mri_scan_type` (`MriScanTypeID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_bids_category` FOREIGN KEY (`BIDSCategoryID`) REFERENCES `bids_category`(`BIDSCategoryID`),
CONSTRAINT `FK_bids_scan_type_subcategory` FOREIGN KEY (`BIDSScanTypeSubCategoryID`) REFERENCES `bids_scan_type_subcategory` (`BIDSScanTypeSubCategoryID`),
CONSTRAINT `FK_bids_scan_type` FOREIGN KEY (`BIDSScanTypeID`) REFERENCES `bids_scan_type` (`BIDSScanTypeID`),
Expand All @@ -865,35 +869,35 @@ INSERT INTO bids_mri_scan_type_rel
(MRIScanTypeID, BIDSCategoryID, BIDSScanTypeSubCategoryID, BIDSScanTypeID, BIDSEchoNumber)
VALUES
(
(SELECT ID FROM mri_scan_type WHERE Scan_type = 'flair'),
(SELECT MriScanTypeID FROM mri_scan_type WHERE MriScanTypeName = 'flair'),
(SELECT BIDSCategoryID FROM bids_category WHERE BIDSCategoryName='anat'),
NULL,
(SELECT BIDSScanTypeID FROM bids_scan_type WHERE BIDSSCanType='FLAIR'),
NULL
),
(
(SELECT ID FROM mri_scan_type WHERE Scan_type = 'fMRI'),
(SELECT MriScanTypeID FROM mri_scan_type WHERE MriScanTypeName = 'fMRI'),
(SELECT BIDSCategoryID FROM bids_category WHERE BIDSCategoryName='func'),
(SELECT BIDSScanTypeSubCategoryID FROM bids_scan_type_subcategory WHERE BIDSScanTypeSubCategory='task-rest'),
(SELECT BIDSScanTypeID FROM bids_scan_type WHERE BIDSSCanType='bold'),
NULL
),
(
(SELECT ID FROM mri_scan_type WHERE Scan_type = 't1'),
(SELECT MriScanTypeID FROM mri_scan_type WHERE MriScanTypeName = 't1'),
(SELECT BIDSCategoryID FROM bids_category WHERE BIDSCategoryName='anat'),
NULL,
(SELECT BIDSScanTypeID FROM bids_scan_type WHERE BIDSSCanType='T1w'),
NULL
),
(
(SELECT ID FROM mri_scan_type WHERE Scan_type = 't2'),
(SELECT MriScanTypeID FROM mri_scan_type WHERE MriScanTypeName = 't2'),
(SELECT BIDSCategoryID FROM bids_category WHERE BIDSCategoryName='anat'),
NULL,
(SELECT BIDSScanTypeID FROM bids_scan_type WHERE BIDSSCanType='T2w'),
NULL
),
(
(SELECT ID FROM mri_scan_type WHERE Scan_type = 'dti'),
(SELECT MriScanTypeID FROM mri_scan_type WHERE MriScanTypeName = 'dti'),
(SELECT BIDSCategoryID FROM bids_category WHERE BIDSCategoryName='dwi'),
NULL,
(SELECT BIDSScanTypeID FROM bids_scan_type WHERE BIDSSCanType='dwi'),
Expand Down Expand Up @@ -975,7 +979,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!

Copy link
Contributor

Choose a reason for hiding this comment

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

@cmadjar I agree with you: I don't think a DEFAULT NULL makes sense here so I'd remove it.

Copy link
Contributor Author

@maximemulder maximemulder Oct 23, 2024

Choose a reason for hiding this comment

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

@cmadjar I agree that if this can not be NULL we might just add a NOT NULL constraint, but should it really be in this (already quite large) PR ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's fine, it can be in a different PR. @maximemulder Could you create an issue for it so we do not forget about it?

`Severity` enum('warning','exclude') DEFAULT NULL,
`Header` varchar(255) DEFAULT NULL,
`Value` varchar(255) DEFAULT NULL,
Expand All @@ -987,6 +991,8 @@ CREATE TABLE `mri_violations_log` (
PRIMARY KEY (`LogID`),
CONSTRAINT `FK_tarchive_mriViolationsLog_1`
FOREIGN KEY (`TarchiveID`) REFERENCES `tarchive` (`TarchiveID`),
CONSTRAINT `FK_mri_violations_log_scan_type`
FOREIGN KEY (`MriScanTypeID`) REFERENCES `mri_scan_type` (`MriScanTypeID`),
CONSTRAINT `FK_mri_checks_group_1`
FOREIGN KEY (`MriProtocolChecksGroupID`) REFERENCES `mri_protocol_checks_group` (`MriProtocolChecksGroupID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Expand Down
4 changes: 2 additions & 2 deletions SQL/0000-00-03-ConfigTables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ INSERT INTO Config (ConfigID, Value) SELECT ID, "." FROM ConfigSettings WHERE Na
INSERT INTO Config (ConfigID, Value) SELECT ID, "(?i)phantom" FROM ConfigSettings WHERE Name="LegoPhantomRegex";
INSERT INTO Config (ConfigID, Value) SELECT ID, "(?i)phantom" FROM ConfigSettings WHERE Name="LivingPhantomRegex";
INSERT INTO Config (ConfigID, Value) SELECT ID, "false" FROM ConfigSettings WHERE Name="showTransferStatus";
INSERT INTO Config (ConfigID, Value) SELECT cs.ID, GROUP_CONCAT(mst.Scan_Type) FROM ConfigSettings cs JOIN mri_scan_type mst WHERE cs.Name="tblScanTypes" AND mst.ID=44;
INSERT INTO Config (ConfigID, Value) SELECT cs.ID, GROUP_CONCAT(mst.Scan_Type) FROM ConfigSettings cs JOIN mri_scan_type mst WHERE cs.Name="tblScanTypes" AND mst.ID=45;
INSERT INTO Config (ConfigID, Value) SELECT cs.ID, GROUP_CONCAT(mst.MriScanTypeName) FROM ConfigSettings cs JOIN mri_scan_type mst WHERE cs.Name="tblScanTypes" AND mst.MriScanTypeID=44;
INSERT INTO Config (ConfigID, Value) SELECT cs.ID, GROUP_CONCAT(mst.MriScanTypeName) FROM ConfigSettings cs JOIN mri_scan_type mst WHERE cs.Name="tblScanTypes" AND mst.MriScanTypeID=45;
INSERT INTO Config (ConfigID, Value) SELECT cs.ID, "mri_parameter_form" FROM ConfigSettings cs WHERE cs.Name="ImagingBrowserLinkedInstruments";
INSERT INTO Config (ConfigID, Value) SELECT cs.ID, "radiology_review" FROM ConfigSettings cs WHERE cs.Name="ImagingBrowserLinkedInstruments";

Expand Down
51 changes: 51 additions & 0 deletions SQL/New_patches/2024-25-06-CleanMriScanType.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-- Rename foreign key fields for consistency

ALTER TABLE `mri_scan_type`
CHANGE `ID` `MriScanTypeID` int(11) unsigned NOT NULL auto_increment;

ALTER TABLE `mri_scan_type`
CHANGE `Scan_type` `MriScanTypeName` VARCHAR(255) NOT NULL;

ALTER TABLE `mri_protocol`
CHANGE `Scan_type` `MriScanTypeID` int(10) unsigned NOT NULL;

ALTER TABLE `mri_protocol_checks`
CHANGE `Scan_type` `MriScanTypeID` int(11) unsigned DEFAULT NULL;

ALTER TABLE `mri_violations_log`
CHANGE `Scan_type` `MriScanTypeID` int(11) unsigned DEFAULT NULL;

ALTER TABLE `files`
CHANGE `AcquisitionProtocolID` `MriScanTypeID` int(10) unsigned default NULL;

-- Add unique constraints on table that benefit from them

ALTER TABLE `mri_protocol_group_target`
ADD CONSTRAINT `UK_mri_protocol_group_target`
UNIQUE (`ProjectID`, `CohortID`, `Visit_label`);

ALTER TABLE `mri_scan_type`
ADD CONSTRAINT `UK_mri_scan_type_name`
UNIQUE KEY `MriScanTypeName` (`MriScanTypeName`);

-- Drop wrong default

ALTER TABLE `mri_protocol`
ALTER `MriScanTypeID` DROP DEFAULT;

-- Add missing foreign key constraints

ALTER TABLE `mri_protocol`
ADD CONSTRAINT `FK_mri_protocol_scan_type`
FOREIGN KEY (`MriScanTypeID`) REFERENCES `mri_scan_type` (`MriScanTypeID`);

ALTER TABLE `mri_violations_log`
ADD CONSTRAINT `FK_mri_violations_log_scan_type`
FOREIGN KEY (`MriScanTypeID`) REFERENCES `mri_scan_type` (`MriScanTypeID`);

-- Rename the existing constraints for consistency

ALTER TABLE `mri_protocol_checks`
DROP FOREIGN KEY `FK_mriProtocolChecks_ScanType`,
ADD CONSTRAINT `FK_mri_protocol_checks_scan_type`
FOREIGN KEY (`MriScanTypeID`) REFERENCES `mri_scan_type` (`MriScanTypeID`);
Loading
Loading