Skip to content

Commit

Permalink
Improve handling of multiple SqlProvisioners (CFM-74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benn Oshrin committed Aug 30, 2025
1 parent 2b32421 commit 25b3e17
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,20 @@ public function provision(
object $data, // $data is currently only \App\Model\Entity\Person, but that might change
string $eligibility
): array {
// Connect to the target database
$this->Servers->SqlServers->connect($provisioningTarget->sql_provisioner->server_id, 'targetdb');
// Connect to the target database. We append the SQL Provisioner ID to generate a
// unique data source name in case there are multiple SQL Provisioners in the same CO.
// Table Alias construction in the sync* commands will need to do the same thing.

$cxnLabel = "targetdb" . $provisioningTarget->sql_provisioner->id;

$this->Servers->SqlServers->connect($provisioningTarget->sql_provisioner->server_id, $cxnLabel);

return $this->syncEntity(
$provisioningTarget->sql_provisioner,
$className,
$data,
$eligibility
$eligibility,
$cxnLabel
);
}

Expand All @@ -395,7 +401,8 @@ protected function syncEntity(
string $entityName,
$data,
string $eligibility,
string $dataSource='targetdb'): array {
string $dataSource='targetdb'
): array {
// Find the model config, which may vary depending on the type of entity.
// We don't check secondaryModels because those aren't directly provisioned.
$mconfig = $this->primaryModels[$entityName]
Expand All @@ -409,11 +416,11 @@ protected function syncEntity(
// XXX similar code in syncReferenceData, refactor?
$options = [
'table' => $SqlProvisioner->table_prefix . $mconfig['table'],
'alias' => $mconfig['name'],
'alias' => $mconfig['name'] . $SqlProvisioner->id,
'connection' => ConnectionManager::get($dataSource)
];

$SpTable = TableUtilities::getTableFromRegistry(alias: $mconfig['name'], options: $options);
$SpTable = TableUtilities::getTableFromRegistry(alias: $options['alias'], options: $options);

try {
$curEntity = $SpTable->get($data->id);
Expand Down Expand Up @@ -565,11 +572,11 @@ public function syncReferenceData(int $id, string $dataSource='targetdb') {

$options = [
'table' => $spcfg->table_prefix . $m['table'],
'alias' => $m['name'],
'alias' => $m['name'] . $SqlProvisioner->id,
'connection' => ConnectionManager::get($dataSource)
];

$SpTable = TableUtilities::getTableFromRegistry(alias: $m['name'], options: $options);
$SpTable = TableUtilities::getTableFromRegistry(alias: $options['alias'], options: $options);

// Next get the source table model

Expand Down Expand Up @@ -640,7 +647,8 @@ protected function syncRelatedEntities(
string $relatedEntityName,
$parentData,
string $eligibility,
string $dataSource='targetdb') {
string $dataSource='targetdb'
) {
// eg: person_id
$parentFk = StringUtilities::entityToForeignKey($parentData);
// eg: names
Expand All @@ -657,11 +665,11 @@ protected function syncRelatedEntities(

$options = [
'table' => $SqlProvisioner->table_prefix . $mconfig['table'],
'alias' => $mconfig['name'],
'alias' => $mconfig['name'] . $SqlProvisioner->id,
'connection' => ConnectionManager::get($dataSource)
];

$SpTable = TableUtilities::getTableFromRegistry(alias: $mconfig['name'], options: $options);
$SpTable = TableUtilities::getTableFromRegistry(alias: $options['alias'], options: $options);

// We have the source values, but we need to convert them to arrays
// for patchEntities
Expand Down Expand Up @@ -741,11 +749,11 @@ protected function syncRelatedEntities(

$options = [
'table' => $SqlProvisioner->table_prefix . $subconfig['table'],
'alias' => $subconfig['name'],
'alias' => $subconfig['name'] . $SqlProvisioner->id,
'connection' => ConnectionManager::get($dataSource)
];

$SubTable = TableUtilities::getTableFromRegistry(alias: $subconfig['name'], options: $options);
$SubTable = TableUtilities::getTableFromRegistry(alias: $options['alias'], options: $options);

foreach($toDelete as $d) {
// We shouldn't get here if either $parentKey or $d->id is null...
Expand Down
4 changes: 4 additions & 0 deletions app/src/Lib/Util/DBALConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public static function factory(?ConsoleIo $io=null, string $connection='default'
'driver' => ($cfg['driver'] == 'Cake\Database\Driver\Postgres' ? "pdo_pgsql" : "mysqli")
];

if(!empty($cfg['port'])) {
$cfargs['port'] = $cfg['port'];
}

// For MySQL SSL
if(!empty($cfg['ssl_ca'])) {
$cfargs['ssl_ca'] = $cfg['ssl_ca'];
Expand Down
4 changes: 2 additions & 2 deletions app/src/Lib/Util/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ protected function processSchema(
$options = [];

if(isset($iCfg->unique) && $iCfg->unique) {
$table->addUniqueConstraint($iCfg->columns, $iName, $flags, $options);
$table->addUniqueConstraint($iCfg->columns, $tablePrefix.$iName, $flags, $options);
} else {
$table->addIndex($iCfg->columns, $iName, $flags, $options);
$table->addIndex($iCfg->columns, $tablePrefix.$iName, $flags, $options);
}
}
}
Expand Down

0 comments on commit 25b3e17

Please sign in to comment.