fix: unique index words.titel_en als partial index + robuster Upsert ohne ON CONFLICT

- Migration: partiell WHERE IS NOT NULL, dedup vorher, kein silent-catch
- Route: INSERT mit .catch(23505) → UPDATE statt ON CONFLICT (partial index inkompatibel)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-18 21:14:21 +02:00
parent 1d25f84f5d
commit 455969bdec
2 changed files with 30 additions and 9 deletions

View File

@@ -861,8 +861,20 @@ async function migrate() {
await query(`ALTER TABLE words ADD CONSTRAINT words_level_check CHECK (level IN ('A1', 'A2', 'B1'))`).catch(() => {});
// Unique-Index auf titel_en — Voraussetzung für ON CONFLICT im CSV-Import.
// Falls bestehende Duplikate den Index verhindern, muss erst bereinigt werden.
await query(`CREATE UNIQUE INDEX IF NOT EXISTS words_titel_en_key ON words (titel_en)`).catch(() => {});
// Partiell (WHERE IS NOT NULL) damit bestehende NULL-Zeilen den Index nicht blockieren.
// Doppelte non-null titel_en erst bereinigen, dann Index anlegen.
await query(`
DELETE FROM words w
USING (
SELECT titel_en, MAX(created_at) AS keep_at
FROM words WHERE titel_en IS NOT NULL
GROUP BY titel_en HAVING COUNT(*) > 1
) dup
WHERE w.titel_en = dup.titel_en AND w.created_at < dup.keep_at
`).catch(() => {});
await query(
`CREATE UNIQUE INDEX IF NOT EXISTS words_titel_en_key ON words (titel_en) WHERE titel_en IS NOT NULL`
);
// enrich_batches — Status-Tracking für Wort-Anreicherungs-Batches (analog category_batches)
await query(`