From 709044efeff8411931f606ea77cd653f58503fbb Mon Sep 17 00:00:00 2001 From: stephan Date: Mon, 6 Jul 2026 14:58:59 -0400 Subject: [PATCH] Add enhanced data synchronization functions and improve user feedback This commit introduces new functions for confirming database names during synchronization, enhancing safety when syncing to production environments. The `fxStaticSyncResolveDataStatus` and `fxStaticSyncFormatDataDiffSummary` functions are added to provide clearer status reporting and summaries of data discrepancies. Additionally, the synchronization process is refined to improve user feedback, including clearer messages regarding data alignment and discrepancies. These changes aim to enhance the robustness and clarity of the database synchronization workflow. --- php/inc_fx_static_sync.php | 76 ++++++++++++++--- php/sync_static_db.php | 90 ++++++++++++++++++--- sql/MSIN-eve-acces-v2-drop-foreign-keys.sql | 19 +++++ 3 files changed, 163 insertions(+), 22 deletions(-) create mode 100644 sql/MSIN-eve-acces-v2-drop-foreign-keys.sql diff --git a/php/inc_fx_static_sync.php b/php/inc_fx_static_sync.php index 9beaccc..356566f 100644 --- a/php/inc_fx_static_sync.php +++ b/php/inc_fx_static_sync.php @@ -249,6 +249,52 @@ function fxStaticSyncWriteAllowed($strEnvId) { return $strEnvId !== 'client_prod'; } +/** Environnements client : confirmation renforcée (nom de BD à retaper). */ +function fxStaticSyncRequiresDbConfirm($strEnvId) { + return ($strEnvId === 'client_preprod' || $strEnvId === 'client_prod'); +} + +function fxStaticSyncResolveDataStatus(array $tabCmp) { + $intMissing = count($tabCmp['missing_keys']); + $intExtra = count($tabCmp['extra_keys']); + $intDiff = count($tabCmp['diff_keys']); + + if ($intMissing === 0 && $intExtra === 0 && $intDiff === 0) { + return 'ok'; + } + if ($intMissing > 0 && $intExtra > 0) { + return 'mixed'; + } + if ($intExtra > 0) { + return 'extra_rows'; + } + if ($intMissing > 0) { + return 'missing_rows'; + } + + return 'content_diff'; +} + +/** Résumé lisible des écarts de données (toujours affiché quand status !== ok). */ +function fxStaticSyncFormatDataDiffSummary(array $tabCmp) { + $tabParts = array(); + $intMissing = count($tabCmp['missing_keys']); + $intExtra = count($tabCmp['extra_keys']); + $intDiff = count($tabCmp['diff_keys']); + + if ($intMissing > 0) { + $tabParts[] = 'manque ' . $intMissing; + } + if ($intExtra > 0) { + $tabParts[] = 'en trop ' . $intExtra; + } + if ($intDiff > 0) { + $tabParts[] = 'contenu diff. ' . $intDiff; + } + + return implode(' · ', $tabParts); +} + /** * @return array status: ok|content_diff|missing_rows|extra_rows|missing_table|schema_mismatch|no_connection|error */ @@ -287,14 +333,11 @@ function fxStaticSyncCompareTable(array $tabSourceData, array $tabTargetData, ar } } - $strStatus = 'ok'; - if (count($tabExtra) > 0) { - $strStatus = 'extra_rows'; - } elseif (count($tabMissing) > 0) { - $strStatus = 'missing_rows'; - } elseif (count($tabDiff) > 0) { - $strStatus = 'content_diff'; - } + $strStatus = fxStaticSyncResolveDataStatus(array( + 'missing_keys' => $tabMissing, + 'extra_keys' => $tabExtra, + 'diff_keys' => $tabDiff, + )); return array( 'status' => $strStatus, @@ -514,16 +557,26 @@ function fxStaticSyncCopyTable($objSource, $objTarget, $strTable, array $tabDef ); } -function fxStaticSyncStatusLabel($strStatus) { +function fxStaticSyncStatusLabel($strStatus, array $tabCmp = null) { + if ($strStatus === 'ok') { + return 'Données alignées'; + } + if (is_array($tabCmp) && $strStatus !== 'schema_mismatch' && $strStatus !== 'missing_table' && $strStatus !== 'no_connection') { + $strSummary = fxStaticSyncFormatDataDiffSummary($tabCmp); + if ($strSummary !== '') { + return 'Écarts : ' . $strSummary; + } + } + switch ($strStatus) { - case 'ok': - return 'Données alignées'; case 'content_diff': return 'Contenu différent'; case 'missing_rows': return 'Lignes manquantes'; case 'extra_rows': return 'Lignes en trop'; + case 'mixed': + return 'Manque et en trop'; case 'missing_table': return 'Table absente'; case 'schema_mismatch': @@ -541,6 +594,7 @@ function fxStaticSyncStatusClass($strStatus) { return 'ss-ok'; case 'content_diff': case 'missing_rows': + case 'mixed': return 'ss-warn'; case 'extra_rows': return 'ss-bad'; diff --git a/php/sync_static_db.php b/php/sync_static_db.php index 75174ce..f1f1662 100644 --- a/php/sync_static_db.php +++ b/php/sync_static_db.php @@ -42,7 +42,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST[' } elseif (!fxStaticSyncWriteAllowed($strEnvId)) { $strFlash = 'Sync désactivée vers client prod — comparaison seule.'; $strFlashType = 'bad'; + } elseif (fxStaticSyncRequiresDbConfirm($strEnvId)) { + $strConfirmDb = trim($_POST['confirm_db'] ?? ''); + $strExpectedDb = $tabEnv['config']['db'] ?? ''; + if ($strConfirmDb !== $strExpectedDb) { + $strFlash = 'Sync annulée — nom de base incorrect (attendu : ' . $strExpectedDb . ').'; + $strFlashType = 'bad'; + } else { + $blnDbConfirmed = true; + } } else { + $blnDbConfirmed = true; + } + + if (!empty($blnDbConfirmed)) { $tabConnections = fxStaticSyncOpenConnections(); $objSource = $tabConnections['dev_preprod']; $objTarget = $tabConnections[$strEnvId]; @@ -59,8 +72,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST[' $strFlash .= ' Vérification : aligné.'; $strFlashType = 'ok'; } else { - $strFlash .= ' ATTENTION — après sync, statut : ' - . fxStaticSyncStatusLabel($tabVerify['status']) . '.'; + $strFlash .= ' ATTENTION — après sync : ' + . fxStaticSyncStatusLabel($tabVerify['status'], $tabVerify) . '.'; $strFlashType = 'bad'; } } else { @@ -108,8 +121,9 @@ if ($strDetailTable !== '' && $strDetailEnv !== '') { .ss-bad { background: #f8d7da; color: #721c24; } .ss-muted { background: #e9ecef; color: #495057; } .ss-schema { background: #fde2e2; color: #7f1d1d; border: 1px solid #f5c2c2; } - .badge { display: inline-block; padding: 3px 8px; border-radius: 4px; font-size: 0.82rem; font-weight: 600; } + .badge { display: inline-block; padding: 3px 8px; border-radius: 4px; font-size: 0.82rem; font-weight: 600; max-width: 100%; line-height: 1.35; } .count { font-size: 0.82rem; color: #444; margin-top: 4px; } + .count-hint { font-size: 0.78rem; color: #666; } .btn { display: inline-block; margin-top: 6px; padding: 4px 10px; font-size: 0.82rem; border: 1px solid #888; background: #fff; border-radius: 4px; cursor: pointer; text-decoration: none; color: #222; } .btn:hover { background: #f0f0f0; } .btn-sync { border-color: #2e6da4; color: #1a4f7a; } @@ -128,8 +142,9 @@ if ($strDetailTable !== '' && $strDetailEnv !== '') {
Attention — opération destructive. Sync = DELETE puis recopie complète depuis dev préprod. - Ne pas utiliser vers client prod (bouton désactivé). - Si le statut indique Structures non alignées, aligner les structures via Navicat — aucune sync possible tant que ce n'est pas réglé. + Client prod : lecture seule (aucun bouton Sync). + Client préprod : confirmation avec le nom exact de la base. + Structures non alignées : corriger via Navicat avant toute sync.

Source de vérité : dev préprod (). @@ -142,11 +157,14 @@ if ($strDetailTable !== '' && $strDetailEnv !== '') {

Données alignées - Manque ou contenu différent - Lignes en trop + Écarts (manque / en trop / contenu) Structures non alignées Table absente / connexion
+

+ Compteur : cible · source (lignes dans l'environnement comparé · lignes en dev préprod). + Les écarts détaillent combien de clés manquent, sont en trop ou diffèrent — indépendamment du total. +

@@ -181,12 +199,13 @@ if ($strDetailTable !== '' && $strDetailEnv !== '') { continue; } $strClass = fxStaticSyncStatusClass($tabCmp['status']); - $strLabel = fxStaticSyncStatusLabel($tabCmp['status']); + $strLabel = fxStaticSyncStatusLabel($tabCmp['status'], $tabCmp); + $strDbName = $tabEnv['config']['db'] ?? ''; ?> @@ -243,7 +269,10 @@ if ($strDetailTable !== '' && $strDetailEnv !== '') {
  • - () + ()
    Source :
    Cible : @@ -258,4 +287,43 @@ if ($strDetailTable !== '' && $strDetailEnv !== '') { + diff --git a/sql/MSIN-eve-acces-v2-drop-foreign-keys.sql b/sql/MSIN-eve-acces-v2-drop-foreign-keys.sql new file mode 100644 index 0000000..47211c7 --- /dev/null +++ b/sql/MSIN-eve-acces-v2-drop-foreign-keys.sql @@ -0,0 +1,19 @@ +/* + MSIN — Retrait FK acces v2 + Base : devinscription_preprod UNIQUEMENT + Aucune donnee touchee. +*/ + +USE devinscription_preprod; + +SET NAMES utf8mb4; + +ALTER TABLE `inscriptions_eve_role_permissions` + DROP FOREIGN KEY `fk_erp_role`, + DROP FOREIGN KEY `fk_erp_perm`; + +ALTER TABLE `inscriptions_eve_acces` + DROP FOREIGN KEY `fk_ea_role`; + +ALTER TABLE `inscriptions_eve_acces_extra` + DROP FOREIGN KEY `fk_ae_perm`;
  • - / ligne(s) + Cible · Source
    &detail_env=">Détails -
    +
    + +
    Lecture seule