Skip to content

Commit

Permalink
Improve indexing for chat.bsky.moderation.getMessageContext (#3520)
Browse files Browse the repository at this point in the history
* Improve indexing for chat.bsky.moderation.getMessageContext

* tidy
  • Loading branch information
matthieusieben authored Feb 14, 2025
1 parent eb83ae1 commit 20e57ba
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-buttons-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto/ozone": patch
---

Improve indexing for "chat.bsky.moderation.getMessageContext"
2 changes: 2 additions & 0 deletions packages/ozone/src/api/chat/getMessageContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 1 addition & 0 deletions packages/ozone/src/db/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 20e57ba

Please sign in to comment.