issue-ai/scripts/migrate-to-resolved.ts

29 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 数据迁移:将现有所有工单的 current_status 批量更新为 'resolved'(已办工单)
// 运行方式npx tsx scripts/migrate-to-resolved.ts
import { getDb } from '../src/lib/db'
import { initDatabase } from '../src/lib/db-schema'
initDatabase()
const db = getDb()
const before = db.prepare('SELECT COUNT(*) as total FROM tickets').get() as { total: number }
console.log(`当前工单总数: ${before.total}`)
const toUpdate = db.prepare(
"SELECT COUNT(*) as total FROM tickets WHERE current_status != 'resolved'"
).get() as { total: number }
console.log(`需要迁移(非 resolved: ${toUpdate.total}`)
if (toUpdate.total > 0) {
db.prepare("UPDATE tickets SET current_status = 'resolved'").run()
console.log('迁移完成:所有工单已归入已办工单。')
} else {
console.log('无需迁移:所有工单已是 resolved 状态。')
}
const after = db.prepare(
"SELECT current_status, COUNT(*) as cnt FROM tickets GROUP BY current_status"
).all() as { current_status: string; cnt: number }[]
console.log('迁移后状态分布:', after)