Skip to content

Commit

Permalink
chore: Update SQLAlchemy to 2.0
Browse files Browse the repository at this point in the history
Updated SQLAlchemy to 2.0 to have proper type hints and newest API.
  • Loading branch information
gbdlin committed Aug 13, 2024
1 parent 97e23f0 commit b4a0aa3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 70 deletions.
27 changes: 14 additions & 13 deletions plugin_store/database/models/Artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from urllib.parse import quote

from sqlalchemy import Boolean, Column, ForeignKey, func, Integer, select, Table, Text, UniqueConstraint
from sqlalchemy.orm import column_property, relationship
from sqlalchemy.orm import column_property, Mapped, relationship

import constants

Expand All @@ -29,30 +29,31 @@ class Tag(Base):
class Artifact(Base):
__tablename__ = "artifacts"

id: int = Column(Integer, autoincrement=True, primary_key=True)
name: str = Column(Text)
author: str = Column(Text)
description: str = Column(Text)
_image_path: "str | None" = Column("image_path", Text, nullable=True)
tags: "list[Tag]" = relationship(
id: Mapped[int] = Column(Integer, autoincrement=True, primary_key=True)
name: Mapped[str] = Column(Text)
author: Mapped[str] = Column(Text)
description: Mapped[str] = Column(Text)
_image_path: Mapped[str | None] = Column("image_path", Text, nullable=True)
tags: "Mapped[list[Tag]]" = relationship(
"Tag", secondary=PluginTag, cascade="all, delete", order_by="Tag.tag", lazy="selectin"
)
versions: "list[Version]" = relationship(
versions: "Mapped[list[Version]]" = relationship(
"Version", cascade="all, delete", lazy="selectin", order_by="Version.created.desc(), Version.id.asc()"
)
visible: bool = Column(Boolean, default=True)
visible: Mapped[bool] = Column(Boolean, default=True)

downloads: int = column_property(
# Properties computed from relations
downloads: Mapped[int] = column_property(
select(func.sum(Version.downloads)).where(Version.artifact_id == id).correlate_except(Version).scalar_subquery()
)
updates: int = column_property(
updates: Mapped[int] = column_property(
select(func.sum(Version.updates)).where(Version.artifact_id == id).correlate_except(Version).scalar_subquery()
)

created: datetime = column_property(
created: Mapped[datetime] = column_property(
select(func.min(Version.created)).where(Version.artifact_id == id).correlate_except(Version).scalar_subquery()
)
updated: datetime = column_property(
updated: Mapped[datetime] = column_property(
select(func.max(Version.created)).where(Version.artifact_id == id).correlate_except(Version).scalar_subquery()
)

Expand Down
123 changes: 69 additions & 54 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ aiosqlite = "^0.17.0" # for async sqlite in sqlalchemy
asgiref = "^3.5.2"
discord-webhook = {version = "^0.17.0", extras = ["async"]}
fastapi = "^0.85.1"
pydantic = "^1.10.13"
python-multipart = "^0.0.5" # for multipart forms in fastapi
sqlalchemy = "^1.4.41"
sqlalchemy = "^2.0.32"
uvicorn = "^0.20.0"
limits = {extras = ["redis"], version = "^3.6.0"}
redis = "^5.0.1"
Expand All @@ -38,7 +39,6 @@ pytest-env = "^0.6.2"
pytest-freezer = "^0.4.8"
pytest-lazy-fixture = "^0.6.3"
pytest-mock = "^3.8.2"
sqlalchemy2-stubs = "*"
types-pytest-lazy-fixture = "^0.6.3.2"

[tool.black]
Expand Down Expand Up @@ -73,7 +73,6 @@ src_paths = ["plugin_store", "tests"]

[tool.mypy]
plugins = [
# "sqlmypy",
"sqlalchemy.ext.mypy.plugin",
]

Expand Down

0 comments on commit b4a0aa3

Please sign in to comment.