Skip to content

Commit

Permalink
Merge pull request #1094 from lsst-sqre/tickets/DM-46034a
Browse files Browse the repository at this point in the history
DM-46034: Convert to the new SQLAlchemy ORM syntax
  • Loading branch information
rra authored Sep 11, 2024
2 parents 81a9a4b + c61964e commit a706edd
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 54 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies = [
"pyyaml",
"redis>=4.2.0",
"safir[db,kubernetes]>=6.2.0",
"sqlalchemy",
"sqlalchemy>=2.0.0",
"structlog",
]
dynamic = ["version"]
Expand Down
5 changes: 3 additions & 2 deletions src/gafaelfawr/schema/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from __future__ import annotations

from sqlalchemy import Column, String
from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column

from .base import Base

Expand All @@ -18,4 +19,4 @@ class Admin(Base):

__tablename__ = "admin"

username = Column(String(64), primary_key=True)
username: Mapped[str] = mapped_column(String(64), primary_key=True)
17 changes: 9 additions & 8 deletions src/gafaelfawr/schema/admin_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

from datetime import datetime

from sqlalchemy import Column, DateTime, Enum, Index, Integer, String
from sqlalchemy import Index, String
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import Mapped, mapped_column

from ..models.history import AdminChange
from .base import Base
Expand All @@ -22,13 +23,13 @@ class AdminHistory(Base):

__tablename__ = "admin_history"

