fix: catalog-repair - allocate sequential ids in generateCatalogSql
Previously allocateCatalogItemId was called per entry, but since generation does not INSERT, MAX(id) never advanced and every entry got the same id. Now MAX(id) is read once and a local counter hands out sequential ids.
This commit is contained in:
1 parent
5308ce6a12
commit
44c34dbae6
1 file changed
+12
-8
@@ -6,7 +6,6 @@ import {
|
||||
writeFurniData,
|
||||
} from "@/lib/services/furni-data";
|
||||
import {
|
||||
allocateCatalogItemId,
|
||||
autoPriceFurni,
|
||||
getOrCreateCategoryPage,
|
||||
} from "@/lib/services/furni-import";
|
||||
@@ -43,6 +42,15 @@ export async function generateCatalogSql(
|
||||
"START TRANSACTION;",
|
||||
];
|
||||
|
||||
// The generation pass does not INSERT, so MAX(id) would never advance and
|
||||
// per-entry allocateCatalogItemId would hand out the same id every time.
|
||||
// Read the current max once and hand out sequential ids from there.
|
||||
const [maxIdResult] = (await db.execute(sql`
|
||||
SELECT MAX(id) as maxId FROM catalog_items
|
||||
`)) as unknown as [Array<{ maxId: number | bigint | null }>, unknown];
|
||||
let nextId = Number(maxIdResult[0]?.maxId ?? 0) + 1;
|
||||
|
||||
let generated = 0;
|
||||
for (const entry of entries) {
|
||||
let pageId: number;
|
||||
try {
|
||||
@@ -51,19 +59,15 @@ export async function generateCatalogSql(
|
||||
continue;
|
||||
}
|
||||
const price = autoPriceFurni(entry.classname);
|
||||
let nextId: number;
|
||||
try {
|
||||
nextId = await allocateCatalogItemId(async (id) => id);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
statements.push(
|
||||
`INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata) VALUES (${nextId}, ${pageId}, ${entry.itemId}, '${escSqlLiteral(entry.classname)}', ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, '-1', '');`,
|
||||
);
|
||||
nextId++;
|
||||
generated++;
|
||||
}
|
||||
|
||||
statements.push("COMMIT;");
|
||||
return { statements, sql: statements.join("\n"), count: entries.length };
|
||||
return { statements, sql: statements.join("\n"), count: generated };
|
||||
}
|
||||
|
||||
/** Execute generated INSERT statements directly against the database. */
|
||||
|
||||
Reference in new issue
Block a user