Spaces:
Running
Running
sghorbal commited on
Commit ·
1bb9c62
1
Parent(s): 8d428f9
add entities
Browse files- alembic/versions/fc681e60e977_add_entities.py +46 -0
- src/entity/match.py +41 -0
- src/entity/odds.py +24 -0
- src/entity/player.py +42 -0
alembic/versions/fc681e60e977_add_entities.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""add entities
|
| 2 |
+
|
| 3 |
+
Revision ID: fc681e60e977
|
| 4 |
+
Revises: 315d7604a82e
|
| 5 |
+
Create Date: 2025-04-17 23:17:58.236176
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from typing import Sequence, Union
|
| 9 |
+
|
| 10 |
+
from alembic import op
|
| 11 |
+
import sqlalchemy as sa
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# revision identifiers, used by Alembic.
|
| 15 |
+
revision: str = 'fc681e60e977'
|
| 16 |
+
down_revision: Union[str, None] = '315d7604a82e'
|
| 17 |
+
branch_labels: Union[str, Sequence[str], None] = None
|
| 18 |
+
depends_on: Union[str, Sequence[str], None] = None
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def upgrade() -> None:
|
| 22 |
+
"""Upgrade schema."""
|
| 23 |
+
# ### commands auto generated by Alembic - please adjust! ###
|
| 24 |
+
op.drop_constraint('caracteristics_fk_player_id_fkey', 'caracteristics', schema='data', type_='foreignkey')
|
| 25 |
+
op.create_foreign_key('caracteristics_fk_player_id_fkey', 'caracteristics', 'player', ['fk_player_id'], ['id'], source_schema='data', referent_schema='data', ondelete='CASCADE')
|
| 26 |
+
op.create_unique_constraint('uq_date_fk_winner_id_fk_loser_id', 'match', ['date', 'fk_winner_id', 'fk_loser_id'], schema='data')
|
| 27 |
+
op.drop_constraint('match_fk_winner_id_fkey', 'match', schema='data', type_='foreignkey')
|
| 28 |
+
op.drop_constraint('match_fk_loser_id_fkey', 'match', schema='data', type_='foreignkey')
|
| 29 |
+
op.create_foreign_key('match_fk_winner_id_fkey', 'match', 'player', ['fk_winner_id'], ['id'], source_schema='data', referent_schema='data', ondelete='CASCADE')
|
| 30 |
+
op.create_foreign_key('match_fk_loser_id_fkey', 'match', 'player', ['fk_loser_id'], ['id'], source_schema='data', referent_schema='data', ondelete='CASCADE')
|
| 31 |
+
op.create_foreign_key('odds_fk_match_id_fkey', 'odds', 'match', ['fk_match_id'], ['id'], source_schema='data', referent_schema='data', ondelete='CASCADE')
|
| 32 |
+
# ### end Alembic commands ###
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def downgrade() -> None:
|
| 36 |
+
"""Downgrade schema."""
|
| 37 |
+
# ### commands auto generated by Alembic - please adjust! ###
|
| 38 |
+
op.drop_constraint('odds_fk_match_id_fkey', 'odds', schema='data', type_='foreignkey')
|
| 39 |
+
op.drop_constraint('match_fk_loser_id_fkey', 'match', schema='data', type_='foreignkey')
|
| 40 |
+
op.drop_constraint('match_fk_winner_id_fkey', 'match', schema='data', type_='foreignkey')
|
| 41 |
+
op.create_foreign_key('match_fk_loser_id_fkey', 'match', 'player', ['fk_loser_id'], ['id'], source_schema='data', referent_schema='data')
|
| 42 |
+
op.create_foreign_key('match_fk_winner_id_fkey', 'match', 'player', ['fk_winner_id'], ['id'], source_schema='data', referent_schema='data')
|
| 43 |
+
op.drop_constraint('uq_date_fk_winner_id_fk_loser_id', 'match', schema='data', type_='unique')
|
| 44 |
+
op.drop_constraint('caracteristics_fk_player_id_fkey', 'caracteristics', schema='data', type_='foreignkey')
|
| 45 |
+
op.create_foreign_key('caracteristics_fk_player_id_fkey', 'caracteristics', 'player', ['fk_player_id'], ['id'], source_schema='data', referent_schema='data')
|
| 46 |
+
# ### end Alembic commands ###
|
src/entity/match.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from sqlalchemy import Integer, String, DateTime, ForeignKey, UniqueConstraint
|
| 3 |
+
from sqlalchemy.orm import relationship, mapped_column, Mapped
|
| 4 |
+
from src.entity.odds import Odds
|
| 5 |
+
from src.entity.player import Player
|
| 6 |
+
|
| 7 |
+
from . import Base
|
| 8 |
+
|
| 9 |
+
class Match(Base):
|
| 10 |
+
"""
|
| 11 |
+
Match table
|
| 12 |
+
"""
|
| 13 |
+
__tablename__ = "match"
|
| 14 |
+
__table_args__ = (
|
| 15 |
+
UniqueConstraint('date', 'fk_winner_id', 'fk_loser_id', name='uq_date_fk_winner_id_fk_loser_id'),
|
| 16 |
+
{'schema': 'data'},
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Match table columns
|
| 20 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 21 |
+
date: Mapped[DateTime] = mapped_column(DateTime, nullable=True)
|
| 22 |
+
comment: Mapped[str] = mapped_column(String, nullable=True)
|
| 23 |
+
winner_rank: Mapped[int] = mapped_column(Integer, nullable=True)
|
| 24 |
+
winner_points: Mapped[int] = mapped_column(Integer, nullable=True)
|
| 25 |
+
loser_rank: Mapped[int] = mapped_column(Integer, nullable=True)
|
| 26 |
+
loser_points: Mapped[int] = mapped_column(Integer, nullable=True)
|
| 27 |
+
tournament_name: Mapped[str] = mapped_column(String, nullable=True)
|
| 28 |
+
tournament_series: Mapped[str] = mapped_column(String, nullable=True)
|
| 29 |
+
tournament_surface: Mapped[str] = mapped_column(String, nullable=True)
|
| 30 |
+
tournament_court: Mapped[str] = mapped_column(String, nullable=True)
|
| 31 |
+
tournament_round: Mapped[str] = mapped_column(String, nullable=True)
|
| 32 |
+
tournament_location: Mapped[str] = mapped_column(String, nullable=True)
|
| 33 |
+
|
| 34 |
+
# Dependent table
|
| 35 |
+
odds: Mapped[list["Odds"]] = relationship("Odds", back_populates="match", cascade="all, delete-orphan", passive_deletes=True)
|
| 36 |
+
|
| 37 |
+
fk_winner_id: Mapped[int] = mapped_column(ForeignKey("data.player.id", ondelete="CASCADE", name='match_fk_winner_id_fkey'), nullable=False)
|
| 38 |
+
fk_loser_id: Mapped[int] = mapped_column(ForeignKey("data.player.id", ondelete="CASCADE", name='match_fk_loser_id_fkey'), nullable=False)
|
| 39 |
+
|
| 40 |
+
winner: Mapped["Player"] = relationship("Player", foreign_keys=[fk_winner_id])
|
| 41 |
+
loser: Mapped["Player"] = relationship("Player", foreign_keys=[fk_loser_id])
|
src/entity/odds.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from sqlalchemy import String, Float, ForeignKey, UniqueConstraint
|
| 3 |
+
from sqlalchemy.orm import relationship, mapped_column, Mapped
|
| 4 |
+
|
| 5 |
+
from . import Base
|
| 6 |
+
|
| 7 |
+
class Odds(Base):
|
| 8 |
+
"""
|
| 9 |
+
Odds table
|
| 10 |
+
"""
|
| 11 |
+
__tablename__ = "odds"
|
| 12 |
+
__table_args__ = (
|
| 13 |
+
UniqueConstraint('fk_match_id', 'bookmaker', name='uq_odds_match_bookmaker'),
|
| 14 |
+
{'schema': 'data', 'extend_existing': True}
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Odds table columns
|
| 18 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 19 |
+
bookmaker: Mapped[str] = mapped_column(String, nullable=True)
|
| 20 |
+
winner: Mapped[float] = mapped_column(Float, nullable=True)
|
| 21 |
+
loser: Mapped[float] = mapped_column(Float, nullable=True)
|
| 22 |
+
|
| 23 |
+
fk_match_id: Mapped[int] = mapped_column(ForeignKey("data.match.id", ondelete="CASCADE", name='odds_fk_match_id_fkey'), nullable=False)
|
| 24 |
+
match: Mapped["Match"] = relationship("Match", back_populates="odds") # type: ignore # noqa: F821
|
src/entity/player.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from datetime import date
|
| 3 |
+
from sqlalchemy import String, Float, ForeignKey
|
| 4 |
+
from sqlalchemy.orm import relationship, mapped_column, Mapped
|
| 5 |
+
from typing import List
|
| 6 |
+
|
| 7 |
+
from . import Base
|
| 8 |
+
|
| 9 |
+
class Player(Base):
|
| 10 |
+
"""
|
| 11 |
+
Player table
|
| 12 |
+
"""
|
| 13 |
+
__tablename__ = "player"
|
| 14 |
+
__table_args__ = {'schema': 'data'}
|
| 15 |
+
|
| 16 |
+
# Player table columns
|
| 17 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 18 |
+
name: Mapped[str] = mapped_column(String, nullable=True)
|
| 19 |
+
tennis_id: Mapped[str] = mapped_column(String, nullable=True)
|
| 20 |
+
|
| 21 |
+
caracteristics: Mapped["Caracteristics"] = relationship("Caracteristics", back_populates="player", cascade="all, delete-orphan")
|
| 22 |
+
|
| 23 |
+
matches_won: Mapped[List["Match"]] = relationship("Match", back_populates="winner", foreign_keys='Match.fk_winner_id')
|
| 24 |
+
matches_lost: Mapped[List["Match"]] = relationship("Match", back_populates="loser", foreign_keys='Match.fk_loser_id')
|
| 25 |
+
|
| 26 |
+
class Caracteristics(Base):
|
| 27 |
+
"""
|
| 28 |
+
Caracteristics table
|
| 29 |
+
"""
|
| 30 |
+
__tablename__ = "caracteristics"
|
| 31 |
+
__table_args__ = {'schema': 'data'}
|
| 32 |
+
|
| 33 |
+
# Caracteristics table columns
|
| 34 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 35 |
+
height: Mapped[float] = mapped_column(Float, nullable=True)
|
| 36 |
+
weight: Mapped[float] = mapped_column(Float, nullable=True)
|
| 37 |
+
handedness: Mapped[str] = mapped_column(String, nullable=True)
|
| 38 |
+
date_of_birth: Mapped[date] = mapped_column(String, nullable=True)
|
| 39 |
+
date_turned_pro: Mapped[date] = mapped_column(String, nullable=True)
|
| 40 |
+
|
| 41 |
+
fk_player_id: Mapped[int] = mapped_column(ForeignKey("data.player.id", ondelete="CASCADE", name='caracteristics_fk_player_id_fkey'), nullable=False)
|
| 42 |
+
player: Mapped["Player"] = relationship("Player", back_populates="caracteristics")
|