Files
Tom Moor f95761dddf perf: Use FOR NO KEY UPDATE where locked rows are only updated (#14559)
* perf: Use FOR NO KEY UPDATE where locked rows are only updated

Rows that other tables reference by foreign key are now locked with
FOR NO KEY UPDATE when the transaction only updates or soft-deletes
them, so concurrent inserts that reference the row are not blocked.

* test: Collaborative save does not wait for foreign key inserts
2026-11-12 18:54:37 -05:00

50 lines
1.4 KiB
TypeScript

import teamUpdater from "@server/commands/teamUpdater";
import { createContext } from "@server/context";
import { Team, User } from "@server/models";
import { sequelize } from "@server/storage/database";
import type { Event as TEvent, CollectionEvent } from "@server/types";
import DetachDraftsFromCollectionTask from "../tasks/DetachDraftsFromCollectionTask";
import BaseProcessor from "./BaseProcessor";
export default class CollectionsProcessor extends BaseProcessor {
static applicableEvents: TEvent["name"][] = [
"collections.delete",
"collections.archive",
];
async perform(event: CollectionEvent) {
await new DetachDraftsFromCollectionTask().schedule({
collectionId: event.collectionId,
actorId: event.actorId,
ip: event.ip,
});
await sequelize.transaction(async (transaction) => {
const team = await Team.findByPk(event.teamId, {
transaction,
lock: transaction.LOCK.NO_KEY_UPDATE,
});
if (team?.defaultCollectionId === event.collectionId) {
const user = await User.findByPk(event.actorId, {
rejectOnEmpty: true,
paranoid: false,
transaction,
});
const ctx = createContext({
user,
transaction,
ip: event.ip,
});
await teamUpdater(ctx, {
params: { defaultCollectionId: null },
user,
team,
});
}
});
}
}