feat: use custom type to handle existing naive datetimes

This commit is contained in:
Iain Learmonth 2024-12-06 18:02:59 +00:00
parent e22abb383c
commit 39bdac1ecf
45 changed files with 210 additions and 84 deletions

21
app/models/types.py Normal file
View file

@ -0,0 +1,21 @@
from datetime import timezone
from sqlalchemy import DateTime, TypeDecorator
class AwareDateTime(TypeDecorator):
impl = DateTime(timezone=True)
cache_ok = True
def process_bind_param(self, value, dialect):
# Ensure the value is aware. If it's naive, assume UTC.
if value is not None and value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
return value
def process_result_value(self, value, dialect):
# Ensure the value is aware. If it's naive, assume UTC.
if value is not None and value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
return value