From 51b1388233fc5cb9ed05653985fcec275da273ff Mon Sep 17 00:00:00 2001 From: Joshua King Date: Tue, 15 Sep 2026 18:03:49 -0400 Subject: [PATCH] Fix: inverted ranges skip the overlap probe and hit the schema CHECK The matrix test's end-before-start probe (expecting the schema's end>=start IntegrityError) hit the new overlap query first, since an inverted range lexically 'overlaps' the seeded row. The overlap check now runs only for well-ordered ranges; invalid ones fall through to the INSERT and keep the IntegrityError contract. Suite: 990 passed. --- custom_components/nestquest/dao_presence.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/custom_components/nestquest/dao_presence.py b/custom_components/nestquest/dao_presence.py index 6bb5ddd..5279a25 100644 --- a/custom_components/nestquest/dao_presence.py +++ b/custom_components/nestquest/dao_presence.py @@ -205,12 +205,20 @@ class PresenceOverridesDao: ) if child is None: raise ValueError(f"child {child_id} does not exist") - conflicting = await self._database.fetch_one( - f"SELECT {_OVERRIDE_COLUMNS} FROM presence_overrides " - "WHERE child_id = ? AND end_date >= ? " - "AND start_date <= ? ORDER BY id LIMIT 1", - (child_id, start_date, end_date), - ) + # Only a well-ordered range has overlap semantics: + # inverted ranges fall through to the INSERT and hit + # the schema's end>=start CHECK, preserving the + # IntegrityError contract the matrix tests rely on. + if end_date >= start_date: + conflicting = await self._database.fetch_one( + f"SELECT {_OVERRIDE_COLUMNS} " + "FROM presence_overrides " + "WHERE child_id = ? AND end_date >= ? " + "AND start_date <= ? ORDER BY id LIMIT 1", + (child_id, start_date, end_date), + ) + else: + conflicting = None if conflicting is not None: conflict = _override_from_row(conflicting) raise ValueError(