四端上传
This commit is contained in:
1 parent
25bbb5f9c8
commit
c6e2cb4d5b
276 files changed
+17812
-3
No files matched your search
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* 从旧库直连读取数据,返回与 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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user