mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
Remove forUpdate() call in stats
This commit is contained in:
parent
370deac1af
commit
b88f532e4c
1 changed files with 12 additions and 6 deletions
|
|
@ -302,23 +302,26 @@ export async function updateAuthorStats(
|
||||||
search: '',
|
search: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Try to update first - this is more efficient under high concurrency
|
||||||
const prev = await kysely
|
const prev = await kysely
|
||||||
.selectFrom('author_stats')
|
.selectFrom('author_stats')
|
||||||
.selectAll()
|
.selectAll()
|
||||||
.forUpdate()
|
|
||||||
.where('pubkey', '=', pubkey)
|
.where('pubkey', '=', pubkey)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
|
|
||||||
const stats = fn(prev ?? empty);
|
|
||||||
|
|
||||||
if (prev) {
|
if (prev) {
|
||||||
|
// Row exists, update it
|
||||||
|
const stats = fn(prev);
|
||||||
await kysely.updateTable('author_stats')
|
await kysely.updateTable('author_stats')
|
||||||
.set(stats)
|
.set(stats)
|
||||||
.where('pubkey', '=', pubkey)
|
.where('pubkey', '=', pubkey)
|
||||||
.execute();
|
.execute();
|
||||||
} else {
|
} else {
|
||||||
|
// Row doesn't exist, insert it
|
||||||
|
const stats = fn(empty);
|
||||||
await kysely.insertInto('author_stats')
|
await kysely.insertInto('author_stats')
|
||||||
.values({ ...empty, ...stats })
|
.values({ ...empty, ...stats })
|
||||||
|
.onConflict((oc) => oc.column('pubkey').doUpdateSet(stats))
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -352,23 +355,26 @@ export async function updateEventStats(
|
||||||
reactions: '{}',
|
reactions: '{}',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Try to update first - this is more efficient under high concurrency
|
||||||
const prev = await kysely
|
const prev = await kysely
|
||||||
.selectFrom('event_stats')
|
.selectFrom('event_stats')
|
||||||
.selectAll()
|
.selectAll()
|
||||||
.forUpdate()
|
|
||||||
.where('event_id', '=', eventId)
|
.where('event_id', '=', eventId)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
|
|
||||||
const stats = fn(prev ?? empty);
|
|
||||||
|
|
||||||
if (prev) {
|
if (prev) {
|
||||||
|
// Row exists, update it
|
||||||
|
const stats = fn(prev);
|
||||||
await kysely.updateTable('event_stats')
|
await kysely.updateTable('event_stats')
|
||||||
.set(stats)
|
.set(stats)
|
||||||
.where('event_id', '=', eventId)
|
.where('event_id', '=', eventId)
|
||||||
.execute();
|
.execute();
|
||||||
} else {
|
} else {
|
||||||
|
// Row doesn't exist, insert it
|
||||||
|
const stats = fn(empty);
|
||||||
await kysely.insertInto('event_stats')
|
await kysely.insertInto('event_stats')
|
||||||
.values({ ...empty, ...stats })
|
.values({ ...empty, ...stats })
|
||||||
|
.onConflict((oc) => oc.column('event_id').doUpdateSet(stats))
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue