Skip to content
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
60 changes: 60 additions & 0 deletions SQL/0000-00-02-Permission.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 3 additions & 0 deletions SQL/9999-99-99-drop_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
59 changes: 59 additions & 0 deletions SQL/New_patches/2026-09-22-add_candidate_identifiers_tables.sql
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;
146 changes: 146 additions & 0 deletions tools/single_use/migrate_candidate_identifiers.php
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,

Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

image

The $generation variable is only available inside the function convertIdentifierFormat.

'AllowMultiple' => 0,
]
);

echo "Created identifier type: $name\n";
}
Loading