-- Adds the two constraints that V72 deferred:
--   1. UNIQUE(geschichte_id, position) DEFERRABLE INITIALLY DEFERRED
--      Allows mid-transaction position swaps during reorder (checked at COMMIT, not per-row).
--      Requires transaction-level or session-level connection pooling (prod uses PgBouncer
--      in transaction mode — correct today; a future switch to statement-level would silently
--      break deferred checking at COMMIT).
--   2. CHECK (position > 0) — defense against off-by-one in the append path.
--
-- MUST run in a single transaction; Flyway's default per-migration transaction satisfies this.
-- Do NOT add executeInTransaction=false or any callback that splits this migration.

ALTER TABLE journey_items
    ADD CONSTRAINT uq_journey_items_geschichte_position
        UNIQUE (geschichte_id, position)
        DEFERRABLE INITIALLY DEFERRED;

ALTER TABLE journey_items
    ADD CONSTRAINT chk_journey_item_position
        CHECK (position > 0);
