Update key column references and enhance error handling in static synchronization functions

This commit modifies key column references in the `fxStaticSyncTableDefinitions` function to align with updated database schema identifiers. Additionally, it introduces a new utility function, `fxStaticSyncKeyColumnsValid`, to validate key columns before executing queries, improving error handling in the `fxStaticSyncFetchRowsByKey` and `fxStaticSyncLoadKeyedRows` functions. The changes aim to enhance the robustness of the synchronization process and provide clearer error feedback during data operations.
This commit is contained in:
2026-07-06 15:34:20 -04:00
parent 2792b06d79
commit a8274dd640
2 changed files with 50 additions and 8 deletions

View File

@ -217,26 +217,44 @@ function fxStaticSyncGetTableRowCount($objDb, $strTable) {
return (int)$objDb->fxGetVar('SELECT COUNT(*) FROM `' . $strTable . '`');
}
function fxStaticSyncKeyColumnsValid($objDb, $strTable, array $tabKeys) {
if (count($tabKeys) === 0) {
return false;
}
$tabCols = fxStaticSyncGetTableColumnNames($objDb, $strTable);
foreach ($tabKeys as $strCol) {
if (!in_array($strCol, $tabCols, true)) {
return false;
}
}
return true;
}
/**
* Charge les lignes ordonnées par clé métier — une entrée par clé (dernière gagne, ordre stable).
* Charge les lignes — une entrée par clé métier (dernière gagne, ordre stable si ORDER BY possible).
*/
function fxStaticSyncFetchRowsByKey($objDb, array $tabDef) {
$strTable = $tabDef['table'];
$tabKeys = $tabDef['keys'];
if (!fxStaticSyncTableExists($objDb, $strTable)) {
return array('rows' => array(), 'duplicate_count' => 0, 'physical_count' => 0);
return array('rows' => array(), 'duplicate_count' => 0, 'physical_count' => 0, 'error' => null);
}
$strOrder = fxStaticSyncSqlOrderByKeys($tabKeys);
$sql = 'SELECT * FROM `' . $strTable . '` ORDER BY ' . $strOrder;
if (!fxStaticSyncKeyColumnsValid($objDb, $strTable, $tabKeys)) {
return array('rows' => array(), 'duplicate_count' => 0, 'physical_count' => 0, 'error' => 'bad_keys');
}
$sql = 'SELECT * FROM `' . $strTable . '` ORDER BY ' . fxStaticSyncSqlOrderByKeys($tabKeys);
$tabResults = $objDb->fxGetResults($sql);
$tabKeyed = array();
$intDupes = 0;
$intPhysical = 0;
if (!is_array($tabResults)) {
return array('rows' => array(), 'duplicate_count' => 0, 'physical_count' => 0);
return array('rows' => array(), 'duplicate_count' => 0, 'physical_count' => 0, 'error' => 'query_failed');
}
foreach ($tabResults as $row) {
@ -255,6 +273,7 @@ function fxStaticSyncFetchRowsByKey($objDb, array $tabDef) {
'rows' => $tabKeyed,
'duplicate_count' => $intDupes,
'physical_count' => $intPhysical,
'error' => null,
);
}
@ -279,6 +298,12 @@ function fxStaticSyncLoadKeyedRows($objDb, array $tabDef, array $tabCompareCols
}
$tabFetch = fxStaticSyncFetchRowsByKey($objDb, $tabDef);
if ($tabFetch['error'] === 'bad_keys') {
return array('error' => 'bad_keys', 'rows' => array(), 'count' => 0, 'physical_count' => 0);
}
if ($tabFetch['error'] === 'query_failed') {
return array('error' => 'query_failed', 'rows' => array(), 'count' => 0, 'physical_count' => 0);
}
$tabKeyed = array();
if ($tabCompareCols === null) {
@ -402,7 +427,7 @@ function fxStaticSyncDbLastError($objDb) {
}
/**
* Exécute une requête et remonte l'erreur MySQL (sans die ni echo legacy).
* Exécute une requête d'écriture — erreur remontée dans le message flash (fxQuery n'echo pas).
*/
function fxStaticSyncExecQuery($objDb, $strSql) {
if (!$objDb) {
@ -619,6 +644,8 @@ function fxStaticSyncStatusLabel($strStatus, array $tabCmp = null) {
return 'Structures non alignées';
case 'no_connection':
return 'Connexion impossible';
case 'error':
return 'Erreur lecture';
}
if (is_array($tabCmp)) {
@ -659,6 +686,8 @@ function fxStaticSyncStatusClass($strStatus) {
return 'ss-muted';
case 'schema_mismatch':
return 'ss-schema';
case 'error':
return 'ss-bad';
default:
return 'ss-bad';
}
@ -738,6 +767,19 @@ function fxStaticSyncCompareEnvPair($objSource, $objTarget, array $tabDef) {
$tabSourceData = fxStaticSyncLoadKeyedRows($objSource, $tabDef, $tabSchema['compare_cols']);
$tabTargetData = fxStaticSyncLoadKeyedRows($objTarget, $tabDef, $tabSchema['compare_cols']);
if (!empty($tabSourceData['error']) || !empty($tabTargetData['error'])) {
return array(
'status' => 'error',
'source_count' => isset($tabSourceData['count']) ? $tabSourceData['count'] : 0,
'target_count' => isset($tabTargetData['count']) ? $tabTargetData['count'] : 0,
'missing_keys' => array(),
'extra_keys' => array(),
'diff_keys' => array(),
'schema_missing_cols' => array(),
'schema_extra_cols' => array(),
);
}
return fxStaticSyncCompareTable($tabSourceData, $tabTargetData, $tabSchema);
}

View File

@ -77,13 +77,13 @@ function fxStaticSyncTableDefinitions() {
array(
'table' => 'modes_remboursements',
'label' => 'Modes remboursement',
'keys' => array('mod_id'),
'keys' => array('mr_id'),
'ignore_cols' => array(),
),
array(
'table' => 'types_remboursements',
'label' => 'Types remboursement',
'keys' => array('typ_id'),
'keys' => array('tr_id'),
'ignore_cols' => array(),
),
array(