Skip to content

Commit 5f9e571

Browse files
committed
Add messages table for AI (bot + TMA)
Create messages table in runSchemaMigrations with id, created_at, user_telegram (FK to users), thread_id, type (bot|app), role (user|assistant|system), content, telegram_update_id (nullable). Add index for thread history (user_telegram, thread_id, type, created_at) and partial unique index for bot deduplication (user_telegram, thread_id, type, telegram_update_id) where telegram_update_id IS NOT NULL. Implements plan in app/database/plan.md. Made-with: Cursor
1 parent d3b223c commit 5f9e571

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

app/database/start.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,31 @@ async function runSchemaMigrations() {
7979
CREATE INDEX IF NOT EXISTS idx_pending_tx_status
8080
ON pending_transactions(status);
8181
`;
82+
83+
// messages table (AI: bot + TMA)
84+
await sql`
85+
CREATE TABLE IF NOT EXISTS messages (
86+
id BIGSERIAL PRIMARY KEY,
87+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
88+
user_telegram TEXT NOT NULL REFERENCES users(telegram_username),
89+
thread_id BIGINT NOT NULL,
90+
type TEXT NOT NULL CHECK (type IN ('bot', 'app')),
91+
role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'system')),
92+
content TEXT,
93+
telegram_update_id BIGINT
94+
);
95+
`;
96+
97+
await sql`
98+
CREATE INDEX IF NOT EXISTS idx_messages_thread
99+
ON messages(user_telegram, thread_id, type, created_at);
100+
`;
101+
102+
await sql`
103+
CREATE UNIQUE INDEX IF NOT EXISTS idx_messages_bot_update_id
104+
ON messages(user_telegram, thread_id, type, telegram_update_id)
105+
WHERE telegram_update_id IS NOT NULL;
106+
`;
82107
}
83108

84109
let schemaInitPromise: Promise<void> | null = null;

0 commit comments

Comments
 (0)