From d2b074d39be372d99f9fec602b3c85e6a9b9cf40 Mon Sep 17 00:00:00 2001 From: chelsea Date: Thu, 19 Feb 2026 13:14:20 -0600 Subject: [PATCH] Fix VARCHAR vs TIME type mismatch in med_logs queries (#15) The medication_schedules.adjusted_time column is a TIME type, but med_logs.scheduled_time is VARCHAR. When adjusted_time values were passed into queries against med_logs, PostgreSQL rejected the comparison. Add _normalize_time() to convert datetime.time objects to "HH:MM" strings at the entry points of should_send_nag, record_nag_sent, and mark_med_taken. Co-Authored-By: Claude Opus 4.6 --- core/adaptive_meds.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/core/adaptive_meds.py b/core/adaptive_meds.py index d4f66a2..a15af69 100644 --- a/core/adaptive_meds.py +++ b/core/adaptive_meds.py @@ -16,6 +16,15 @@ import core.postgres as postgres from core.tz import user_now, user_today_for +def _normalize_time(val): + """Convert datetime.time objects to 'HH:MM' strings for use in VARCHAR queries.""" + if isinstance(val, time): + return val.strftime("%H:%M") + if val is not None: + return str(val)[:5] + return val + + def get_adaptive_settings(user_uuid: str) -> Optional[Dict]: """Get user's adaptive medication settings.""" rows = postgres.select("adaptive_med_settings", {"user_uuid": user_uuid}) @@ -226,7 +235,7 @@ def calculate_adjusted_times( def should_send_nag( - user_uuid: str, med_id: str, scheduled_time: str, current_time: datetime + user_uuid: str, med_id: str, scheduled_time, current_time: datetime ) -> Tuple[bool, str]: """ Determine if we should send a nag notification. @@ -234,6 +243,8 @@ def should_send_nag( Returns: (should_nag: bool, reason: str) """ + scheduled_time = _normalize_time(scheduled_time) + settings = get_adaptive_settings(user_uuid) if not settings: return False, "No settings" @@ -301,8 +312,9 @@ def should_send_nag( return True, "Time to nag" -def record_nag_sent(user_uuid: str, med_id: str, scheduled_time: str): +def record_nag_sent(user_uuid: str, med_id: str, scheduled_time): """Record that a nag was sent.""" + scheduled_time = _normalize_time(scheduled_time) today = user_today_for(user_uuid) query = {"user_uuid": user_uuid, "medication_id": med_id, "adjustment_date": today} @@ -353,8 +365,9 @@ def create_daily_schedule(user_uuid: str, med_id: str, base_times: List[str]): postgres.insert("medication_schedules", data) -def mark_med_taken(user_uuid: str, med_id: str, scheduled_time: str): +def mark_med_taken(user_uuid: str, med_id: str, scheduled_time): """Mark a medication as taken.""" + scheduled_time = _normalize_time(scheduled_time) today = user_today_for(user_uuid) postgres.update(