Skip to content
Snippets Groups Projects
Commit db257579 authored by Tulir Asokan's avatar Tulir Asokan :cat2:
Browse files

Merge remote-tracking branch 'erdnaxeli/fix_sqlite_timezone'

parents d6157c84 14e48050
No related branches found
No related tags found
No related merge requests found
...@@ -16,11 +16,13 @@ ...@@ -16,11 +16,13 @@
from typing import Optional, Iterable, List from typing import Optional, Iterable, List
from datetime import datetime from datetime import datetime
from sqlalchemy import Column, String, DateTime, SmallInteger, UniqueConstraint, and_ from sqlalchemy import Column, String, SmallInteger, UniqueConstraint, and_
from mautrix.types import RoomID, EventID from mautrix.types import RoomID, EventID
from mautrix.util.db import Base from mautrix.util.db import Base
from .types import UTCDateTime
class Message(Base): class Message(Base):
__tablename__ = "message" __tablename__ = "message"
...@@ -31,7 +33,7 @@ class Message(Base): ...@@ -31,7 +33,7 @@ class Message(Base):
fb_chat: str = Column(String(127), nullable=True) fb_chat: str = Column(String(127), nullable=True)
fb_receiver: str = Column(String(127), primary_key=True) fb_receiver: str = Column(String(127), primary_key=True)
index: int = Column(SmallInteger, primary_key=True, default=0) index: int = Column(SmallInteger, primary_key=True, default=0)
date: Optional[datetime] = Column(DateTime(timezone=True), nullable=True) date: Optional[datetime] = Column(UTCDateTime(timezone=True), nullable=True)
__table_args__ = (UniqueConstraint("mxid", "mx_room", name="_mx_id_room"),) __table_args__ = (UniqueConstraint("mxid", "mx_room", name="_mx_id_room"),)
......
from datetime import timezone
import sqlalchemy.types as types
class UTCDateTime(types.TypeDecorator):
"""Decorates the SQLAlchemy DateTime type to work with UTC datetimes.
It supposes we only manipulate UTC datetime. If the timezone is not set when saving or reading
a value, the UTC timezone is set. If a timezone is set, it ensures the datetime is converted to
UTC before saving it.
This is useful when working with SQLite as the SQLalchemy DateTime type loses the timezone
information when saving a datetime on this database.
"""
impl = types.DateTime
def process_bind_param(self, value, dialect):
if value is not None:
if value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
elif value.tzinfo != timezone.utc:
value = value.astimezone(timezone.utc)
return value
def process_result_value(self, value, dialect):
if value is not None and value.tzinfo is None:
return value.replace(tzinfo=timezone.utc)
else:
return value
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment