mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-05-03 03:31:18 +08:00
feat: 旧数据库兼容
This commit is contained in:
@@ -78,9 +78,31 @@ function getAiDb(): Database.Database {
|
||||
CREATE INDEX IF NOT EXISTS idx_ai_message_conversation ON ai_message(conversation_id);
|
||||
`)
|
||||
|
||||
// 数据库迁移:为旧数据库添加缺失的列
|
||||
migrateAiDatabase(AI_DB)
|
||||
|
||||
return AI_DB
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库迁移:检查并添加缺失的列
|
||||
*/
|
||||
function migrateAiDatabase(db: Database.Database): void {
|
||||
try {
|
||||
// 获取 ai_message 表的列信息
|
||||
const tableInfo = db.pragma('table_info(ai_message)') as Array<{ name: string }>
|
||||
const columnNames = tableInfo.map((col) => col.name)
|
||||
|
||||
// 检查并添加 content_blocks 列
|
||||
if (!columnNames.includes('content_blocks')) {
|
||||
db.exec('ALTER TABLE ai_message ADD COLUMN content_blocks TEXT')
|
||||
console.log('[AI DB Migration] 添加 content_blocks 列')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[AI DB Migration] 迁移失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭 AI 数据库连接
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user