From 4da6bd5b952a8555502d94e01c799abb52976120 Mon Sep 17 00:00:00 2001 From: Hailey Date: Mon, 1 Sep 2025 10:07:50 -0700 Subject: [PATCH] use single row for cursor --- bskylink/src/cache/safelinkClient.ts | 8 +++++--- bskylink/src/db/migrations/002-safelink.ts | 4 ++-- bskylink/src/db/schema.ts | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/bskylink/src/cache/safelinkClient.ts b/bskylink/src/cache/safelinkClient.ts index 7b015316c1..a5111ebeea 100644 --- a/bskylink/src/cache/safelinkClient.ts +++ b/bskylink/src/cache/safelinkClient.ts @@ -227,8 +227,7 @@ export class SafelinkClient { const res = await this.db.db .selectFrom('safelink_cursor') .selectAll() - .orderBy('createdAt', 'desc') - .limit(1) + .where('id', '=', 1) .executeTakeFirst() if (!res) { return '' @@ -239,13 +238,16 @@ export class SafelinkClient { } private async setCursor(cursor: string) { + const updatedAt = new Date() try { await this.db.db .insertInto('safelink_cursor') .values({ + id: 1, cursor, - createdAt: new Date(), + updatedAt, }) + .onConflict(oc => oc.column('id').doUpdateSet({cursor, updatedAt})) .execute() this.cursor = cursor } catch (err) { diff --git a/bskylink/src/db/migrations/002-safelink.ts b/bskylink/src/db/migrations/002-safelink.ts index 023200a136..e7dda61e5a 100644 --- a/bskylink/src/db/migrations/002-safelink.ts +++ b/bskylink/src/db/migrations/002-safelink.ts @@ -16,9 +16,9 @@ export async function up(db: Kysely): Promise { await db.schema .createTable('safelink_cursor') - .addColumn('id', 'bigserial', col => col.primaryKey()) + .addColumn('id', 'bigserial', col => col.notNull()) .addColumn('cursor', 'varchar', col => col.notNull()) - .addColumn('createdAt', 'timestamptz', col => col.notNull()) + .addColumn('updatedAt', 'timestamptz', col => col.notNull()) .execute() await db.schema diff --git a/bskylink/src/db/schema.ts b/bskylink/src/db/schema.ts index 08412a105c..edd070d200 100644 --- a/bskylink/src/db/schema.ts +++ b/bskylink/src/db/schema.ts @@ -1,4 +1,4 @@ -import {type GeneratedAlways, type Selectable} from 'kysely' +import {type Selectable} from 'kysely' export type DbSchema = { link: Link @@ -43,9 +43,9 @@ export interface SafelinkRule { } export interface SafelinkCursor { - id: GeneratedAlways + id: number cursor: string - createdAt: Date + updatedAt: Date } export type LinkEntry = Selectable