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. */