Repository navigation
[Candidate Identifier (Rework Part 1)] Database Schema and Configuration Migration #11209
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
+268
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
SQL/New_patches/2026-09-22-add_candidate_identifiers_tables.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Migrates legacy candidate identifier configuration from config.xml | ||
| * into the candidate_identifier_types database table | ||
| * | ||
| * Does not modify config.xml or existing candidate identifier values | ||
| */ | ||
|
|
||
| require_once __DIR__ . "/../generic_includes.php"; | ||
|
|
||
| $config = \NDB_Factory::singleton()->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<string, mixed> $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"; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be using the calculated $generation variable from line 29?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The $generation variable is only available inside the function
convertIdentifierFormat.