Compare commits

...

2 commits

Author SHA1 Message Date
855c36c38c [refactor] use ORM declarative models
All checks were successful
/ run-pytest (push) Successful in 49s
docs: https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#orm-declarative-models
2024-09-30 01:46:11 +08:00
b14de650e9 [build] update sqlalchemy 2024-09-30 01:45:56 +08:00
4 changed files with 71 additions and 69 deletions

View file

@ -6,7 +6,7 @@ from sqlalchemy import MetaData
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.asyncio import create_async_engine from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import declarative_base from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from app.config import DB_PATH from app.config import DB_PATH
@ -25,7 +25,9 @@ async_engine = create_async_engine(
) )
async_session = sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False) async_session = sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
Base: Any = declarative_base() class Base(DeclarativeBase):
pass
metadata_obj = MetaData() metadata_obj = MetaData()
async def get_db_session() -> AsyncGenerator[AsyncSession, None]: async def get_db_session() -> AsyncGenerator[AsyncSession, None]:

View file

@ -10,7 +10,7 @@ from app.database import metadata_obj
from app.activitypub import BaseActor from app.activitypub import BaseActor
from app.ap_object import BaseObject from app.ap_object import BaseObject
from sqlalchemy import Column from sqlalchemy.orm import mapped_column
from sqlalchemy import Boolean from sqlalchemy import Boolean
from sqlalchemy import DateTime from sqlalchemy import DateTime
from sqlalchemy import Enum from sqlalchemy import Enum
@ -32,46 +32,46 @@ def now() -> dtime:
class Actor(Base, BaseActor): class Actor(Base, BaseActor):
__tablename__ = "actor" __tablename__ = "actor"
id = Column(Integer, primary_key=True, index=True) id = mapped_column(Integer, primary_key=True, index=True)
created_at = Column(DateTime(timezone=True), nullable=False, default=now) created_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
updated_at = Column(DateTime(timezone=True), nullable=False, default=now) updated_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
ap_id = Column(String, unique=True, nullable=False, index=True) ap_id = mapped_column(String, unique=True, nullable=False, index=True)
ap_actor = Column(JSON, nullable=False) ap_actor = mapped_column(JSON, nullable=False)
ap_type = Column(String, nullable=False) ap_type = mapped_column(String, nullable=False)
handle = Column(String, nullable=True, index=True) handle = mapped_column(String, nullable=True, index=True)
is_blocked = Column(Boolean, nullable=False, default=False, server_default="0") is_blocked = mapped_column(Boolean, nullable=False, default=False, server_default="0")
is_deleted = Column(Boolean, nullable=False, default=False, server_default="0") is_deleted = mapped_column(Boolean, nullable=False, default=False, server_default="0")
class InboxObject(Base, BaseObject): class InboxObject(Base, BaseObject):
__tablename__ = "inbox" __tablename__ = "inbox"
id = Column(Integer, primary_key=True, index=True) id = mapped_column(Integer, primary_key=True, index=True)
created_at = Column(DateTime(timezone=True), nullable=False, default=now) created_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
updated_at = Column(DateTime(timezone=True), nullable=False, default=now) updated_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
actor_id = Column(Integer, ForeignKey("actor.id"), nullable=False) actor_id = mapped_column(Integer, ForeignKey("actor.id"), nullable=False)
actor: Mapped[Actor] = relationship(Actor, uselist=False) actor: Mapped[Actor] = relationship(Actor, uselist=False)
server = Column(String, nullable=False) server = mapped_column(String, nullable=False)
is_hidden_from_stream = Column(Boolean, nullable=False, default=False) is_hidden_from_stream = mapped_column(Boolean, nullable=False, default=False)
ap_actor_id = Column(String, nullable=False) ap_actor_id = mapped_column(String, nullable=False)
ap_type = Column(String, nullable=False, index=True) ap_type = mapped_column(String, nullable=False, index=True)
ap_id = Column(String, nullable=False, unique=True, index=True) ap_id = mapped_column(String, nullable=False, unique=True, index=True)
ap_context = Column(String, nullable=True) ap_context = mapped_column(String, nullable=True)
ap_published_at = Column(DateTime(timezone=True), nullable=False) ap_published_at = mapped_column(DateTime(timezone=True), nullable=False)
ap_object = Column(JSON, nullable=False) ap_object = mapped_column(JSON, nullable=False)
activity_object_ap_id = Column(String, nullable=True, index=True) activity_object_ap_id = mapped_column(String, nullable=True, index=True)
visibility = Column(Enum(ap.VisibilityEnum), nullable=False) visibility = mapped_column(Enum(ap.VisibilityEnum), nullable=False)
is_deleted = Column(Boolean, nullable=False, default=False) is_deleted = mapped_column(Boolean, nullable=False, default=False)
relates_to_inbox_object_id = Column( relates_to_inbox_object_id = mapped_column(
Integer, Integer,
ForeignKey("inbox.id"), ForeignKey("inbox.id"),
nullable=True, nullable=True,
@ -82,7 +82,7 @@ class InboxObject(Base, BaseObject):
remote_side=id, remote_side=id,
uselist=False, uselist=False,
) )
relates_to_outbox_object_id = Column( relates_to_outbox_object_id = mapped_column(
Integer, Integer,
ForeignKey("outbox.id", use_alter=True), ForeignKey("outbox.id", use_alter=True),
nullable=True, nullable=True,
@ -97,28 +97,28 @@ class InboxObject(Base, BaseObject):
class OutboxObject(Base, BaseObject): class OutboxObject(Base, BaseObject):
__tablename__ = "outbox" __tablename__ = "outbox"
id = Column(Integer, primary_key=True, index=True) id = mapped_column(Integer, primary_key=True, index=True)
created_at = Column(DateTime(timezone=True), nullable=False, default=now) created_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
updated_at = Column(DateTime(timezone=True), nullable=False, default=now) updated_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
public_id = Column(String, nullable=False, index=True) public_id = mapped_column(String, nullable=False, index=True)
slug = Column(String, nullable=True, index=True) slug = mapped_column(String, nullable=True, index=True)
ap_type = Column(String, nullable=False, index=True) ap_type = mapped_column(String, nullable=False, index=True)
ap_id = Column(String, nullable=False, unique=True, index=True) ap_id = mapped_column(String, nullable=False, unique=True, index=True)
ap_context = Column(String, nullable=True) ap_context = mapped_column(String, nullable=True)
ap_object = Column(JSON, nullable=False) ap_object = mapped_column(JSON, nullable=False)
activity_object_ap_id = Column(String, nullable=True, index=True) activity_object_ap_id = mapped_column(String, nullable=True, index=True)
visibility = Column(Enum(ap.VisibilityEnum), nullable=False) visibility = mapped_column(Enum(ap.VisibilityEnum), nullable=False)
is_deleted = Column(Boolean, nullable=False, default=False) is_deleted = mapped_column(Boolean, nullable=False, default=False)
likes_count = Column(Integer, nullable=False, default=0) likes_count = mapped_column(Integer, nullable=False, default=0)
announces_count = Column(Integer, nullable=False, default=0) announces_count = mapped_column(Integer, nullable=False, default=0)
replies_count = Column(Integer, nullable=False, default=0) replies_count = mapped_column(Integer, nullable=False, default=0)
relates_to_inbox_object_id = Column( relates_to_inbox_object_id = mapped_column(
Integer, Integer,
ForeignKey("inbox.id", use_alter=True), ForeignKey("inbox.id", use_alter=True),
nullable=True, nullable=True,
@ -128,7 +128,7 @@ class OutboxObject(Base, BaseObject):
foreign_keys=relates_to_inbox_object_id, foreign_keys=relates_to_inbox_object_id,
uselist=False, uselist=False,
) )
relates_to_outbox_object_id = Column( relates_to_outbox_object_id = mapped_column(
Integer, Integer,
ForeignKey("outbox.id"), ForeignKey("outbox.id"),
nullable=True, nullable=True,
@ -140,7 +140,7 @@ class OutboxObject(Base, BaseObject):
uselist=False, uselist=False,
) )
relates_to_actor_id = Column( relates_to_actor_id = mapped_column(
Integer, Integer,
ForeignKey("actor.id"), ForeignKey("actor.id"),
nullable=True, nullable=True,
@ -155,50 +155,50 @@ class OutboxObject(Base, BaseObject):
class IncomingActivity(Base): class IncomingActivity(Base):
__tablename__ = "ingress" __tablename__ = "ingress"
id = Column(Integer, primary_key=True, index=True) id = mapped_column(Integer, primary_key=True, index=True)
created_at = Column(DateTime(timezone=True), nullable=False, default=now) created_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
ap_id = Column(String, nullable=True, index=True) ap_id = mapped_column(String, nullable=True, index=True)
ap_object = Column(JSON, nullable=True) ap_object = mapped_column(JSON, nullable=True)
class OutcomingActivity(Base): class OutcomingActivity(Base):
__tablename__ = "push" __tablename__ = "push"
id = Column(Integer, primary_key=True, index=True) id = mapped_column(Integer, primary_key=True, index=True)
created_at = Column(DateTime(timezone=True), nullable=False, default=now) created_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
ap_id = Column(String, nullable=True, index=True) ap_id = mapped_column(String, nullable=True, index=True)
ap_object = Column(JSON, nullable=True) ap_object = mapped_column(JSON, nullable=True)
class Follower(Base): class Follower(Base):
__tablename__ = "follower" __tablename__ = "follower"
id = Column(Integer, primary_key=True, index=True) id = mapped_column(Integer, primary_key=True, index=True)
created_at = Column(DateTime(timezone=True), nullable=False, default=now) created_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
updated_at = Column(DateTime(timezone=True), nullable=False, default=now) updated_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
actor_id = Column(Integer, ForeignKey("actor.id"), nullable=False, unique=True) actor_id = mapped_column(Integer, ForeignKey("actor.id"), nullable=False, unique=True)
actor: Mapped[Actor] = relationship(Actor, uselist=False) actor: Mapped[Actor] = relationship(Actor, uselist=False)
inbox_object_id = Column(Integer, ForeignKey("inbox.id"), nullable=False) inbox_object_id = mapped_column(Integer, ForeignKey("inbox.id"), nullable=False)
inbox_object = relationship(InboxObject, uselist=False) inbox_object = relationship(InboxObject, uselist=False)
ap_actor_id = Column(String, nullable=False, unique=True) ap_actor_id = mapped_column(String, nullable=False, unique=True)
class Following(Base): class Following(Base):
__tablename__ = "following" __tablename__ = "following"
id = Column(Integer, primary_key=True, index=True) id = mapped_column(Integer, primary_key=True, index=True)
created_at = Column(DateTime(timezone=True), nullable=False, default=now) created_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
updated_at = Column(DateTime(timezone=True), nullable=False, default=now) updated_at = mapped_column(DateTime(timezone=True), nullable=False, default=now)
actor_id = Column(Integer, ForeignKey("actor.id"), nullable=False, unique=True) actor_id = mapped_column(Integer, ForeignKey("actor.id"), nullable=False, unique=True)
actor: Mapped[Actor] = relationship(Actor, uselist=False) actor: Mapped[Actor] = relationship(Actor, uselist=False)
outbox_object_id = Column(Integer, ForeignKey("outbox.id"), nullable=False) outbox_object_id = mapped_column(Integer, ForeignKey("outbox.id"), nullable=False)
outbox_object = relationship(OutboxObject, uselist=False) outbox_object = relationship(OutboxObject, uselist=False)
ap_actor_id = Column(String, nullable=False, unique=True) ap_actor_id = mapped_column(String, nullable=False, unique=True)

2
poetry.lock generated
View file

@ -1981,4 +1981,4 @@ dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = ">3.11, <3.13" python-versions = ">3.11, <3.13"
content-hash = "88180fc69bca605f3f6a80d8e62a8e959731763ad3a13bc74fb1a41ebca40487" content-hash = "a5a79aad191dfc8d9fd0dfa18e8a16cc72d5e4ddf6313aad512eb339a3c8dda9"

View file

@ -11,7 +11,7 @@ fastapi = "^0.111.0"
httpx = "^0.27.0" httpx = "^0.27.0"
uvicorn = "^0.30.1" uvicorn = "^0.30.1"
loguru = "^0.7.2" loguru = "^0.7.2"
sqlalchemy = "^2.0.27" sqlalchemy = "^2.0.35"
aiosqlite = "^0.20.0" aiosqlite = "^0.20.0"
alembic = "^1.13.1" alembic = "^1.13.1"
tomli = "^2.0.1" tomli = "^2.0.1"