Compare commits

...
Author SHA1 Message Date
Félix Malfait 54d50768d8 fix(messaging): split thread create/update statements and stop clobbering accumulator
Fixes two bugs in MessagingMessageService.saveMessagesWithinTransaction.

1. Postgres "ON CONFLICT DO UPDATE command cannot affect row a second
   time" during message import: creates and subject updates were merged
   into a single bulk upsert keyed on id, so when the same thread id
   appeared in both lists Postgres rejected the statement. Issue them as
   two separate statements instead — insert for creates, upsert for
   subject updates — which is also closer to the pre-#19351 behavior.

2. enrichMessageAccumulatorWithExistingMessageChannelMessageAssociations
   replaced the accumulator object wholesale, dropping any
   existingThreadInDB previously set by
   enrichMessageAccumulatorWithExistingMessageThreadIds. Mutate the
   existing accumulator instead.
2026-04-07 13:27:32 +02:00
@@ -231,16 +231,18 @@ export class MessagingMessageService {
}
}
const threadsToUpsert = [
...messageThreadsToCreate,
...Array.from(threadSubjectUpdates.entries()).map(
([id, { subject }]) => ({ id, subject }),
),
];
if (messageThreadsToCreate.length > 0) {
await messageThreadRepository.insert(
messageThreadsToCreate,
transactionManager,
);
}
if (threadsToUpsert.length > 0) {
if (threadSubjectUpdates.size > 0) {
await messageThreadRepository.upsert(
threadsToUpsert,
Array.from(threadSubjectUpdates.entries()).map(
([id, { subject }]) => ({ id, subject }),
),
['id'],
transactionManager,
);
@@ -433,11 +435,8 @@ export class MessagingMessageService {
);
if (existingMessageChannelMessageAssociation) {
messageAccumulatorMap.set(message.externalId, {
existingMessageInDB: existingMessage,
existingMessageChannelMessageAssociationInDB:
existingMessageChannelMessageAssociation,
});
messageAccumulator.existingMessageChannelMessageAssociationInDB =
existingMessageChannelMessageAssociation;
}
}
}