11from __future__ import annotations
22
33from datetime import datetime
4- from typing import Any , List , Mapping , Self , Sequence , TypedDict
4+ from typing import Mapping , Self , Sequence
55from uuid import uuid4
66
7- from beets . importer import ImportTask , library
8- from sqlalchemy import Index , LargeBinary , select
7+ import pytz
8+ from sqlalchemy import LargeBinary , select
99from sqlalchemy .orm import (
1010 DeclarativeBase ,
1111 Mapped ,
1212 Session ,
1313 mapped_column ,
14+ reconstructor ,
1415 registry ,
1516)
1617from sqlalchemy .sql import func
@@ -24,6 +25,7 @@ class Base(DeclarativeBase):
2425 registry = registry (type_annotation_map = {bytes : LargeBinary })
2526
2627 id : Mapped [str ] = mapped_column (primary_key = True )
28+
2729 created_at : Mapped [datetime ] = mapped_column (default = func .now (), index = True )
2830 updated_at : Mapped [datetime ] = mapped_column (
2931 default = func .now (), onupdate = func .now ()
@@ -96,3 +98,13 @@ def exist_all_ids(cls, ids: list[str], session: Session) -> bool:
9698
9799 def to_dict (self ) -> Mapping :
98100 return {c .name : getattr (self , c .name ) for c in self .__table__ .columns }
101+
102+ @reconstructor
103+ def _sqlalchemy_reconstructor (self ):
104+ # Set timezone info for created_at and updated_at
105+ # Seems a bit hacky but is the only way to ensure that
106+ # datetime objects are timezone-aware after deserialization
107+ if self .created_at and self .created_at .tzinfo is None :
108+ self .created_at = self .created_at .replace (tzinfo = pytz .UTC )
109+ if self .updated_at and self .updated_at .tzinfo is None :
110+ self .updated_at = self .updated_at .replace (tzinfo = pytz .UTC )
0 commit comments