Spaces:
Running
Running
sghorbal commited on
Commit ·
3e66851
1
Parent(s): 97e590c
create a view for listing tournaments
Browse files
alembic/versions/141ff488c041_tournament_view_generation.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""view generation
|
| 2 |
+
|
| 3 |
+
Revision ID: 141ff488c041
|
| 4 |
+
Revises: 1891eeeaa700
|
| 5 |
+
Create Date: 2025-04-20 18:53:07.272272
|
| 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 = '141ff488c041'
|
| 16 |
+
down_revision: Union[str, None] = '1891eeeaa700'
|
| 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 |
+
sql_view = """
|
| 24 |
+
CREATE MATERIALIZED VIEW IF NOT EXISTS data.tournaments_list_m_view
|
| 25 |
+
TABLESPACE pg_default
|
| 26 |
+
AS
|
| 27 |
+
SELECT tournament_name AS name,
|
| 28 |
+
min(date_part('year'::text, date)) AS first_year,
|
| 29 |
+
max(date_part('year'::text, date)) AS last_year
|
| 30 |
+
FROM data.match
|
| 31 |
+
WHERE tournament_name IS NOT NULL
|
| 32 |
+
GROUP BY tournament_name
|
| 33 |
+
ORDER BY (lower(tournament_name::text))
|
| 34 |
+
WITH DATA;
|
| 35 |
+
|
| 36 |
+
ALTER TABLE IF EXISTS data.tournaments_list_m_view
|
| 37 |
+
OWNER TO tennis_admin;
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
sql_index = """
|
| 41 |
+
CREATE INDEX IF NOT EXISTS idx_tournaments_list_m_view_name
|
| 42 |
+
ON data.tournaments_list_m_view USING btree
|
| 43 |
+
(name COLLATE pg_catalog."default" ASC NULLS LAST)
|
| 44 |
+
WITH (deduplicate_items=True)
|
| 45 |
+
TABLESPACE pg_default;
|
| 46 |
+
"""
|
| 47 |
+
op.execute(sa.text(sql_view))
|
| 48 |
+
op.execute(sa.text(sql_index))
|
| 49 |
+
# ### end Alembic commands ###
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def downgrade() -> None:
|
| 53 |
+
"""Downgrade schema."""
|
| 54 |
+
|
| 55 |
+
sql_view = """
|
| 56 |
+
DROP MATERIALIZED VIEW IF EXISTS data.tournaments_list_m_view;
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
sql_index = """
|
| 60 |
+
DROP INDEX IF EXISTS data.idx_tournaments_list_m_view_name;
|
| 61 |
+
"""
|
| 62 |
+
op.execute(sa.text(sql_view))
|
| 63 |
+
op.execute(sa.text(sql_index))
|
| 64 |
+
# ### end Alembic commands ###
|
src/main.py
CHANGED
|
@@ -219,29 +219,18 @@ async def list_tournament_names(
|
|
| 219 |
"""
|
| 220 |
List all the tournament names and first and last year of occurrence
|
| 221 |
"""
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
tournaments =
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
"first_year": first_year[0].year,
|
| 235 |
-
"last_year": last_year[0].year
|
| 236 |
-
})
|
| 237 |
-
else:
|
| 238 |
-
output.append({
|
| 239 |
-
"name": tournament,
|
| 240 |
-
"first_year": None,
|
| 241 |
-
"last_year": None
|
| 242 |
-
})
|
| 243 |
-
|
| 244 |
-
return output
|
| 245 |
|
| 246 |
# Get a player
|
| 247 |
@app.get("/player/{player_id}", tags=["player"], description="Get a player from the database", response_model=PlayerApiDetail)
|
|
|
|
| 219 |
"""
|
| 220 |
List all the tournament names and first and last year of occurrence
|
| 221 |
"""
|
| 222 |
+
tournaments = session.execute(text("SELECT t.name, t.first_year, t.last_year FROM data.tournaments_list_m_view AS t")).all()
|
| 223 |
+
|
| 224 |
+
tournaments = [
|
| 225 |
+
{
|
| 226 |
+
"name": tournament[0],
|
| 227 |
+
"first_year": tournament[1],
|
| 228 |
+
"last_year": tournament[2]
|
| 229 |
+
}
|
| 230 |
+
for tournament in tournaments
|
| 231 |
+
]
|
| 232 |
+
|
| 233 |
+
return tournaments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
# Get a player
|
| 236 |
@app.get("/player/{player_id}", tags=["player"], description="Get a player from the database", response_model=PlayerApiDetail)
|