Skip to content

Commit

Permalink
fix: make ChiiTimeLine.id type safe
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Sep 21, 2024
1 parent 7ab36c0 commit 2137cd2
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions chii/db/tables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datetime
import html
import time
from dataclasses import dataclass, field
Expand All @@ -18,14 +17,17 @@
reg: registry = registry()


@reg.mapped
@dataclass(kw_only=True)
class ChiiTimeline:
__tablename__ = "chii_timeline"
__sa_dataclass_metadata_key__ = "sa"

# mysql will generate ID for AUTO_INCREMENT field if value is 0
id: int = field(
init=False, metadata={"sa": Column("tml_id", INTEGER(10), primary_key=True)}
default=0,
metadata={
"sa": Column("tml_id", INTEGER(10), primary_key=True, autoincrement=True)
},
)
uid: int = field(
metadata={
Expand Down Expand Up @@ -97,12 +99,16 @@ class ChiiTimeline:
INTEGER(10),
nullable=False,
server_default=text("'0'"),
default=lambda: int(datetime.datetime.now().timestamp()),
default=lambda: int(time.time()),
)
},
)


# normally we should just use @reg.mapped
# but it make pycharm think `ChiiTimeline` is not a dataclasses
reg.mapped(ChiiTimeline)

# type helper for ChiiTimeline.uid.desc()
ChiiTimeline_column_id: Column[int] = cast(Column[int], ChiiTimeline.id)
ChiiTimeline_column_uid: Column[int] = cast(Column[int], ChiiTimeline.uid)
Expand All @@ -122,7 +128,6 @@ def process_result_value(self, value, dialect):
return html.unescape(value)


@reg.mapped
@dataclass(kw_only=True)
class ChiiSubject:
__tablename__ = "chii_subjects"
Expand Down Expand Up @@ -228,3 +233,6 @@ class ChiiSubject:
ban: int = field(
metadata={"sa": Column("subject_ban", TINYINT(1), server_default=text("'0'"))}
)


reg.mapped(ChiiSubject)

0 comments on commit 2137cd2

Please sign in to comment.