-- JOURNEY intros travel the verbatim (unsanitized) write path and get the same
-- three-layer bound as journey notes: frontend maxlength, the
-- GeschichteService.MAX_INTRO_LENGTH check, and this CHECK as the atomic backstop.
-- STORY bodies are sanitized Tiptap HTML and stay unbounded on purpose.
--
-- The title needs no CHECK here — VARCHAR(255) (V58) already bounds it at the
-- DB layer; the service-level check exists to turn that 500 into a friendly 400.

-- Defensive clamp first: intros written before this migration may exceed the
-- cap. No-op on a clean database.
UPDATE geschichten SET body = left(body, 4000)
    WHERE type = 'JOURNEY' AND length(body) > 4000;

ALTER TABLE geschichten
    ADD CONSTRAINT chk_geschichte_journey_intro_length
        CHECK (type <> 'JOURNEY' OR body IS NULL OR length(body) <= 4000);
