fix: gérer les violations UNIQUE dans migrateExistingItems (supprimer les doublons)

This commit is contained in:
Manus Deploy
2026-05-03 06:31:56 -04:00
parent a8ba0ee979
commit e502ca97d8

View File

@@ -503,6 +503,7 @@ export async function migrateExistingItems(): Promise<MigrationSummary> {
const normalizedTitle = buildMergeKey(row.titre || ""); const normalizedTitle = buildMergeKey(row.titre || "");
const newDedupKey = dedupHash(normalizedTitle + "|veille"); const newDedupKey = dedupHash(normalizedTitle + "|veille");
try {
await db.update(veilleItems) await db.update(veilleItems)
.set({ .set({
categorie: newCategorie, categorie: newCategorie,
@@ -514,6 +515,15 @@ export async function migrateExistingItems(): Promise<MigrationSummary> {
}) })
.where(eq(veilleItems.id, row.id)); .where(eq(veilleItems.id, row.id));
veilleUpdated++; veilleUpdated++;
} catch (e: any) {
// Si le nouveau dedupKey existe déjà → cet article est un doublon, le supprimer
if (e?.code === "ER_DUP_ENTRY" || e?.cause?.code === "ER_DUP_ENTRY" || e?.cause?.message?.includes("Duplicate entry")) {
await db.delete(veilleItems).where(eq(veilleItems.id, row.id));
veilleMerged++;
} else {
throw e;
}
}
} else { } else {
// Groupe : fusionner en gardant le premier, supprimer les autres // Groupe : fusionner en gardant le premier, supprimer les autres
const sorted = group.sort((a: (typeof veilleRows)[number], b: (typeof veilleRows)[number]) => a.id - b.id); const sorted = group.sort((a: (typeof veilleRows)[number], b: (typeof veilleRows)[number]) => a.id - b.id);
@@ -575,6 +585,7 @@ export async function migrateExistingItems(): Promise<MigrationSummary> {
const normalizedTitle = buildMergeKey(row.titre || ""); const normalizedTitle = buildMergeKey(row.titre || "");
const newDedupKey = dedupHash(normalizedTitle + "|aap"); const newDedupKey = dedupHash(normalizedTitle + "|aap");
try {
await db.update(aapItems) await db.update(aapItems)
.set({ .set({
region: newRegion, region: newRegion,
@@ -585,6 +596,15 @@ export async function migrateExistingItems(): Promise<MigrationSummary> {
}) })
.where(eq(aapItems.id, row.id)); .where(eq(aapItems.id, row.id));
aapUpdated++; aapUpdated++;
} catch (e: any) {
// Si le nouveau dedupKey existe déjà → cet article est un doublon, le supprimer
if (e?.code === "ER_DUP_ENTRY" || e?.cause?.code === "ER_DUP_ENTRY" || e?.cause?.message?.includes("Duplicate entry")) {
await db.delete(aapItems).where(eq(aapItems.id, row.id));
aapMerged++;
} else {
throw e;
}
}
} else { } else {
// Fusionner // Fusionner
const sorted = group.sort((a: (typeof aapRows)[number], b: (typeof aapRows)[number]) => a.id - b.id); const sorted = group.sort((a: (typeof aapRows)[number], b: (typeof aapRows)[number]) => a.id - b.id);