Improve indexing for chat.bsky.moderation.getMessageContext ()

* Improve indexing for chat.bsky.moderation.getMessageContext

* tidy
This commit is contained in:
Matthieu Sieben 2025-02-14 09:44:07 +01:00 committed by GitHub
parent eb83ae176d
commit 20e57bacf9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 0 deletions

@ -0,0 +1,5 @@
---
"@atproto/ozone": patch
---
Improve indexing for "chat.bsky.moderation.getMessageContext"

@ -19,6 +19,8 @@ export default function (server: Server, ctx: AppContext) {
.selectFrom('moderation_event')
.select('id')
.where('subjectMessageId', '=', params.messageId)
// uses "moderation_event_message_id_idx" index
.where('subjectMessageId', 'is not', null)
.where('action', '=', 'tools.ozone.moderation.defs#modEventReport')
.limit(1)
.executeTakeFirst()

@ -0,0 +1,26 @@
import { Kysely, sql } from 'kysely'
// support lookup for chat.bsky.moderation.getMessageContext
export async function up(db: Kysely<unknown>): Promise<void> {
// @NOTE: These queries should be run with the "CONCURRENTLY" option in
// production to avoid locking the table. This is not supported by Kysely.
await db.schema.dropIndex('moderation_event_message_id_index').execute()
await db.schema
.createIndex('moderation_event_message_id_idx')
.on('moderation_event')
// https://github.com/kysely-org/kysely/issues/302
.expression(
sql`"subjectMessageId") WHERE ("subjectMessageId" IS NOT NULL AND "action" = 'tools.ozone.moderation.defs#modEventReport'`,
)
.execute()
}
export async function down(db: Kysely<unknown>): Promise<void> {
await db.schema.dropIndex('moderation_event_message_id_idx').execute()
await db.schema
.createIndex('moderation_event_message_id_index')
.on('moderation_event')
.column('subjectMessageId')
.execute()
}

@ -19,3 +19,4 @@ export * as _20241018T205730722Z from './20241018T205730722Z-setting'
export * as _20241026T205730722Z from './20241026T205730722Z-add-hosting-status-to-subject-status'
export * as _20241220T144630860Z from './20241220T144630860Z-stats-materialized-views'
export * as _20250204T003647759Z from './20250204T003647759Z-add-subject-priority-score'
export * as _20250211T132135150Z from './20250211T132135150Z-moderation-event-message-partial-idx'