From 89c90d01fecfd27b707c8ec04eedacc91edcbb64 Mon Sep 17 00:00:00 2001 From: HachemJ Date: Tue, 3 Nov 2026 09:33:58 -0500 Subject: [PATCH] Refactor candidate identifier type structure and database schema --- SQL/0000-00-02-Permission.sql | 60 +++++++ SQL/9999-99-99-drop_tables.sql | 3 + ...09-22-add_candidate_identifiers_tables.sql | 59 +++++++ .../migrate_candidate_identifiers.php | 146 ++++++++++++++++++ 4 files changed, 268 insertions(+) create mode 100644 SQL/New_patches/2026-09-22-add_candidate_identifiers_tables.sql create mode 100644 tools/single_use/migrate_candidate_identifiers.php diff --git a/SQL/0000-00-02-Permission.sql b/SQL/0000-00-02-Permission.sql index 259eafc13f..eba3acd35e 100644 --- a/SQL/0000-00-02-Permission.sql +++ b/SQL/0000-00-02-Permission.sql @@ -326,3 +326,63 @@ CREATE TABLE `testnames_permissions_rel` ( CONSTRAINT `FK_testnames_permissions_rel_test` FOREIGN KEY (`TestID`) REFERENCES `test_names` (`ID`), CONSTRAINT `FK_testnames_permissions_rel_perm` FOREIGN KEY (`permID`) REFERENCES `permissions` (`permID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `candidate_identifier_types` ( + `CandidateIdentifierTypeID` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Name` varchar(100) NOT NULL, + `Format` varchar(255) NOT NULL, + `IsRequired` boolean NOT NULL, + `IsUnique` boolean NOT NULL, + `AutoGenerate` boolean NOT NULL, + `AllowMultiple` boolean NOT NULL, + `permID` int(10) unsigned DEFAULT NULL, + PRIMARY KEY (`CandidateIdentifierTypeID`), + UNIQUE KEY (`Name`), + CONSTRAINT `FK_candidate_identifier_type_permission` + FOREIGN KEY (`permID`) + REFERENCES `permissions` (`permID`) + ON DELETE SET NULL + ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +INSERT INTO `candidate_identifier_types` + (`Name`, `Format`, `IsRequired`, `IsUnique`, + `AutoGenerate`, `AllowMultiple`) +VALUES + ( + 'PSCID', + '{SITE:ALIAS}{SEQUENCE:4,FORMAT:alphanumeric}', + 1, 1, 1, 0 + ); + +CREATE TABLE `candidate_identifier_types_project_rel` ( + `CandidateIdentifierTypeID` int(10) unsigned NOT NULL, + `ProjectID` int(10) unsigned NOT NULL, + PRIMARY KEY (`CandidateIdentifierTypeID`, `ProjectID`), + CONSTRAINT `FK_candidate_identifier_type_project_type` + FOREIGN KEY (`CandidateIdentifierTypeID`) + REFERENCES `candidate_identifier_types` (`CandidateIdentifierTypeID`) + ON DELETE CASCADE, + CONSTRAINT `FK_candidate_identifier_type_project_project` + FOREIGN KEY (`ProjectID`) + REFERENCES `Project` (`ProjectID`) + ON DELETE CASCADE + ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE `candidate_identifiers` ( + `CandidateIdentifierID` int(10) unsigned NOT NULL AUTO_INCREMENT, + `CandidateID` int(10) unsigned NOT NULL, + `CandidateIdentifierTypeID` int(10) unsigned NOT NULL, + `Value` varchar(255) NOT NULL, + `DateAdded` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `DateEdited` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`CandidateIdentifierID`), + CONSTRAINT `FK_candidate_identifier_candidate` + FOREIGN KEY (`CandidateID`) + REFERENCES `candidate` (`ID`) + ON DELETE CASCADE, + CONSTRAINT `FK_candidate_identifier_type` + FOREIGN KEY (`CandidateIdentifierTypeID`) + REFERENCES `candidate_identifier_types` (`CandidateIdentifierTypeID`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/SQL/9999-99-99-drop_tables.sql b/SQL/9999-99-99-drop_tables.sql index 9ffde2a948..c013c92f73 100644 --- a/SQL/9999-99-99-drop_tables.sql +++ b/SQL/9999-99-99-drop_tables.sql @@ -280,6 +280,9 @@ DROP TABLE IF EXISTS `session_status`; DROP TABLE IF EXISTS `session`; DROP TABLE IF EXISTS `user_psc_rel`; DROP TABLE IF EXISTS `user_project_rel`; +DROP TABLE IF EXISTS `candidate_identifiers`; +DROP TABLE IF EXISTS `candidate_identifier_types_project_rel`; +DROP TABLE IF EXISTS `candidate_identifier_types`; DROP TABLE IF EXISTS `candidate`; DROP TABLE IF EXISTS `caveat_options`; DROP TABLE IF EXISTS `users`; diff --git a/SQL/New_patches/2026-09-22-add_candidate_identifiers_tables.sql b/SQL/New_patches/2026-09-22-add_candidate_identifiers_tables.sql new file mode 100644 index 0000000000..327f7ac8af --- /dev/null +++ b/SQL/New_patches/2026-09-22-add_candidate_identifiers_tables.sql @@ -0,0 +1,59 @@ +CREATE TABLE `candidate_identifier_types` ( + `CandidateIdentifierTypeID` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Name` varchar(100) NOT NULL, + `Format` varchar(255) NOT NULL, + `IsRequired` boolean NOT NULL, + `IsUnique` boolean NOT NULL, + `AutoGenerate` boolean NOT NULL, + `AllowMultiple` boolean NOT NULL, + `permID` int(10) unsigned DEFAULT NULL, + PRIMARY KEY (`CandidateIdentifierTypeID`), + UNIQUE KEY (`Name`), + CONSTRAINT `FK_candidate_identifier_type_permission` + FOREIGN KEY (`permID`) + REFERENCES `permissions` (`permID`) + ON DELETE SET NULL + ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +INSERT INTO `candidate_identifier_types` + (`Name`, `Format`, `IsRequired`, `IsUnique`, + `AutoGenerate`, `AllowMultiple`) +VALUES + ( + 'PSCID', + '{SITE:ALIAS}{SEQUENCE:4,FORMAT:alphanumeric}', + 1, 1, 1, 0 + ); + +CREATE TABLE `candidate_identifier_types_project_rel` ( + `CandidateIdentifierTypeID` int(10) unsigned NOT NULL, + `ProjectID` int(10) unsigned NOT NULL, + PRIMARY KEY (`CandidateIdentifierTypeID`, `ProjectID`), + CONSTRAINT `FK_candidate_identifier_type_project_type` + FOREIGN KEY (`CandidateIdentifierTypeID`) + REFERENCES `candidate_identifier_types` (`CandidateIdentifierTypeID`) + ON DELETE CASCADE, + CONSTRAINT `FK_candidate_identifier_type_project_project` + FOREIGN KEY (`ProjectID`) + REFERENCES `Project` (`ProjectID`) + ON DELETE CASCADE + ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE `candidate_identifiers` ( + `CandidateIdentifierID` int(10) unsigned NOT NULL AUTO_INCREMENT, + `CandidateID` int(10) unsigned NOT NULL, + `CandidateIdentifierTypeID` int(10) unsigned NOT NULL, + `Value` varchar(255) NOT NULL, + `DateAdded` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `DateEdited` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`CandidateIdentifierID`), + CONSTRAINT `FK_candidate_identifier_candidate` + FOREIGN KEY (`CandidateID`) + REFERENCES `candidate` (`ID`) + ON DELETE CASCADE, + CONSTRAINT `FK_candidate_identifier_type` + FOREIGN KEY (`CandidateIdentifierTypeID`) + REFERENCES `candidate_identifier_types` (`CandidateIdentifierTypeID`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; \ No newline at end of file diff --git a/tools/single_use/migrate_candidate_identifiers.php b/tools/single_use/migrate_candidate_identifiers.php new file mode 100644 index 0000000000..5193a655c4 --- /dev/null +++ b/tools/single_use/migrate_candidate_identifiers.php @@ -0,0 +1,146 @@ +config(); +$DB = $lorisInstance->getDatabaseConnection(); + +$identifierSettings = [ + 'PSCID' => $config->getSetting('PSCID'), + 'ExternalID' => $config->getSetting('ExternalID'), +]; + +/** + * Convert a legacy XML identifier definition into a format template + * + * @param array $settings Legacy identifier settings + * + * @return string + */ +function convertIdentifierFormat(array $settings): string +{ + $generation = match ($settings['generation'] ?? '') { + 'sequential' => 'SEQUENCE', + 'random' => 'RANDOM', + 'user' => null, + default => throw new \RuntimeException('Unsupported identifier generation method'), + }; + + $seqs = $settings['structure']['seq'] ?? []; + + if (isset($seqs['@'])) { + $seqs = [$seqs]; + } + + $format = ''; + $generationCount = 0; + + foreach ($seqs as $seq) { + $attributes = $seq['@'] ?? []; + + switch ($attributes['type'] ?? '') { + case 'siteAbbrev': + $format .= '{SITE:ALIAS}'; + break; + + case 'projectAbbrev': + $format .= '{PROJECT:ALIAS}'; + break; + + case 'static': + $format .= $seq['#'] ?? ''; + break; + + case 'numeric': + case 'alphanumeric': + case 'alpha': + $generationCount++; + + $length = $attributes['length'] ?? null; + + if ( + $length === null || !ctype_digit((string) $length) + || (int) $length < 1 + ) { + throw new \RuntimeException( + 'Identifier sequence must have a positive fixed length' + ); + } + + $segment = $generation === null + ? sprintf( + '{%s:%d', + strtoupper($attributes['type']), + (int) $length + ) + : sprintf( + '{%s:%d,FORMAT:%s', + $generation, + (int) $length, + $attributes['type'] + ); + + if ($generation !== null) { + foreach (['min' => 'MIN', 'max' => 'MAX'] as $key => $option) { + if (isset($attributes[$key])) { + $segment .= ',' . $option . ':' . $attributes[$key]; + } + } + } + + $format .= $segment . '}'; + break; + + default: + throw new \RuntimeException( + 'Unsupported identifier sequence type: ' + . ($attributes['type'] ?? '(missing)') + ); + } + } + + if ($generationCount !== 1) { + throw new \RuntimeException( + 'Expected exactly one identifier segment' + ); + } + + return $format; +} + +foreach ($identifierSettings as $name => $settings) { + $existingID = $DB->pselectOne( + "SELECT CandidateIdentifierTypeID + FROM candidate_identifier_types + WHERE Name = :name", + ['name' => $name] + ); + + if (!empty($existingID)) { + echo "Skipping existing identifier type: $name\n"; + continue; + } + + $format = convertIdentifierFormat($settings); + + $DB->insert( + 'candidate_identifier_types', + [ + 'Name' => $name, + 'Format' => $format, + 'IsRequired' => $name === 'PSCID' ? 1 : 0, + 'IsUnique' => 1, + 'AutoGenerate' => $settings['generation'] === 'user' ? 0 : 1, + 'AllowMultiple' => 0, + ] + ); + + echo "Created identifier type: $name\n"; +} \ No newline at end of file