From 38edbc387ba0c7d01b8c7debac6b61f8003283c4 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 18 Sep 2024 23:50:00 +0200 Subject: [PATCH] events-backend: refactor db query to clarify precedence Signed-off-by: Patrik Oldsberg --- .../src/service/hub/DatabaseEventBusStore.ts | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/plugins/events-backend/src/service/hub/DatabaseEventBusStore.ts b/plugins/events-backend/src/service/hub/DatabaseEventBusStore.ts index c51179896b..3a6a98d919 100644 --- a/plugins/events-backend/src/service/hub/DatabaseEventBusStore.ts +++ b/plugins/events-backend/src/service/hub/DatabaseEventBusStore.ts @@ -553,15 +553,22 @@ export class DatabaseEventBusStore implements EventBusStore { const eventCount = await this.#db(TABLE_EVENTS) .delete() // Delete any events that are outside both the min age and size window - .whereIn( - 'id', - this.#db - .select('id') - .from(TABLE_EVENTS) - .orderBy('id', 'desc') - .offset(this.#windowMaxCount), + .orWhere(inner => + inner + .whereIn( + 'id', + this.#db + .select('id') + .from(TABLE_EVENTS) + .orderBy('id', 'desc') + .offset(this.#windowMaxCount), + ) + .andWhere( + 'created_at', + '<', + new Date(Date.now() - this.#windowMinAge), + ), ) - .andWhere('created_at', '<', new Date(Date.now() - this.#windowMinAge)) // If events are outside the max age they will always be deleted .orWhere('created_at', '<', new Date(Date.now() - this.#windowMaxAge));