33 lines
1.0 KiB
TypeScript
33 lines
1.0 KiB
TypeScript
/**
|
|
* 从旧库直连读取数据,返回与 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<LegacyRows> {
|
|
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<string, unknown>[]).map((row) => {
|
|
const obj: Record<string, SqlValue> = {};
|
|
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();
|
|
}
|
|
}
|
|
|