refactor(threads): dedup maintenance threads by stable key via index
This commit is contained in:
@@ -387,19 +387,20 @@ export const findOpenMaintenanceThread = internalQuery({
|
||||
args: {
|
||||
spoonId: v.id('spoons'),
|
||||
ownerId: v.id('users'),
|
||||
upstreamTo: v.string(),
|
||||
dedupKey: v.string(),
|
||||
},
|
||||
handler: async (ctx, { spoonId, ownerId, upstreamTo }) => {
|
||||
handler: async (ctx, { spoonId, ownerId, dedupKey }) => {
|
||||
const threads = await ctx.db
|
||||
.query('threads')
|
||||
.withIndex('by_spoon', (q) => q.eq('spoonId', spoonId))
|
||||
.withIndex('by_spoon_dedup', (q) =>
|
||||
q.eq('spoonId', spoonId).eq('dedupKey', dedupKey),
|
||||
)
|
||||
.order('desc')
|
||||
.collect();
|
||||
return (
|
||||
threads.find(
|
||||
(thread) =>
|
||||
thread.ownerId === ownerId &&
|
||||
thread.upstreamTo === upstreamTo &&
|
||||
!['resolved', 'ignored', 'failed', 'cancelled'].includes(
|
||||
thread.status,
|
||||
),
|
||||
@@ -416,7 +417,8 @@ export const createMaintenanceThread = internalMutation({
|
||||
title: v.string(),
|
||||
summary: v.string(),
|
||||
upstreamFrom: v.optional(v.string()),
|
||||
upstreamTo: v.string(),
|
||||
upstreamTo: v.optional(v.string()),
|
||||
dedupKey: v.string(),
|
||||
forkHeadAtCreation: v.optional(v.string()),
|
||||
mergeBaseAtCreation: v.optional(v.string()),
|
||||
relatedSyncRunId: v.optional(v.id('syncRuns')),
|
||||
@@ -429,14 +431,15 @@ export const createMaintenanceThread = internalMutation({
|
||||
const now = Date.now();
|
||||
const existing = await ctx.db
|
||||
.query('threads')
|
||||
.withIndex('by_spoon', (q) => q.eq('spoonId', args.spoonId))
|
||||
.withIndex('by_spoon_dedup', (q) =>
|
||||
q.eq('spoonId', args.spoonId).eq('dedupKey', args.dedupKey),
|
||||
)
|
||||
.order('desc')
|
||||
.collect()
|
||||
.then((threads) =>
|
||||
threads.find(
|
||||
(thread) =>
|
||||
thread.ownerId === args.ownerId &&
|
||||
thread.upstreamTo === args.upstreamTo &&
|
||||
!['resolved', 'ignored', 'failed', 'cancelled'].includes(
|
||||
thread.status,
|
||||
),
|
||||
@@ -470,6 +473,7 @@ export const createMaintenanceThread = internalMutation({
|
||||
priority: args.source === 'merge_conflict' ? 'high' : 'normal',
|
||||
upstreamFrom: args.upstreamFrom,
|
||||
upstreamTo: args.upstreamTo,
|
||||
dedupKey: args.dedupKey,
|
||||
forkHeadAtCreation: args.forkHeadAtCreation,
|
||||
mergeBaseAtCreation: args.mergeBaseAtCreation,
|
||||
relatedSyncRunId: args.relatedSyncRunId,
|
||||
|
||||
Reference in New Issue
Block a user