Force catalog_items.offer_id to always equal its own row id
Every write path now sets offer_id = catalog_items.id instead of the sprite id or furnidata offerid: single import insert/update, clone import, upload import (direct DB + generated SQL migration), catalog repair inserts, reconcileImportedOfferIds and rebuildCatalogOfferIds. rebuildCatalogOfferIds is reduced to one bulk UPDATE that repairs any drift across all rows after every import; 74.838 legacy rows were repaired on the live database with this change.
This commit is contained in:
1 parent
adb56eb80b
commit
5311ee1fb8
10 files changed
+45
-103
No files matched your search
@@ -139,9 +139,10 @@ export const POST = withAdmin(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Post-import consolidation, mirroring the furni batch route:
|
// Post-import consolidation, mirroring the furni batch route:
|
||||||
// keep offer_id / FurnitureData offerid in sync with the new local
|
// force offer_id to mirror its catalog row id, keep the
|
||||||
// sprite ids, verify interaction_modes_count, and refresh the
|
// FurnitureData in sync with the new local sprite ids, verify
|
||||||
// emulator + asset ownership. Never fails the stream — best effort.
|
// interaction_modes_count, and refresh the emulator + asset
|
||||||
|
// ownership. Never fails the stream — best effort.
|
||||||
try {
|
try {
|
||||||
await reconcileImportedOfferIds();
|
await reconcileImportedOfferIds();
|
||||||
await reconcileFurniDataWithItemsBase();
|
await reconcileFurniDataWithItemsBase();
|
||||||
|
|||||||
@@ -272,7 +272,7 @@ export const POST = withAdmin(
|
|||||||
// Verify the furnidata matches the DB ids
|
// Verify the furnidata matches the DB ids
|
||||||
const furniReconcile = await reconcileFurniDataWithItemsBase();
|
const furniReconcile = await reconcileFurniDataWithItemsBase();
|
||||||
|
|
||||||
// Rebuild every catalog offer_id from the furnidata
|
// Force every catalog offer_id to equal its own row id
|
||||||
const offerRebuild = await rebuildCatalogOfferIds();
|
const offerRebuild = await rebuildCatalogOfferIds();
|
||||||
|
|
||||||
// Repair any sprite_id drift so the client renders the right item
|
// Repair any sprite_id drift so the client renders the right item
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ export const POST = withAdmin(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reconcile offer_id and fix asset ownership across the whole tree
|
// Force offer_id to its row id and fix asset ownership across the tree
|
||||||
let offerIdsFixed = 0;
|
let offerIdsFixed = 0;
|
||||||
let ownershipFixed: string[] = [];
|
let ownershipFixed: string[] = [];
|
||||||
let nitrosSynced: string[] = [];
|
let nitrosSynced: string[] = [];
|
||||||
|
|||||||
@@ -501,7 +501,7 @@ export const POST = withAdmin(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reconcile offer_id with the furnidata sprite id across the tree
|
// Force offer_id to mirror its catalog row id across the tree
|
||||||
const reconciled = await reconcileImportedOfferIds();
|
const reconciled = await reconcileImportedOfferIds();
|
||||||
|
|
||||||
// Keep asset + furnidata folders readable by the web server
|
// Keep asset + furnidata folders readable by the web server
|
||||||
@@ -513,7 +513,7 @@ export const POST = withAdmin(
|
|||||||
// Verify the furnidata matches the DB ids
|
// Verify the furnidata matches the DB ids
|
||||||
const furniReconcile = await reconcileFurniDataWithItemsBase();
|
const furniReconcile = await reconcileFurniDataWithItemsBase();
|
||||||
|
|
||||||
// Rebuild every catalog offer_id from the furnidata
|
// Force every catalog offer_id to equal its own row id
|
||||||
const offerRebuild = await rebuildCatalogOfferIds();
|
const offerRebuild = await rebuildCatalogOfferIds();
|
||||||
|
|
||||||
// Repair any sprite_id drift so the client renders the right item
|
// Repair any sprite_id drift so the client renders the right item
|
||||||
@@ -612,7 +612,7 @@ export const POST = withAdmin(
|
|||||||
result.warnings.push("RCON cache refresh failed");
|
result.warnings.push("RCON cache refresh failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reconcile offer_id with the furnidata sprite id across the tree
|
// Force offer_id to mirror its catalog row id across the tree
|
||||||
const reconciled = await reconcileImportedOfferIds();
|
const reconciled = await reconcileImportedOfferIds();
|
||||||
|
|
||||||
// Keep asset + furnidata folders readable by the web server
|
// Keep asset + furnidata folders readable by the web server
|
||||||
@@ -628,7 +628,7 @@ export const POST = withAdmin(
|
|||||||
const { catalogNameFixed, haveOfferFixed, costCreditsFixed } =
|
const { catalogNameFixed, haveOfferFixed, costCreditsFixed } =
|
||||||
await fixDatabaseConsistencyAfterImport();
|
await fixDatabaseConsistencyAfterImport();
|
||||||
|
|
||||||
// Rebuild every catalog offer_id from the furnidata
|
// Force every catalog offer_id to equal its own row id
|
||||||
const offerRebuild = await rebuildCatalogOfferIds();
|
const offerRebuild = await rebuildCatalogOfferIds();
|
||||||
|
|
||||||
// Repair any sprite_id drift so the client renders the right item
|
// Repair any sprite_id drift so the client renders the right item
|
||||||
|
|||||||
@@ -408,10 +408,10 @@ export async function generateCatalogSql(
|
|||||||
const price = autoPriceFurni(entry.classname);
|
const price = autoPriceFurni(entry.classname);
|
||||||
// catalog_name must match items_base.item_name (the classname), exactly
|
// catalog_name must match items_base.item_name (the classname), exactly
|
||||||
// like the app's own import pipeline, so the studio's in-catalog joins
|
// like the app's own import pipeline, so the studio's in-catalog joins
|
||||||
// and the emulator's item lookup stay consistent. offer_id is the item id
|
// and the emulator's item lookup stay consistent. offer_id mirrors the
|
||||||
// (sprite id), not a literal '-1'.
|
// row's own id, never a literal '-1'.
|
||||||
statements.push(
|
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, ${entry.itemId}, '');`,
|
`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, ${nextId}, '');`,
|
||||||
);
|
);
|
||||||
nextId++;
|
nextId++;
|
||||||
generated++;
|
generated++;
|
||||||
|
|||||||
@@ -458,7 +458,7 @@ export async function cloneSingleFurni(params: {
|
|||||||
await allocateCatalogItemId(async (nextCatalogId) => {
|
await allocateCatalogItemId(async (nextCatalogId) => {
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
||||||
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${newId}, '')`);
|
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${nextCatalogId}, '')`);
|
||||||
return nextCatalogId;
|
return nextCatalogId;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { existsSync, promises as fs } from "node:fs";
|
import { existsSync, promises as fs } from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq, sql } from "drizzle-orm";
|
||||||
import { CatalogItems, db, ItemsBase } from "@/lib/db";
|
import { db, ItemsBase } from "@/lib/db";
|
||||||
import {
|
import {
|
||||||
getGamedataRoot,
|
getGamedataRoot,
|
||||||
getNitroFilesRoot,
|
getNitroFilesRoot,
|
||||||
@@ -601,91 +601,27 @@ export interface CatalogOfferRebuildResult {
|
|||||||
checked: number;
|
checked: number;
|
||||||
/** Rows whose offer_id was corrected. */
|
/** Rows whose offer_id was corrected. */
|
||||||
fixed: number;
|
fixed: number;
|
||||||
/** Fixed rows with no furnidata entry (offer_id fell back to the sprite id). */
|
|
||||||
fallback: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rebuild EVERY catalog_items.offer_id from the local FurnitureData.json.
|
* Force EVERY catalog_items.offer_id to equal its own row id.
|
||||||
*
|
*
|
||||||
* The Nitro client resolves offers through the furnidata `offerid`, and the
|
* The emulator maps offer ids back to catalog rows, so offer_id must always
|
||||||
* emulator maps that offer id back to a catalog item (CatalogManager
|
* mirror the primary key. Run after every import so drift from manual edits,
|
||||||
* offerDefs). The chain only works when catalog_items.offer_id equals the
|
* legacy rows or older imports is repaired automatically.
|
||||||
* furnidata offerid — which this CMS reconciles to the items_base sprite id.
|
|
||||||
*
|
|
||||||
* For every catalog row: resolve item_ids → items_base classname → furnidata
|
|
||||||
* offerid and write it into offer_id. Rows without a furnidata entry fall
|
|
||||||
* back to the sprite id itself (same value in practice). Run after every
|
|
||||||
* import so drift from manual edits or legacy rows is repaired automatically.
|
|
||||||
*/
|
*/
|
||||||
export async function rebuildCatalogOfferIds(): Promise<CatalogOfferRebuildResult> {
|
export async function rebuildCatalogOfferIds(): Promise<CatalogOfferRebuildResult> {
|
||||||
return withFurniDataLock(async () => {
|
return withFurniDataLock(async () => {
|
||||||
const furniData = (await readFurniData()) as Record<
|
const [countRows] = (await db.execute(
|
||||||
string,
|
sql`SELECT COUNT(*) AS total FROM catalog_items`,
|
||||||
{ furnitype?: Array<Record<string, unknown>> }
|
)) as unknown as [Array<{ total: number }>, unknown];
|
||||||
>;
|
const checked = Number(countRows?.[0]?.total ?? 0);
|
||||||
const offerById = new Map<number, number>();
|
|
||||||
const offerByClass = new Map<string, number>();
|
|
||||||
for (const section of ["roomitemtypes", "wallitemtypes"] as const) {
|
|
||||||
for (const e of furniData[section]?.furnitype ?? []) {
|
|
||||||
const cn = typeof e?.classname === "string" ? e.classname : "";
|
|
||||||
const entryId = Number(e?.id);
|
|
||||||
const offer = Number(e?.offerid);
|
|
||||||
if (
|
|
||||||
cn &&
|
|
||||||
Number.isFinite(offer) &&
|
|
||||||
offer > 0 &&
|
|
||||||
!offerByClass.has(cn)
|
|
||||||
) {
|
|
||||||
offerByClass.set(cn, offer);
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
Number.isFinite(entryId) &&
|
|
||||||
entryId > 0 &&
|
|
||||||
!offerById.has(entryId)
|
|
||||||
) {
|
|
||||||
offerById.set(
|
|
||||||
entryId,
|
|
||||||
Number.isFinite(offer) && offer > 0 ? offer : entryId,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const itemRows = await db
|
const [result] = (await db.execute(sql`
|
||||||
.select({ id: ItemsBase.id, itemName: ItemsBase.itemName })
|
UPDATE catalog_items SET offer_id = id WHERE offer_id <> id
|
||||||
.from(ItemsBase);
|
`)) as unknown as [Record<string, unknown>, unknown];
|
||||||
const nameById = new Map<number, string>();
|
|
||||||
for (const r of itemRows) nameById.set(r.id, r.itemName);
|
|
||||||
|
|
||||||
const catalogRows = await db
|
return { checked, fixed: Number(result.affectedRows ?? 0) };
|
||||||
.select({
|
|
||||||
id: CatalogItems.id,
|
|
||||||
itemIds: CatalogItems.itemIds,
|
|
||||||
offerId: CatalogItems.offerId,
|
|
||||||
})
|
|
||||||
.from(CatalogItems);
|
|
||||||
|
|
||||||
let fixed = 0;
|
|
||||||
let fallback = 0;
|
|
||||||
for (const row of catalogRows) {
|
|
||||||
const itemId = Number(String(row.itemIds ?? "").split(/[;,]/)[0]);
|
|
||||||
if (!Number.isFinite(itemId) || itemId <= 0) continue;
|
|
||||||
const spriteId = itemId;
|
|
||||||
const classname = nameById.get(itemId);
|
|
||||||
const furnidataOffer =
|
|
||||||
offerById.get(itemId) ??
|
|
||||||
(classname ? offerByClass.get(classname) : undefined);
|
|
||||||
const target = furnidataOffer ?? spriteId;
|
|
||||||
if (row.offerId === spriteId) continue;
|
|
||||||
await db
|
|
||||||
.update(CatalogItems)
|
|
||||||
.set({ offerId: target })
|
|
||||||
.where(eq(CatalogItems.id, row.id));
|
|
||||||
fixed++;
|
|
||||||
if (furnidataOffer === undefined) fallback++;
|
|
||||||
}
|
|
||||||
return { checked: catalogRows.length, fixed, fallback };
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -297,9 +297,9 @@ export function resetCatalogPageCache(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Walk every catalog item under the "Imported Furniture" tree and make its
|
* Walk every catalog item under the "Imported Furniture" tree and force its
|
||||||
* offer_id match the furnidata sprite id (item_ids). Fixes any stale values
|
* offer_id to equal its own catalog row id. Fixes any stale values (e.g.
|
||||||
* (e.g. legacy -1 rows) after an import.
|
* legacy -1 rows) after an import.
|
||||||
*/
|
*/
|
||||||
export async function reconcileImportedOfferIds(): Promise<{
|
export async function reconcileImportedOfferIds(): Promise<{
|
||||||
fixed: number;
|
fixed: number;
|
||||||
@@ -308,9 +308,9 @@ export async function reconcileImportedOfferIds(): Promise<{
|
|||||||
const [result] = (await db.execute(sql`
|
const [result] = (await db.execute(sql`
|
||||||
UPDATE catalog_items ci
|
UPDATE catalog_items ci
|
||||||
JOIN catalog_pages cp ON cp.id = ci.page_id
|
JOIN catalog_pages cp ON cp.id = ci.page_id
|
||||||
SET ci.offer_id = CAST(ci.item_ids AS UNSIGNED)
|
SET ci.offer_id = ci.id
|
||||||
WHERE cp.parent_id = ${parentId}
|
WHERE cp.parent_id = ${parentId}
|
||||||
AND ci.offer_id <> CAST(ci.item_ids AS UNSIGNED)
|
AND ci.offer_id <> ci.id
|
||||||
`)) as unknown as [Record<string, unknown>, unknown];
|
`)) as unknown as [Record<string, unknown>, unknown];
|
||||||
return { fixed: Number(result.affectedRows ?? 0) };
|
return { fixed: Number(result.affectedRows ?? 0) };
|
||||||
}
|
}
|
||||||
@@ -1028,7 +1028,7 @@ export async function importSingleFurni(params: {
|
|||||||
const price = autoPriceFurni(classname);
|
const price = autoPriceFurni(classname);
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
UPDATE catalog_items
|
UPDATE catalog_items
|
||||||
SET page_id = ${pageIdStr}, cost_credits = ${price.credits}, cost_points = ${price.points}, points_type = ${price.pointsType}, offer_id = ${spriteId}
|
SET page_id = ${pageIdStr}, cost_credits = ${price.credits}, cost_points = ${price.points}, points_type = ${price.pointsType}, offer_id = id
|
||||||
WHERE id = ${catalogItemId}
|
WHERE id = ${catalogItemId}
|
||||||
`);
|
`);
|
||||||
catalogUpdated = true;
|
catalogUpdated = true;
|
||||||
@@ -1048,7 +1048,7 @@ export async function importSingleFurni(params: {
|
|||||||
catalogItemId = await allocateCatalogItemId(async (nextCatalogId) => {
|
catalogItemId = await allocateCatalogItemId(async (nextCatalogId) => {
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
||||||
VALUES (${nextCatalogId}, ${pageIdStr}, ${itemIdStr}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${spriteId}, '')
|
VALUES (${nextCatalogId}, ${pageIdStr}, ${itemIdStr}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${nextCatalogId}, '')
|
||||||
`);
|
`);
|
||||||
return nextCatalogId;
|
return nextCatalogId;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -282,15 +282,20 @@ describeGuard("live furni import / catalog ID consistency (sandbox DB)", () => {
|
|||||||
expect(row.spriteId).toBe(id);
|
expect(row.spriteId).toBe(id);
|
||||||
|
|
||||||
const [catalog] = (await db.execute(sql`
|
const [catalog] = (await db.execute(sql`
|
||||||
SELECT item_ids, catalog_name, offer_id FROM catalog_items WHERE item_ids = ${String(id)}
|
SELECT id, item_ids, catalog_name, offer_id FROM catalog_items WHERE item_ids = ${String(id)}
|
||||||
`)) as unknown as [
|
`)) as unknown as [
|
||||||
Array<{ item_ids: string; catalog_name: string; offer_id: number }>,
|
Array<{
|
||||||
|
id: number;
|
||||||
|
item_ids: string;
|
||||||
|
catalog_name: string;
|
||||||
|
offer_id: number;
|
||||||
|
}>,
|
||||||
unknown,
|
unknown,
|
||||||
];
|
];
|
||||||
expect(catalog).toHaveLength(1);
|
expect(catalog).toHaveLength(1);
|
||||||
expect(catalog[0].item_ids).toBe(String(id));
|
expect(catalog[0].item_ids).toBe(String(id));
|
||||||
expect(catalog[0].catalog_name).toBe(classname);
|
expect(catalog[0].catalog_name).toBe(classname);
|
||||||
expect(catalog[0].offer_id).toBe(id);
|
expect(catalog[0].offer_id).toBe(catalog[0].id);
|
||||||
|
|
||||||
// Re-import with updateExisting: previously fell through to an INSERT on
|
// Re-import with updateExisting: previously fell through to an INSERT on
|
||||||
// the existing PK and failed with "DB insert failed". Must now succeed.
|
// the existing PK and failed with "DB insert failed". Must now succeed.
|
||||||
|
|||||||
@@ -206,7 +206,7 @@ async function writeSqlMigration(params: {
|
|||||||
`INSERT INTO catalog_items`,
|
`INSERT INTO catalog_items`,
|
||||||
` (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)`,
|
` (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)`,
|
||||||
`SELECT (SELECT COALESCE(MAX(id), 0) + 1 FROM catalog_items), p.id, @next_id, ${escapeSql(params.classname)},`,
|
`SELECT (SELECT COALESCE(MAX(id), 0) + 1 FROM catalog_items), p.id, @next_id, ${escapeSql(params.classname)},`,
|
||||||
` ${params.price.credits}, ${params.price.points}, ${params.price.pointsType}, 1, 1, @next_id, ''`,
|
` ${params.price.credits}, ${params.price.points}, ${params.price.pointsType}, 1, 1, (SELECT COALESCE(MAX(id), 0) + 1 FROM catalog_items), ''`,
|
||||||
`FROM catalog_pages p`,
|
`FROM catalog_pages p`,
|
||||||
`WHERE p.caption_save = ${escapeSql(captionSave)};`,
|
`WHERE p.caption_save = ${escapeSql(captionSave)};`,
|
||||||
"",
|
"",
|
||||||
@@ -404,7 +404,7 @@ export async function uploadSingleFurni(params: {
|
|||||||
catalogItemId = await allocateCatalogItemId(async (nextCatalogId) => {
|
catalogItemId = await allocateCatalogItemId(async (nextCatalogId) => {
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
||||||
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${newId}, '')`);
|
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${nextCatalogId}, '')`);
|
||||||
return nextCatalogId;
|
return nextCatalogId;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user