fix: gérer les violations UNIQUE dans migrateExistingItems (supprimer les doublons)
This commit is contained in:
@@ -503,7 +503,8 @@ 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");
|
||||||
|
|
||||||
await db.update(veilleItems)
|
try {
|
||||||
|
await db.update(veilleItems)
|
||||||
.set({
|
.set({
|
||||||
categorie: newCategorie,
|
categorie: newCategorie,
|
||||||
niveau: newNiveau,
|
niveau: newNiveau,
|
||||||
@@ -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,7 +585,8 @@ 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");
|
||||||
|
|
||||||
await db.update(aapItems)
|
try {
|
||||||
|
await db.update(aapItems)
|
||||||
.set({
|
.set({
|
||||||
region: newRegion,
|
region: newRegion,
|
||||||
departement: newDept,
|
departement: newDept,
|
||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user