refactor(telegram): remove auto table creation, add SQL script for ops
Move table DDL to scripts/telegram-users.sql for manual execution by ops. Remove OnModuleInit and ensureTable() from TelegramUserStore. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
6284ffd74a
commit
1547da3279
2 changed files with 18 additions and 35 deletions
17
scripts/telegram-users.sql
Normal file
17
scripts/telegram-users.sql
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
-- Telegram users table for Gateway
|
||||
-- Run this manually before starting the Gateway with Telegram enabled.
|
||||
|
||||
DROP TABLE IF EXISTS telegram_users;
|
||||
|
||||
CREATE TABLE telegram_users (
|
||||
telegram_user_id VARCHAR(64) PRIMARY KEY,
|
||||
hub_id VARCHAR(64) NOT NULL,
|
||||
agent_id VARCHAR(64) NOT NULL,
|
||||
device_id VARCHAR(64) NOT NULL UNIQUE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
telegram_username VARCHAR(255),
|
||||
telegram_first_name VARCHAR(255),
|
||||
telegram_last_name VARCHAR(255),
|
||||
INDEX idx_device_id (device_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
|
@ -3,7 +3,6 @@
|
|||
*/
|
||||
|
||||
import { Inject, Injectable, Logger } from "@nestjs/common";
|
||||
import type { OnModuleInit } from "@nestjs/common";
|
||||
import { v7 as uuidv7 } from "uuid";
|
||||
import type { RowDataPacket } from "mysql2/promise";
|
||||
import { DatabaseService } from "../database/database.service.js";
|
||||
|
|
@ -22,44 +21,11 @@ interface TelegramUserRow extends RowDataPacket {
|
|||
}
|
||||
|
||||
@Injectable()
|
||||
export class TelegramUserStore implements OnModuleInit {
|
||||
export class TelegramUserStore {
|
||||
private readonly logger = new Logger(TelegramUserStore.name);
|
||||
|
||||
constructor(@Inject(DatabaseService) private readonly db: DatabaseService) {}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
console.log("[TelegramUserStore] onModuleInit starting...");
|
||||
if (!this.db.isAvailable()) {
|
||||
console.log("[TelegramUserStore] Database not available");
|
||||
this.logger.warn("Database not available, TelegramUserStore disabled");
|
||||
return;
|
||||
}
|
||||
console.log("[TelegramUserStore] Ensuring table...");
|
||||
await this.ensureTable();
|
||||
console.log("[TelegramUserStore] Done");
|
||||
}
|
||||
|
||||
/** Drop and recreate telegram_users table (schema changed: hub_url -> hub_id + agent_id) */
|
||||
private async ensureTable(): Promise<void> {
|
||||
await this.db.execute("DROP TABLE IF EXISTS telegram_users");
|
||||
const sql = `
|
||||
CREATE TABLE telegram_users (
|
||||
telegram_user_id VARCHAR(64) PRIMARY KEY,
|
||||
hub_id VARCHAR(64) NOT NULL,
|
||||
agent_id VARCHAR(64) NOT NULL,
|
||||
device_id VARCHAR(64) NOT NULL UNIQUE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
telegram_username VARCHAR(255),
|
||||
telegram_first_name VARCHAR(255),
|
||||
telegram_last_name VARCHAR(255),
|
||||
INDEX idx_device_id (device_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||
`;
|
||||
await this.db.execute(sql);
|
||||
this.logger.log("telegram_users table ensured");
|
||||
}
|
||||
|
||||
/** Find user by Telegram user ID */
|
||||
async findByTelegramUserId(telegramUserId: string): Promise<TelegramUser | null> {
|
||||
if (!this.db.isAvailable()) return null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue