Skip to content

Commit

Permalink
chore: adjustments after running "pre-commit run --all-files"
Browse files Browse the repository at this point in the history
  • Loading branch information
ayharano committed Jan 23, 2024
1 parent a776203 commit fb883af
Show file tree
Hide file tree
Showing 36 changed files with 1,684 additions and 1,371 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

Expand Down
6 changes: 3 additions & 3 deletions docs/architecture/decisions/0003-data-modeling.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ erDiagram
int finalMatch FK "Reference to the final match"
int thirdPlaceMatch FK "Reference to the third place match"
}
Match {
int id PK "Internal identifier"
uuid uuid UK "External identifier using UUID"
Expand All @@ -103,13 +103,13 @@ erDiagram
int winner FK "Reference to the winner competitor"
int loser FK "Reference to the loser competitor"
}
TournamentCompetitor {
int tournament PK, FK
int competitor PK, FK
timestamp created "Internal timestamp to track an instance creation"
timestamp updated "Internal timestamp to track an instance creation"
int nextMatch FK "Reference to the next match the competitor will compete in the tournament"
int nextMatch FK "Reference to the next match the competitor will compete in the tournament"
}
Competitor only one -- zero or more TournamentCompetitor : is
Expand Down
2 changes: 1 addition & 1 deletion migrations/README
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Generic single-database configuration.
Generic single-database configuration.
13 changes: 4 additions & 9 deletions migrations/env.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context
from sqlalchemy import engine_from_config, pool

from matamata.settings import settings
from matamata.models import Base

from matamata.settings import settings

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
config.set_main_option('sqlalchemy.url', settings.DATABASE_URL)
config.set_main_option("sqlalchemy.url", settings.DATABASE_URL)

# Interpret the config file for Python logging.
# This line sets up loggers basically.
Expand Down Expand Up @@ -69,9 +66,7 @@ def run_migrations_online() -> None:
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
context.configure(connection=connection, target_metadata=target_metadata)

