Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Commit

Permalink
🗃️ Add basic models
Browse files Browse the repository at this point in the history
  • Loading branch information
Paillat-dev committed Jul 24, 2024
1 parent d0f53b5 commit 8c55464
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 1 deletion.
4 changes: 3 additions & 1 deletion alembic-migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""${message}
# ruff: noqa: D103, D212, D400, D415, INP001
"""
${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ reportUnknownMemberType = false
reportUnknownParameterType = false
reportUnknownLambdaType = false

executionEnvironments = [
{ root = "src/db_tables", reportImportCycles = false },
]

[tool.pytest.ini_options]
minversion = "6.0"
asyncio_mode = "auto"
Expand Down
15 changes: 15 additions & 0 deletions src/db_tables/_media.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column

# Prevent circular imports for relationships


class Media:
"""Represents a media object in the database. DO NOT USE DIRECTLY."""

id: Mapped[int] = mapped_column(primary_key=True, index=True, nullable=False)

favorite: Mapped[bool] = mapped_column(default=False)
watched: Mapped[bool] = mapped_column(default=False)

user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), nullable=False)
25 changes: 25 additions & 0 deletions src/db_tables/episode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import TYPE_CHECKING

from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.utils.database import Base

# Prevent circular imports for relationships
if TYPE_CHECKING:
from src.db_tables.show import Show
from src.db_tables.user import User


class Episode(Base):
"""User database table."""

__tablename__ = "episode"

id: Mapped[int] = mapped_column(primary_key=True, index=True, nullable=False)

show_id: Mapped[int] = mapped_column(ForeignKey("user.id"), nullable=False)
tvdb_id: Mapped[int] = mapped_column(nullable=False)

show: Mapped["Show"] = relationship(lazy="selectin", back_populates="episodes")
user: Mapped["User"] = relationship(lazy="selectin", back_populates="shows")
17 changes: 17 additions & 0 deletions src/db_tables/movie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import TYPE_CHECKING

from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.db_tables._media import Media
from src.utils.database import Base

if TYPE_CHECKING:
from src.db_tables.user import User


class Movie(Media, Base):
"""User database table."""

user: Mapped["User"] = relationship(lazy="selectin", back_populates="movies")
tvdb_id: Mapped[int] = mapped_column(nullable=False)
__tablename__ = "movie"
26 changes: 26 additions & 0 deletions src/db_tables/show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import TYPE_CHECKING

from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.db_tables._media import Media
from src.utils.database import Base

if TYPE_CHECKING:
from src.db_tables.episode import Episode
from src.db_tables.user import User


class Show(Media, Base):
"""User database table."""

__tablename__ = "show"

user: Mapped["User"] = relationship(lazy="selectin", back_populates="shows")
tvdb_id: Mapped[int] = mapped_column(nullable=False)

# contains episodes whose watch state is different from the show's
episodes: Mapped[list["Episode"]] = relationship(
lazy="selectin",
back_populates="show",
cascade="all, delete-orphan",
)
30 changes: 30 additions & 0 deletions src/db_tables/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import TYPE_CHECKING

from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.utils.database import Base

# Prevent circular imports for relationships
if TYPE_CHECKING:
from src.db_tables.movie import Movie
from src.db_tables.show import Show


class User(Base):
"""User database table."""

__tablename__ = "user"

id: Mapped[int] = mapped_column(primary_key=True, index=True, nullable=False)

shows: Mapped[list["Show"]] = relationship(
lazy="selectin",
back_populates="user",
cascade="all, delete-orphan",
)

movies: Mapped[list["Movie"]] = relationship(
lazy="selectin",
back_populates="user",
cascade="all, delete-orphan",
)

0 comments on commit 8c55464

Please sign in to comment.