You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mail-yubao/scripts/backfill-import-log-meta.ts

57 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 回填导入日志的 import_kind,并清理错误的 "[object Object]" external_id。
* Usage: pnpm exec tsx scripts/backfill-import-log-meta.ts
*/
import { prisma } from "../src/services/db";
import {
inferImportKindFromObjectKey,
normalizeExternalId,
} from "../src/services/import/import-log-meta";
async function main() {
const rows = await prisma.containerImport.findMany({
select: {
id: true,
containerNo: true,
importKind: true,
externalId: true,
},
});
let kindUpdated = 0;
let externalFixed = 0;
for (const row of rows) {
const kind = row.importKind || inferImportKindFromObjectKey(row.containerNo);
const externalId = normalizeExternalId(row.externalId);
const needKind = !row.importKind;
const needExt =
row.externalId === "[object Object]" ||
(row.externalId != null && externalId !== row.externalId);
if (!needKind && !needExt) continue;
await prisma.containerImport.update({
where: { id: row.id },
data: {
...(needKind ? { importKind: kind } : {}),
...(needExt ? { externalId } : {}),
},
});
if (needKind) kindUpdated += 1;
if (needExt) externalFixed += 1;
}
console.log(
JSON.stringify(
{ scanned: rows.length, kindUpdated, externalFixed },
null,
2,
),
);
}
main()
.catch((err) => {
console.error(err);
process.exitCode = 1;
})
.finally(async () => {
await prisma.$disconnect();
});