/** * 从旧库直连读取数据,返回与 loadLegacyDump 一致的结构。 * 仅 migrate.ts 的 --from-db 模式使用。 */ import mysql from 'mysql2/promise'; import { LEGACY_COLUMNS, type LegacyTable } from './columns'; import type { LegacyRows } from './load'; import type { SqlValue } from './parser'; export async function loadLegacyDb(url: string): Promise { if (!url) throw new Error('缺少 LEGACY_DATABASE_URL'); const conn = await mysql.createConnection(url); try { const result = {} as LegacyRows; for (const table of Object.keys(LEGACY_COLUMNS) as LegacyTable[]) { const [rows] = await conn.query(`SELECT * FROM \`${table}\``); result[table] = (rows as Record[]).map((row) => { const obj: Record = {}; for (const col of LEGACY_COLUMNS[table]) { const v = row[col]; obj[col] = v === null || v === undefined ? null : (v as SqlValue); } return obj; }); } return result; } finally { await conn.end(); } }