From fb719f945e265431db6fb5d3a51636f09d135bfe Mon Sep 17 00:00:00 2001 From: Ada Date: Fri, 9 Jan 2026 14:03:05 -0500 Subject: [PATCH] =?UTF-8?q?FIX=20[DB]=20Resolve=20localized=20slug=20colli?= =?UTF-8?q?sions=20by=20enforcing=20(slug,=20locale)=20identity=20?= =?UTF-8?q?=F0=9F=A7=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drop legacy UNIQUE(slug) constraints that blocked localized notes Enforce correct UNIQUE(slug, locale) index Preserve existing data while allowing multi-locale ingestion Unblock KO lab note sync without altering ingest logic co-authored-by: Lyric lyric@thehumanpatternlab.com co-authored-by: Carmel carmel@thehumanpatternlab.com --- src/db/migrateLabNotes.ts | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/db/migrateLabNotes.ts b/src/db/migrateLabNotes.ts index f8a9b78..1a76184 100644 --- a/src/db/migrateLabNotes.ts +++ b/src/db/migrateLabNotes.ts @@ -17,7 +17,7 @@ import crypto from "crypto"; * If content appears stale or "reverts", check views FIRST. */ -export const LAB_NOTES_SCHEMA_VERSION = 9; +export const LAB_NOTES_SCHEMA_VERSION = 10; function setLabNotesSchemaVersion(db: Database.Database, version: number) { const cur = db @@ -563,6 +563,37 @@ export function migrateLabNotesSchema( `); } +// ---- v10: remove legacy UNIQUE(slug) constraints (keep UNIQUE(slug, locale)) ---- + if (prevVersion < 10) { + // Find any UNIQUE indexes that cover slug only (legacy) and drop them. + // We keep uq_lab_notes_slug_locale (or recreate it if missing). + const indexes = db + .prepare(`PRAGMA index_list('lab_notes')`) + .all() as Array<{ name: string; unique: number }>; + + for (const idx of indexes) { + if (!idx.unique) continue; + + const cols = db + .prepare(`PRAGMA index_info('${idx.name.replace(/'/g, "''")}')`) + .all() as Array<{ name: string }>; + + const colNames = cols.map((c) => c.name); + const isSlugOnly = colNames.length === 1 && colNames[0] === "slug"; + + // Drop legacy slug-only uniqueness (this is what is causing your error) + if (isSlugOnly) { + db.exec(`DROP INDEX IF EXISTS ${idx.name};`); + log?.(`[db] dropped legacy unique index on slug: ${idx.name}`); + } + } + + // Ensure the correct uniqueness exists + db.exec(` + CREATE UNIQUE INDEX IF NOT EXISTS uq_lab_notes_slug_locale + ON lab_notes(slug, locale); + `); + } // ⚠️ WARNING ⚠️