diff --git a/api/index.php b/api/index.php index d651c39..0793d77 100644 --- a/api/index.php +++ b/api/index.php @@ -8990,7 +8990,7 @@ function importMergeBackup(PDO $db): void { $db->beginTransaction(); try { - // --- 1. Tables référentielles simples : skip si key existe --- +// --- 1. Tables référentielles simples : upsert (update label/icon si key existe déjà) --- $refTables = [ 'categories' => ['key'], 'locations' => ['key'], @@ -9004,9 +9004,20 @@ function importMergeBackup(PDO $db): void { $where = []; $params = []; foreach ($uniqueCols as $c) { $where[] = "$c = ?"; $params[] = $row[$c]; } - $exists = $db->prepare("SELECT 1 FROM {$table} WHERE " . implode(' AND ', $where)); + $exists = $db->prepare("SELECT id FROM {$table} WHERE " . implode(' AND ', $where)); $exists->execute($params); - if ($exists->fetch()) continue; + $existingId = $exists->fetchColumn(); + if ($existingId) { + // Update label/icon (and keywords if present) from prod, keep local id/is_builtin + $updCols = array_diff(array_keys($row), $uniqueCols, ['is_builtin', 'sort_order']); + if (!empty($updCols)) { + $setClause = implode(', ', array_map(fn($c) => "$c = ?", $updCols)); + $updParams = array_map(fn($c) => $row[$c], $updCols); + $updParams[] = $existingId; + $db->prepare("UPDATE {$table} SET {$setClause} WHERE id = ?")->execute($updParams); + } + continue; + } $cols = array_keys($row); $stmt = $db->prepare("INSERT INTO {$table} (" . implode(',', $cols) . ") VALUES (" . implode(',', array_fill(0, count($cols), '?')) . ")"); $stmt->execute(array_values($row));