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 <noreply@anthropic.com>
This commit is contained in:
gilesb
2026-01-08 00:40:37 +00:00
parent 9c10de7534
commit 432632aadc

20
db.py
View File

@@ -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)