{"record":{"id":"b6b982fa7a333334","repo":"tobi/qmd","slug":"collection-newname-already-exists-b6b982","errorCode":null,"errorMessage":"Collection '${newName}' already exists","messagePattern":"Collection '(.+?)' already exists","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/store.ts","lineNumber":1380,"sourceCode":"    collection.path,\n    collection.pattern || '**/*.md',\n    collection.ignore ? JSON.stringify(collection.ignore) : null,\n    collection.includeByDefault === false ? 0 : 1,\n    collection.update || null,\n    collection.context ? JSON.stringify(collection.context) : null,\n  );\n}\n\nexport function deleteStoreCollection(db: Database, name: string): boolean {\n  const result = db.prepare(`DELETE FROM store_collections WHERE name = ?`).run(name);\n  return result.changes > 0;\n}\n\nexport function renameStoreCollection(db: Database, oldName: string, newName: string): boolean {\n  // Check target doesn't exist\n  const existing = db.prepare(`SELECT name FROM store_collections WHERE name = ?`).get(newName) as { name: string } | null | undefined;\n  if (existing != null) {\n    throw new Error(`Collection '${newName}' already exists`);\n  }\n\n  const result = db.prepare(`UPDATE store_collections SET name = ? WHERE name = ?`).run(newName, oldName);\n  return result.changes > 0;\n}\n\nexport function updateStoreContext(db: Database, collectionName: string, path: string, text: string): boolean {\n  const row = db.prepare(`SELECT context FROM store_collections WHERE name = ?`).get(collectionName) as { context: string | null } | null | undefined;\n  if (row == null) return false;\n\n  const ctxMap: ContextMap = row.context ? JSON.parse(row.context) : {};\n  ctxMap[path] = text;\n  db.prepare(`UPDATE store_collections SET context = ? WHERE name = ?`).run(JSON.stringify(ctxMap), collectionName);\n  return true;\n}\n\nexport function removeStoreContext(db: Database, collectionName: string, path: string): boolean {\n  const row = db.prepare(`SELECT context FROM store_collections WHERE name = ?`).get(collectionName) as { context: string | null } | null | undefined;","sourceCodeStart":1362,"sourceCodeEnd":1398,"githubUrl":"https://github.com/tobi/qmd/blob/dbfd0b4736aeaf761d1a16ca8e424f071df8feb9/src/store.ts#L1362-L1398","documentation":"renameStoreCollection() refuses to rename a collection to a name that already exists in the store_collections table, to avoid a name collision (which would otherwise break unique lookups).","triggerScenarios":"Calling renameStoreCollection(db, old, new) where a collection named `new` already exists — e.g. `qmd collection rename mynotes notes` when `notes` exists.","commonSituations":"Re-creating a target name after a previous merge/rename; case-insensitive filesystems hiding an existing name; scripts renaming in a loop that re-run after partial failure.","solutions":["Choose a different target name","Delete or re-add the existing target collection first if the collision is intended (qmd collection remove <new>)","Check collection existence with `qmd collection list` before renaming"],"exampleFix":"// before\nrenameStoreCollection(db, 'mynotes', 'notes'); // 'notes' exists\n// after\nif (!collectionExists(db, 'notes')) renameStoreCollection(db, 'mynotes', 'notes');\nelse throw new Error('target name taken — pick another');","handlingStrategy":"validation","validationCode":"const exists = db.prepare('SELECT 1 FROM store_collections WHERE name=?').get(newName); if (!exists) renameStoreCollection(db, old, newName);","typeGuard":"const isNameFree = (db: Database, n: string) => db.prepare('SELECT 1 FROM store_collections WHERE name=?').get(n) == null;","tryCatchPattern":null,"preventionTips":["List collections before rename scripts","Make rename operations idempotent with pre-checks","Avoid auto-generated target names that can collide"],"tags":["collection","rename","conflict"],"backgroundTag":"duplicate-resource","analyzedSha":"dbfd0b4736aeaf761d1a16ca8e424f071df8feb9","analyzedAt":"2026-08-28T18:07:46.628Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}