From b34c397b8e88bebb11eca7193c769ae31b68ccef Mon Sep 17 00:00:00 2001 From: Leohearts Date: Wed, 26 Mar 2025 17:10:54 +0800 Subject: [PATCH] fix, feat(telegram): fix typo, make chat_id unique (#99) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📝 doc(db): add database schema changes for chat_id - update the schema to enforce unique constraints on chat_id - added onConflictDoUpdate to handle duplicate entries * 🔧 chore(telegram-bot): Rename embedding dimension variable --- services/telegram-bot/.env | 2 +- services/telegram-bot/drizzle/0000_harsh_king_cobra.sql | 2 +- services/telegram-bot/src/db/schema.ts | 2 +- services/telegram-bot/src/models/chats.ts | 8 +++++++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/services/telegram-bot/.env b/services/telegram-bot/.env index b91bba627..810bbb5d8 100644 --- a/services/telegram-bot/.env +++ b/services/telegram-bot/.env @@ -13,6 +13,6 @@ LLM_VISION_MODEL='' EMBEDDING_API_BASE_URL='' EMBEDDING_API_KEY='' EMBEDDING_MODEL='' -EMBEDDING_DIMENSIONS='' +EMBEDDING_DIMENSION='' ADMIN_USER_IDS='' diff --git a/services/telegram-bot/drizzle/0000_harsh_king_cobra.sql b/services/telegram-bot/drizzle/0000_harsh_king_cobra.sql index ece50481a..3510765ac 100644 --- a/services/telegram-bot/drizzle/0000_harsh_king_cobra.sql +++ b/services/telegram-bot/drizzle/0000_harsh_king_cobra.sql @@ -19,7 +19,7 @@ CREATE TABLE "chat_messages" ( CREATE TABLE "joined_chats" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "platform" text DEFAULT '' NOT NULL, - "chat_id" text DEFAULT '' NOT NULL, + "chat_id" text DEFAULT '' UNIQUE NOT NULL, "chat_name" text DEFAULT '' NOT NULL, "created_at" bigint DEFAULT 0 NOT NULL, "updated_at" bigint DEFAULT 0 NOT NULL diff --git a/services/telegram-bot/src/db/schema.ts b/services/telegram-bot/src/db/schema.ts index 902505c3c..a631f9308 100644 --- a/services/telegram-bot/src/db/schema.ts +++ b/services/telegram-bot/src/db/schema.ts @@ -60,7 +60,7 @@ export const joinedChatsTable = pgTable('joined_chats', () => { return { id: uuid().primaryKey().defaultRandom(), platform: text().notNull().default(''), - chat_id: text().notNull().default(''), + chat_id: text().notNull().default('').unique(), chat_name: text().notNull().default(''), created_at: bigint({ mode: 'number' }).notNull().default(0).$defaultFn(() => Date.now()), updated_at: bigint({ mode: 'number' }).notNull().default(0).$defaultFn(() => Date.now()), diff --git a/services/telegram-bot/src/models/chats.ts b/services/telegram-bot/src/models/chats.ts index fd6302912..9d79f32f9 100644 --- a/services/telegram-bot/src/models/chats.ts +++ b/services/telegram-bot/src/models/chats.ts @@ -19,5 +19,11 @@ export async function recordJoinedChat(chatId: string, chatName: string) { chat_id: chatId, chat_name: chatName, }) - .onConflictDoNothing() + .onConflictDoUpdate({ + target: joinedChatsTable.chat_id, + set: { + chat_name: chatName, + updated_at: Date.now(), + }, + }) }