Skip to content

Commit ac84382

Browse files
authored
Merge pull request #18 from AdaInTheLab/bugfix/ko-error
FIX [DB] Resolve localized slug collisions by enforcing (slug, locale…
2 parents 5960cd9 + fb719f9 commit ac84382

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/db/migrateLabNotes.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import crypto from "crypto";
1717
* If content appears stale or "reverts", check views FIRST.
1818
*/
1919

20-
export const LAB_NOTES_SCHEMA_VERSION = 9;
20+
export const LAB_NOTES_SCHEMA_VERSION = 10;
2121

2222
function setLabNotesSchemaVersion(db: Database.Database, version: number) {
2323
const cur = db
@@ -563,6 +563,37 @@ export function migrateLabNotesSchema(
563563
`);
564564
}
565565

566+
// ---- v10: remove legacy UNIQUE(slug) constraints (keep UNIQUE(slug, locale)) ----
567+
if (prevVersion < 10) {
568+
// Find any UNIQUE indexes that cover slug only (legacy) and drop them.
569+
// We keep uq_lab_notes_slug_locale (or recreate it if missing).
570+
const indexes = db
571+
.prepare(`PRAGMA index_list('lab_notes')`)
572+
.all() as Array<{ name: string; unique: number }>;
573+
574+
for (const idx of indexes) {
575+
if (!idx.unique) continue;
576+
577+
const cols = db
578+
.prepare(`PRAGMA index_info('${idx.name.replace(/'/g, "''")}')`)
579+
.all() as Array<{ name: string }>;
580+
581+
const colNames = cols.map((c) => c.name);
582+
const isSlugOnly = colNames.length === 1 && colNames[0] === "slug";
583+
584+
// Drop legacy slug-only uniqueness (this is what is causing your error)
585+
if (isSlugOnly) {
586+
db.exec(`DROP INDEX IF EXISTS ${idx.name};`);
587+
log?.(`[db] dropped legacy unique index on slug: ${idx.name}`);
588+
}
589+
}
590+
591+
// Ensure the correct uniqueness exists
592+
db.exec(`
593+
CREATE UNIQUE INDEX IF NOT EXISTS uq_lab_notes_slug_locale
594+
ON lab_notes(slug, locale);
595+
`);
596+
}
566597

567598

568599
// ⚠️ WARNING ⚠️

0 commit comments

Comments
 (0)