From 44c34dbae64c04594408d1e56031889a74c8aa3c Mon Sep 17 00:00:00 2001 From: openhands Date: Sun, 2 Aug 2026 19:57:21 +0200 Subject: [PATCH] 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. --- src/lib/services/catalog-repair.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/lib/services/catalog-repair.ts b/src/lib/services/catalog-repair.ts index 70241615..916947a2 100644 --- a/src/lib/services/catalog-repair.ts +++ b/src/lib/services/catalog-repair.ts @@ -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. */