with context.begin_transaction():
context.run_migrations()
Expand Down
187 changes: 120 additions & 67 deletions migrations/versions/ad6bf02d324d_initial_tables.py
Original file line number Diff line number Diff line change
@@ -1,95 +1,148 @@
"""initial tables
Revision ID: ad6bf02d324d
Revises:
Revises:
Create Date: 2024-01-11 12:28:20.802398
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = 'ad6bf02d324d'
revision: str = "ad6bf02d324d"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('competitor',
sa.Column('label', sa.String(length=255), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('uuid', sa.Uuid(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.Column('updated', sa.DateTime(), nullable=False),
sa.CheckConstraint("NOT(TRIM(label) LIKE '')", name='competitor_label_not_empty_nor_whitespace_only'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('uuid')
op.create_table(
"competitor",
sa.Column("label", sa.String(length=255), nullable=False),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("uuid", sa.Uuid(), nullable=False),
sa.Column("created", sa.DateTime(), nullable=False),
sa.Column("updated", sa.DateTime(), nullable=False),
sa.CheckConstraint(
"NOT(TRIM(label) LIKE '')",
name="competitor_label_not_empty_nor_whitespace_only",
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("uuid"),
)
op.create_table('tournament',
sa.Column('label', sa.String(length=255), nullable=False),
sa.Column('matches_creation', sa.DateTime(), nullable=True),
sa.Column('number_competitors', sa.Integer(), nullable=True),
sa.Column('starting_round', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('uuid', sa.Uuid(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.Column('updated', sa.DateTime(), nullable=False),
sa.CheckConstraint("NOT(TRIM(label) LIKE '')", name='tournament_label_not_empty_nor_whitespace_only'),
sa.CheckConstraint('( matches_creation IS NULL AND number_competitors IS NULL AND starting_round IS NULL) OR ( matches_creation IS NOT NULL AND number_competitors IS NOT NULL AND starting_round IS NOT NULL AND number_competitors >= 1 AND starting_round >= 0)', name='tournament_all_null_or_all_set_under_conditions'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('uuid')
op.create_table(
"tournament",
sa.Column("label", sa.String(length=255), nullable=False),
sa.Column("matches_creation", sa.DateTime(), nullable=True),
sa.Column("number_competitors", sa.Integer(), nullable=True),
sa.Column("starting_round", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("uuid", sa.Uuid(), nullable=False),
sa.Column("created", sa.DateTime(), nullable=False),
sa.Column("updated", sa.DateTime(), nullable=False),
sa.CheckConstraint(
"NOT(TRIM(label) LIKE '')",
name="tournament_label_not_empty_nor_whitespace_only",
),
sa.CheckConstraint(
"( matches_creation IS NULL AND number_competitors IS NULL AND starting_round IS NULL) OR ( matches_creation IS NOT NULL AND number_competitors IS NOT NULL AND starting_round IS NOT NULL AND number_competitors >= 1 AND starting_round >= 0)",
name="tournament_all_null_or_all_set_under_conditions",
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("uuid"),
)
op.create_table('match',
sa.Column('tournament_id', sa.Integer(), nullable=False),
sa.Column('round', sa.Integer(), nullable=False),
sa.Column('position', sa.Integer(), nullable=False),
sa.Column('competitor_a_id', sa.Integer(), nullable=True),
sa.Column('competitor_b_id', sa.Integer(), nullable=True),
sa.Column('result_registration', sa.DateTime(), nullable=True),
sa.Column('winner_id', sa.Integer(), nullable=True),
sa.Column('loser_id', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('uuid', sa.Uuid(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.Column('updated', sa.DateTime(), nullable=False),
sa.CheckConstraint('( competitor_a_id IS NULL AND competitor_b_id IS NULL) OR ( competitor_a_id <> competitor_b_id)', name='match_non_null_competitors_cannot_be_the_same'),
sa.CheckConstraint('( result_registration IS NULL AND loser_id is NULL) OR ( result_registration IS NOT NULL AND loser_id is NULL) OR ( result_registration IS NOT NULL AND loser_id IS NOT NULL)', name='match_result_registration_loser'),
sa.CheckConstraint('( result_registration IS NULL AND winner_id is NULL) OR ( result_registration IS NOT NULL AND winner_id IS NOT NULL)', name='match_result_registration_winner'),
sa.CheckConstraint('( round = 0 AND position < 2) OR ( round > 0 AND position < pow(2, round))', name='match_round_position_values'),
sa.CheckConstraint('position >= 0', name='match_position_non_negative'),
sa.CheckConstraint('round >= 0', name='match_round_non_negative'),
sa.ForeignKeyConstraint(['competitor_a_id'], ['competitor.id'], ),
sa.ForeignKeyConstraint(['competitor_b_id'], ['competitor.id'], ),
sa.ForeignKeyConstraint(['loser_id'], ['competitor.id'], ),
sa.ForeignKeyConstraint(['tournament_id'], ['tournament.id'], ),
sa.ForeignKeyConstraint(['winner_id'], ['competitor.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('tournament_id', 'round', 'position', name='match_tournament_round_position_unique'),
sa.UniqueConstraint('uuid')
op.create_table(
"match",
sa.Column("tournament_id", sa.Integer(), nullable=False),
sa.Column("round", sa.Integer(), nullable=False),
sa.Column("position", sa.Integer(), nullable=False),
sa.Column("competitor_a_id", sa.Integer(), nullable=True),
sa.Column("competitor_b_id", sa.Integer(), nullable=True),
sa.Column("result_registration", sa.DateTime(), nullable=True),
sa.Column("winner_id", sa.Integer(), nullable=True),
sa.Column("loser_id", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("uuid", sa.Uuid(), nullable=False),
sa.Column("created", sa.DateTime(), nullable=False),
sa.Column("updated", sa.DateTime(), nullable=False),
sa.CheckConstraint(
"( competitor_a_id IS NULL AND competitor_b_id IS NULL) OR ( competitor_a_id <> competitor_b_id)",
name="match_non_null_competitors_cannot_be_the_same",
),
sa.CheckConstraint(
"( result_registration IS NULL AND loser_id is NULL) OR ( result_registration IS NOT NULL AND loser_id is NULL) OR ( result_registration IS NOT NULL AND loser_id IS NOT NULL)",
name="match_result_registration_loser",
),
sa.CheckConstraint(
"( result_registration IS NULL AND winner_id is NULL) OR ( result_registration IS NOT NULL AND winner_id IS NOT NULL)",
name="match_result_registration_winner",
),
sa.CheckConstraint(
"( round = 0 AND position < 2) OR ( round > 0 AND position < pow(2, round))",
name="match_round_position_values",
),
sa.CheckConstraint("position >= 0", name="match_position_non_negative"),
sa.CheckConstraint("round >= 0", name="match_round_non_negative"),
sa.ForeignKeyConstraint(
["competitor_a_id"],
["competitor.id"],
),
sa.ForeignKeyConstraint(
["competitor_b_id"],
["competitor.id"],
),
sa.ForeignKeyConstraint(
["loser_id"],
["competitor.id"],
),
sa.ForeignKeyConstraint(
["tournament_id"],
["tournament.id"],
),
sa.ForeignKeyConstraint(
["winner_id"],
["competitor.id"],
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint(
"tournament_id",
"round",
"position",
name="match_tournament_round_position_unique",
),
sa.UniqueConstraint("uuid"),
)
op.create_table('tournament_competitor',
sa.Column('tournament_id', sa.Integer(), nullable=False),
sa.Column('competitor_id', sa.Integer(), nullable=False),
sa.Column('next_match_id', sa.Integer(), nullable=True),
sa.Column('created', sa.DateTime(), nullable=False),
sa.Column('updated', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['competitor_id'], ['competitor.id'], ),
sa.ForeignKeyConstraint(['next_match_id'], ['match.id'], ),
sa.ForeignKeyConstraint(['tournament_id'], ['tournament.id'], ),
sa.PrimaryKeyConstraint('tournament_id', 'competitor_id')
op.create_table(
"tournament_competitor",
sa.Column("tournament_id", sa.Integer(), nullable=False),
sa.Column("competitor_id", sa.Integer(), nullable=False),
sa.Column("next_match_id", sa.Integer(), nullable=True),
sa.Column("created", sa.DateTime(), nullable=False),
sa.Column("updated", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(
["competitor_id"],
["competitor.id"],
),
sa.ForeignKeyConstraint(
["next_match_id"],
["match.id"],
),
sa.ForeignKeyConstraint(
["tournament_id"],
["tournament.id"],
),
sa.PrimaryKeyConstraint("tournament_id", "competitor_id"),
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('tournament_competitor')
op.drop_table('match')
op.drop_table('tournament')
op.drop_table('competitor')
op.drop_table("tournament_competitor")
op.drop_table("match")
op.drop_table("tournament")
op.drop_table("competitor")
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion src/matamata/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.1.1
2024.1.1
4 changes: 2 additions & 2 deletions src/matamata/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = (
__import__('pathlib').Path(__file__).parent / 'VERSION'
).read_text().strip()
(__import__("pathlib").Path(__file__).parent / "VERSION").read_text().strip()
)
1 change: 0 additions & 1 deletion src/matamata/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from matamata.settings import settings


engine = create_engine(settings.DATABASE_URL)


Expand Down
7 changes: 2 additions & 5 deletions src/matamata/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
from . import __version__ as VERSION
from .routers import competitor, match, tournament


app = FastAPI(
title='matamata',
summary=(
'REST API for single-elimination tournament management'
),
title="matamata",
summary=("REST API for single-elimination tournament management"),
version=VERSION,
)

Expand Down
13 changes: 12 additions & 1 deletion src/matamata/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
from .base import Base, TimestampedBase, IdUuidBase, IdUuidTimestampedBase
from .base import Base, IdUuidBase, IdUuidTimestampedBase, TimestampedBase
from .competitor import Competitor
from .match import Match
from .tournament import Tournament
from .tournament_competitor import TournamentCompetitor

__all__ = [
"Base",
"Competitor",
"IdUuidBase",
"IdUuidTimestampedBase",
"Match",
"TimestampedBase",
"Tournament",
"TournamentCompetitor",
]
18 changes: 9 additions & 9 deletions src/matamata/models/competitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@generic_repr
class Competitor(IdUuidTimestampedBase):
__tablename__ = 'competitor'
__tablename__ = "competitor"
__table_args__ = (
CheckConstraint(
"NOT(TRIM(label) LIKE '')",
Expand All @@ -23,15 +23,15 @@ class Competitor(IdUuidTimestampedBase):
label: Mapped[str] = mapped_column(String(255))

tournament_associations: Mapped[list[TournamentCompetitor]] = relationship(
cascade='all, delete-orphan',
overlaps='competitor',
cascade="all, delete-orphan",
overlaps="competitor",
)
tournaments: AssociationProxy[list['Tournament']] = association_proxy(
'tournament_associations',
'tournament',
tournaments: AssociationProxy[list["Tournament"]] = association_proxy( # noqa: F821
"tournament_associations",
"tournament",
creator=lambda tournament_: TournamentCompetitor(tournament=tournament_),
)
next_matches: AssociationProxy[list['Match']] = association_proxy(
'tournament_associations',
'next_match',
next_matches: AssociationProxy[list["Match"]] = association_proxy( # noqa: F821
"tournament_associations",
"next_match",
)
24 changes: 14 additions & 10 deletions src/matamata/models/constants.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
COMPETITOR_LABEL_CONSTRAINT = 'competitor_label_not_empty_nor_whitespace_only'
MATCH_ROUND_CONSTRAINT = 'match_round_non_negative'
MATCH_ROUND_POSITION_CONSTRAINT = 'match_round_position_values'
MATCH_POSITION_CONSTRAINT = 'match_position_non_negative'
MATCH_TOURNAMENT_ROUND_POSITION_UNIQUE_CONSTRAINT = 'match_tournament_round_position_unique'
MATCH_NON_NULL_COMPETITORS_CANNOT_BE_THE_SAME = 'match_non_null_competitors_cannot_be_the_same'
MATCH_RESULT_REGISTRATION_MUST_REGISTER_A_WINNER = 'match_result_registration_winner'
MATCH_RESULT_REGISTRATION_MIGHT_REGISTER_A_LOSER = 'match_result_registration_loser'
TOURNAMENT_LABEL_CONSTRAINT = 'tournament_label_not_empty_nor_whitespace_only'
TOURNAMENT_START_ATTRS_CONSTRAINT = 'tournament_all_null_or_all_set_under_conditions'
COMPETITOR_LABEL_CONSTRAINT = "competitor_label_not_empty_nor_whitespace_only"
MATCH_ROUND_CONSTRAINT = "match_round_non_negative"
MATCH_ROUND_POSITION_CONSTRAINT = "match_round_position_values"
MATCH_POSITION_CONSTRAINT = "match_position_non_negative"
MATCH_TOURNAMENT_ROUND_POSITION_UNIQUE_CONSTRAINT = (
"match_tournament_round_position_unique"
)
MATCH_NON_NULL_COMPETITORS_CANNOT_BE_THE_SAME = (
"match_non_null_competitors_cannot_be_the_same"
)
MATCH_RESULT_REGISTRATION_MUST_REGISTER_A_WINNER = "match_result_registration_winner"
MATCH_RESULT_REGISTRATION_MIGHT_REGISTER_A_LOSER = "match_result_registration_loser"
TOURNAMENT_LABEL_CONSTRAINT = "tournament_label_not_empty_nor_whitespace_only"
TOURNAMENT_START_ATTRS_CONSTRAINT = "tournament_all_null_or_all_set_under_conditions"
Loading

0 comments on commit fb883af

Please sign in to comment.