Skip to content

Commit 2ea97a7

Browse files
committed
fix(utils): add Dexie migration to clean up corrupted history entries
Add automatic database migration (v1 → v2) that removes history entries containing "[object Object]" in the entityId field. This corruption was caused by TanStack Router's location.search being an object that was concatenated with strings in useNavigationEnhancements.ts (now fixed). The migration runs automatically when the database is opened with the new version, cleaning up any corrupted entries and logging the count.
1 parent dfe3d64 commit 2ea97a7

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

packages/utils/src/storage/catalogue-db.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export const catalogueEventEmitter = new CatalogueEventEmitter();
5252
// Constants
5353
const LOG_CATEGORY = "catalogue";
5454
const DB_NAME = "bibgraph-catalogue";
55-
const DB_VERSION = 1;
55+
const DB_VERSION = 2;
56+
57+
// Pattern for corrupted history entries (from location.search object concatenation bug)
58+
const CORRUPTED_ENTITY_ID_PATTERN = "[object Object]";
5659

5760
// Special list identifiers
5861
export const SPECIAL_LIST_IDS = {
@@ -132,13 +135,45 @@ class CatalogueDB extends Dexie {
132135
super(DB_NAME);
133136

134137
// T076: Optimized indexes for common query patterns
135-
this.version(DB_VERSION).stores({
138+
this.version(1).stores({
136139
catalogueLists: "id, title, type, createdAt, updatedAt, isPublic, shareToken, *tags",
137140
// Added compound index [listId+position] for efficient reordering and sorted entity retrieval
138141
// Added compound index [listId+entityType+entityId] for duplicate detection
139142
catalogueEntities: "id, listId, entityType, entityId, addedAt, position, [listId+position], [listId+entityType+entityId]",
140143
catalogueShares: "id, listId, shareToken, createdAt, expiresAt",
141144
});
145+
146+
// Migration v2: Clean up corrupted history entries containing [object Object]
147+
// This fixes data corruption from the useNavigationEnhancements.ts bug where
148+
// TanStack Router's location.search (an object) was concatenated with strings
149+
this.version(2)
150+
.stores({
151+
// Same schema as v1 - no structural changes
152+
catalogueLists: "id, title, type, createdAt, updatedAt, isPublic, shareToken, *tags",
153+
catalogueEntities: "id, listId, entityType, entityId, addedAt, position, [listId+position], [listId+entityType+entityId]",
154+
catalogueShares: "id, listId, shareToken, createdAt, expiresAt",
155+
})
156+
.upgrade(async (tx) => {
157+
const entities = tx.table("catalogueEntities");
158+
const corruptedEntries = await entities
159+
.filter((entity: CatalogueEntity) =>
160+
entity.entityId.includes(CORRUPTED_ENTITY_ID_PATTERN)
161+
)
162+
.toArray();
163+
164+
if (corruptedEntries.length > 0) {
165+
const corruptedIds = corruptedEntries
166+
.map((e: CatalogueEntity) => e.id)
167+
.filter((id): id is string => id !== undefined);
168+
169+
await entities.bulkDelete(corruptedIds);
170+
171+
// Log cleanup (console.log since logger not available in migration context)
172+
console.log(
173+
`[catalogue-db] Migration v2: Cleaned up ${corruptedIds.length} corrupted history entries`
174+
);
175+
}
176+
});
142177
}
143178
}
144179

0 commit comments

Comments
 (0)