id: int = Column(Integer, primary_key=True)
username: str = Column(String(64), nullable=False)
action: AdminChange = Column(Enum(AdminChange), nullable=False)
actor: str = Column(String(64), nullable=False)
ip_address: str = Column(
String(64).with_variant(postgresql.INET, "postgresql"), nullable=False
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
username: Mapped[str] = mapped_column(String(64))
action: Mapped[AdminChange]
actor: Mapped[str] = mapped_column(String(64))
ip_address: Mapped[str] = mapped_column(
String(64).with_variant(postgresql.INET, "postgresql")
)
event_time: datetime = Column(DateTime, nullable=False)
event_time: Mapped[datetime]

__table_args__ = (Index("admin_history_by_time", "event_time", "id"),)
6 changes: 4 additions & 2 deletions src/gafaelfawr/schema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from __future__ import annotations

from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import DeclarativeBase

__all__ = ["Base"]

Base = declarative_base()

class Base(DeclarativeBase):
"""Declarative base for the Gafaelfawr database schema."""
7 changes: 4 additions & 3 deletions src/gafaelfawr/schema/subtoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from __future__ import annotations

from sqlalchemy import Column, ForeignKey, Index, String
from sqlalchemy import ForeignKey, Index, String
from sqlalchemy.orm import Mapped, mapped_column

from .base import Base

Expand All @@ -14,12 +15,12 @@ class Subtoken(Base):

__tablename__ = "subtoken"

child: str = Column(
child: Mapped[str] = mapped_column(
String(64),
ForeignKey("token.token", ondelete="CASCADE"),
primary_key=True,
)
parent: str | None = Column(
parent: Mapped[str | None] = mapped_column(
String(64), ForeignKey("token.token", ondelete="SET NULL")
)

Expand Down
23 changes: 13 additions & 10 deletions src/gafaelfawr/schema/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from datetime import datetime

from sqlalchemy import Column, DateTime, Enum, Index, String, UniqueConstraint
from sqlalchemy import Index, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column

from ..models.token import TokenType
from .base import Base
Expand All @@ -17,15 +18,17 @@ class Token(Base):

__tablename__ = "token"

token: str = Column(String(64, collation="C"), primary_key=True)
username: str = Column(String(64), nullable=False)
token_type: TokenType = Column(Enum(TokenType), nullable=False)
token_name: str | None = Column(String(64))
scopes: str = Column(String(512), nullable=False)
service: str | None = Column(String(64))
created: datetime = Column(DateTime, nullable=False)
last_used: datetime | None = Column(DateTime)
expires: datetime | None = Column(DateTime)
token: Mapped[str] = mapped_column(
String(64, collation="C"), primary_key=True
)
username: Mapped[str] = mapped_column(String(64))
token_type: Mapped[TokenType]
token_name: Mapped[str | None] = mapped_column(String(64))
scopes: Mapped[str] = mapped_column(String(512))
service: Mapped[str | None] = mapped_column(String(64))
created: Mapped[datetime]
last_used: Mapped[datetime | None]
expires: Mapped[datetime | None]

__table_args__ = (
UniqueConstraint("username", "token_name"),
Expand Down
23 changes: 12 additions & 11 deletions src/gafaelfawr/schema/token_auth_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

from datetime import datetime

from sqlalchemy import Column, DateTime, Enum, Index, Integer, String
from sqlalchemy import Index, String
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import Mapped, mapped_column

from ..models.token import TokenType
from .base import Base
Expand All @@ -18,18 +19,18 @@ class TokenAuthHistory(Base):

__tablename__ = "token_auth_history"

id: int = Column(Integer, primary_key=True)
token: str = Column(String(64), nullable=False)
username: str = Column(String(64), nullable=False)
token_type: TokenType = Column(Enum(TokenType), nullable=False)
token_name: str | None = Column(String(64))
parent: str | None = Column(String(64))
scopes: str | None = Column(String(512))
service: str | None = Column(String(64))
ip_address: str | None = Column(
id: Mapped[int] = mapped_column(primary_key=True)
token: Mapped[str] = mapped_column(String(64))
username: Mapped[str] = mapped_column(String(64))
token_type: Mapped[TokenType]
token_name: Mapped[str | None] = mapped_column(String(64))
parent: Mapped[str | None] = mapped_column(String(64))
scopes: Mapped[str | None] = mapped_column(String(512))
service: Mapped[str | None] = mapped_column(String(64))
ip_address: Mapped[str | None] = mapped_column(
String(64).with_variant(postgresql.INET, "postgresql")
)
event_time: datetime = Column(DateTime, nullable=False)
event_time: Mapped[datetime]

__table_args__ = (
Index("token_auth_history_by_time", "event_time", "id"),
Expand Down
35 changes: 18 additions & 17 deletions src/gafaelfawr/schema/token_change_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

from datetime import datetime

from sqlalchemy import Column, DateTime, Enum, Index, Integer, String
from sqlalchemy import Index, String
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import Mapped, mapped_column

from ..models.history import TokenChange
from ..models.token import TokenType
Expand All @@ -19,24 +20,24 @@ class TokenChangeHistory(Base):

__tablename__ = "token_change_history"

id: int = Column(Integer, primary_key=True)
token: str = Column(String(64), nullable=False)
username: str = Column(String(64), nullable=False)
token_type: TokenType = Column(Enum(TokenType), nullable=False)
token_name: str | None = Column(String(64))
parent: str = Column(String(64))
scopes: str = Column(String(512), nullable=False)
service: str | None = Column(String(64))
expires: datetime | None = Column(DateTime)
actor: str | None = Column(String(64))
action: TokenChange = Column(Enum(TokenChange), nullable=False)
old_token_name: str | None = Column(String(64))
old_scopes: str | None = Column(String(512))
old_expires: datetime | None = Column(DateTime)
ip_address: str | None = Column(
id: Mapped[int] = mapped_column(primary_key=True)
token: Mapped[str] = mapped_column(String(64))
username: Mapped[str] = mapped_column(String(64))
token_type: Mapped[TokenType]
token_name: Mapped[str | None] = mapped_column(String(64))
parent: Mapped[str | None] = mapped_column(String(64))
scopes: Mapped[str] = mapped_column(String(512))
service: Mapped[str | None] = mapped_column(String(64))
expires: Mapped[datetime | None]
actor: Mapped[str | None] = mapped_column(String(64))
action: Mapped[TokenChange]
old_token_name: Mapped[str | None] = mapped_column(String(64))
old_scopes: Mapped[str | None] = mapped_column(String(512))
old_expires: Mapped[datetime | None]
ip_address: Mapped[str | None] = mapped_column(
String(64).with_variant(postgresql.INET, "postgresql")
)
event_time: datetime = Column(DateTime, nullable=False)
event_time: Mapped[datetime]

__table_args__ = (
Index("token_change_history_by_time", "event_time", "id"),
Expand Down

0 comments on commit a706edd

Please sign in to comment.