feat: use custom type to handle existing naive datetimes
This commit is contained in:
parent
e22abb383c
commit
39bdac1ecf
45 changed files with 210 additions and 84 deletions
21
app/models/types.py
Normal file
21
app/models/types.py
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue