fix: 迁移逻辑独立执行,不依赖 schema/seed 成功
问题:runMigrations 在 try 块内,如果 seed 失败则迁移不执行 修复:将迁移逻辑移到独立的 try-catch 块,确保始终执行 添加详细日志帮助调试
This commit is contained in:
@@ -12,19 +12,27 @@ async function columnExists(conn: mysql.Connection, table: string, column: strin
|
||||
}
|
||||
|
||||
async function runMigrations(conn: mysql.Connection) {
|
||||
console.log('[DB] Checking migrations...')
|
||||
|
||||
// transactions 表添加 user_id
|
||||
if (!await columnExists(conn, 'transactions', 'user_id')) {
|
||||
const hasTxUserId = await columnExists(conn, 'transactions', 'user_id')
|
||||
console.log(`[DB] transactions.user_id exists: ${hasTxUserId}`)
|
||||
if (!hasTxUserId) {
|
||||
console.log('[DB] Migrating: adding user_id to transactions')
|
||||
await conn.query('ALTER TABLE transactions ADD COLUMN user_id INT NOT NULL DEFAULT 1 AFTER id')
|
||||
await conn.query('ALTER TABLE transactions ADD INDEX idx_user_date (user_id, date)')
|
||||
await conn.query('ALTER TABLE transactions ADD INDEX idx_user_type_date (user_id, type, date)')
|
||||
console.log('[DB] transactions.user_id added')
|
||||
}
|
||||
|
||||
// budgets 表添加 user_id
|
||||
if (!await columnExists(conn, 'budgets', 'user_id')) {
|
||||
const hasBudgetUserId = await columnExists(conn, 'budgets', 'user_id')
|
||||
console.log(`[DB] budgets.user_id exists: ${hasBudgetUserId}`)
|
||||
if (!hasBudgetUserId) {
|
||||
console.log('[DB] Migrating: adding user_id to budgets')
|
||||
await conn.query('ALTER TABLE budgets ADD COLUMN user_id INT NOT NULL DEFAULT 1 AFTER id')
|
||||
await conn.query('ALTER TABLE budgets ADD UNIQUE KEY uk_user_month (user_id, month)')
|
||||
console.log('[DB] budgets.user_id added')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +58,7 @@ export async function initDatabase() {
|
||||
database: dbName,
|
||||
multipleStatements: true,
|
||||
})
|
||||
|
||||
try {
|
||||
const schemaPath = join(__dirname, 'schema.sql')
|
||||
const seedPath = join(__dirname, 'seed.sql')
|
||||
@@ -63,18 +72,23 @@ export async function initDatabase() {
|
||||
const seed = readFileSync(seedPath, 'utf-8')
|
||||
await initConn.query(seed)
|
||||
|
||||
// 运行迁移(添加缺失列)
|
||||
await runMigrations(initConn)
|
||||
|
||||
console.log('[DB] Database initialized')
|
||||
console.log('[DB] Schema and seed applied')
|
||||
} catch (err: any) {
|
||||
if (err.code === 'ER_DUP_ENTRY' || err.errno === 1062) {
|
||||
console.log('[DB] Already initialized, skipping seed')
|
||||
await runMigrations(initConn)
|
||||
} else {
|
||||
console.error('[DB] Init failed:', err.message)
|
||||
console.error('[DB] Init warning:', err.message)
|
||||
}
|
||||
} finally {
|
||||
}
|
||||
|
||||
// 始终运行迁移(独立于 schema/seed)
|
||||
try {
|
||||
await runMigrations(initConn)
|
||||
console.log('[DB] Migrations complete')
|
||||
} catch (err: any) {
|
||||
console.error('[DB] Migration error:', err.message)
|
||||
}
|
||||
|
||||
await initConn.end()
|
||||
}
|
||||
console.log('[DB] Database ready')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user