From 432632aadc8aeb7f702879f45b073dd3884a6935 Mon Sep 17 00:00:00 2001 From: gilesb Date: Thu, 8 Jan 2026 00:40:37 +0000 Subject: [PATCH] Fix asyncpg datetime type error in create_asset and create_activity asyncpg requires datetime objects, not ISO strings. Added _parse_timestamp helper to convert string timestamps to datetime objects. Co-Authored-By: Claude Opus 4.5 --- db.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/db.py b/db.py index 6e997aa..8653e5a 100644 --- a/db.py +++ b/db.py @@ -14,6 +14,22 @@ from uuid import UUID import asyncpg # Connection pool (initialized on startup) + + +def _parse_timestamp(ts) -> datetime: + """Parse a timestamp string or datetime to datetime object.""" + if ts is None: + return datetime.now(timezone.utc) + if isinstance(ts, datetime): + return ts + # Parse ISO format string + if isinstance(ts, str): + if ts.endswith('Z'): + ts = ts[:-1] + '+00:00' + return datetime.fromisoformat(ts) + return datetime.now(timezone.utc) + + _pool: Optional[asyncpg.Pool] = None # Configuration from environment @@ -246,7 +262,7 @@ async def create_asset(asset: dict) -> dict: asset.get("description"), json.dumps(asset.get("origin")) if asset.get("origin") else None, asset["owner"], - asset.get("created_at") or datetime.now(timezone.utc).isoformat() + _parse_timestamp(asset.get("created_at")) ) return _parse_asset_row(row) @@ -376,7 +392,7 @@ async def create_activity(activity: dict) -> dict: activity["activity_type"], activity["actor_id"], json.dumps(activity["object_data"]), - activity["published"], + _parse_timestamp(activity["published"]), json.dumps(activity.get("signature")) if activity.get("signature") else None ) return _parse_activity_row(row)