From 216c9dbefa31418b33bff2d922be5618f1550558 Mon Sep 17 00:00:00 2001 From: Muhammad Ibrahim Date: Fri, 19 Sep 2025 16:07:29 +0100 Subject: [PATCH] improved duplicate repo handling --- backend/src/routes/hostRoutes.js | 46 +++++++++++++------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/backend/src/routes/hostRoutes.js b/backend/src/routes/hostRoutes.js index 289ef11..13b00cc 100644 --- a/backend/src/routes/hostRoutes.js +++ b/backend/src/routes/hostRoutes.js @@ -306,8 +306,17 @@ router.post('/update', validateApiCredentials, [ where: { hostId: host.id } }); - // Process each repository + // Deduplicate repositories by URL+distribution+components to avoid constraint violations + const uniqueRepos = new Map(); for (const repoData of repositories) { + const key = `${repoData.url}|${repoData.distribution}|${repoData.components}`; + if (!uniqueRepos.has(key)) { + uniqueRepos.set(key, repoData); + } + } + + // Process each unique repository + for (const repoData of uniqueRepos.values()) { // Find or create repository let repo = await tx.repository.findFirst({ where: { @@ -332,34 +341,15 @@ router.post('/update', validateApiCredentials, [ }); } - // Create host repository relationship (handle duplicates gracefully) - try { - await tx.hostRepository.create({ - data: { - hostId: host.id, - repositoryId: repo.id, - isEnabled: repoData.isEnabled !== false, // Default to enabled - lastChecked: new Date() - } - }); - } catch (error) { - // If it's a duplicate constraint error, update the existing record - if (error.code === 'P2002') { - await tx.hostRepository.updateMany({ - where: { - hostId: host.id, - repositoryId: repo.id - }, - data: { - isEnabled: repoData.isEnabled !== false, - lastChecked: new Date() - } - }); - } else { - // Re-throw other errors - throw error; + // Create host repository relationship + await tx.hostRepository.create({ + data: { + hostId: host.id, + repositoryId: repo.id, + isEnabled: repoData.isEnabled !== false, // Default to enabled + lastChecked: new Date() } - } + }); } } });