Files
outline/server/queues/processors/UserDeletedProcessor.ts
T
Tom Moor 46fc485a89 perf: Make documents ownerIds index partial (#14776)
Score-only updates on documents are non-HOT, so every update wrote a new
entry into the full GIN index on ownerIds even though almost all rows have
no owners. Index only rows with owners.
2026-11-27 08:49:07 -05:00

127 lines
2.9 KiB
TypeScript

import { Op } from "sequelize";
import {
ApiKey,
Collection,
Document,
GroupUser,
OAuthAuthentication,
Star,
Subscription,
UserAuthentication,
UserMembership,
WebhookSubscription,
} from "@server/models";
import { sequelize } from "@server/storage/database";
import type { Event as TEvent, UserEvent } from "@server/types";
import BaseProcessor from "./BaseProcessor";
export default class UserDeletedProcessor extends BaseProcessor {
static applicableEvents: TEvent["name"][] = ["users.delete"];
async perform(event: UserEvent) {
await sequelize.transaction(async (transaction) => {
await GroupUser.destroy({
where: {
userId: event.userId,
},
transaction,
individualHooks: true,
});
await UserAuthentication.destroy({
where: {
userId: event.userId,
},
transaction,
});
await UserMembership.destroy({
where: {
userId: event.userId,
},
transaction,
});
await Subscription.destroy({
where: {
userId: event.userId,
},
transaction,
});
await ApiKey.destroy({
where: {
userId: event.userId,
},
transaction,
});
await OAuthAuthentication.destroy({
where: {
userId: event.userId,
},
transaction,
});
await Star.destroy({
where: {
userId: event.userId,
},
transaction,
});
// The personal collection becomes an ordinary private collection, so
// that admins can recover the documents in it.
await Collection.update(
{ ownerId: null },
{
where: {
ownerId: event.userId,
},
transaction,
}
);
await Document.unscoped().update(
{
ownerIds: sequelize.fn(
"array_remove",
sequelize.col("ownerIds"),
event.userId
),
},
{
where: {
teamId: event.teamId,
// The non-empty check lets Postgres use the partial owner index.
ownerIds: { [Op.contains]: [event.userId], [Op.ne]: [] },
},
paranoid: false,
silent: true,
transaction,
}
);
await Collection.unscoped().update(
{
ownerIds: sequelize.fn(
"array_remove",
sequelize.col("ownerIds"),
event.userId
),
},
{
where: {
teamId: event.teamId,
ownerIds: { [Op.contains]: [event.userId] },
},
paranoid: false,
silent: true,
transaction,
}
);
await WebhookSubscription.update(
{ enabled: false },
{
where: {
createdById: event.userId,
enabled: true,
},
transaction,
}
);
});
}
}