diff --git a/backend/src/main/resources/db/migration/V74__journey_items_document_dedup_and_note_length.sql b/backend/src/main/resources/db/migration/V74__journey_items_document_dedup_and_note_length.sql index 5939d2c6..062e0d6d 100644 --- a/backend/src/main/resources/db/migration/V74__journey_items_document_dedup_and_note_length.sql +++ b/backend/src/main/resources/db/migration/V74__journey_items_document_dedup_and_note_length.sql @@ -11,6 +11,22 @@ -- 2. CHECK on note length: mirrors chk_text_length on transcription_blocks. -- 2000 is the spec'd limit — JourneyItemService.MAX_NOTE_LENGTH, the frontend -- maxlength, and the i18n error message all agree (#793). +-- +-- Defensive cleanup first: a database that served writes on the base branch +-- (no dedup guard, MAX_NOTE_LENGTH = 5000) can hold rows that would make the +-- DDL below fail mid-migration and boot-loop the backend on a failed Flyway +-- row. Both statements are no-ops on a clean database. + +-- Keep the earliest-positioned row of each (geschichte, document) pair. +DELETE FROM journey_items a + USING journey_items b + WHERE a.geschichte_id = b.geschichte_id + AND a.document_id = b.document_id + AND a.document_id IS NOT NULL + AND a.position > b.position; + +-- Clamp over-long notes written under the old 5000-char service limit. +UPDATE journey_items SET note = left(note, 2000) WHERE length(note) > 2000; CREATE UNIQUE INDEX uq_journey_items_geschichte_document ON journey_items (geschichte_id, document_id)