@@ -54,11 +54,11 @@ def init_connection_pool() -> sqlalchemy.engine.base.Engine:
5454# create 'votes' table in database if it does not already exist
5555def migrate_db (db : sqlalchemy .engine .base .Engine ) -> None :
5656 with db .connect () as conn :
57- conn .execute (
57+ conn .execute (sqlalchemy . text (
5858 "CREATE TABLE IF NOT EXISTS votes "
5959 "( vote_id SERIAL NOT NULL, time_cast timestamp NOT NULL, "
6060 "candidate VARCHAR(6) NOT NULL, PRIMARY KEY (vote_id) );"
61- )
61+ ))
6262
6363
6464# This global variable is declared with a value of `None`, instead of calling
@@ -96,9 +96,9 @@ def get_index_context(db: sqlalchemy.engine.base.Engine) -> Dict:
9696
9797 with db .connect () as conn :
9898 # Execute the query and fetch all results
99- recent_votes = conn .execute (
99+ recent_votes = conn .execute (sqlalchemy . text (
100100 "SELECT candidate, time_cast FROM votes ORDER BY time_cast DESC LIMIT 5"
101- ).fetchall ()
101+ )) .fetchall ()
102102 # Convert the results into a list of dicts representing votes
103103 for row in recent_votes :
104104 votes .append ({"candidate" : row [0 ], "time_cast" : row [1 ]})
@@ -107,11 +107,9 @@ def get_index_context(db: sqlalchemy.engine.base.Engine) -> Dict:
107107 "SELECT COUNT(vote_id) FROM votes WHERE candidate=:candidate"
108108 )
109109 # Count number of votes for tabs
110- tab_result = conn .execute (stmt , candidate = "TABS" ).fetchone ()
111- tab_count = tab_result [0 ]
110+ tab_count = conn .execute (stmt , parameters = {"candidate" : "TABS" }).scalar ()
112111 # Count number of votes for spaces
113- space_result = conn .execute (stmt , candidate = "SPACES" ).fetchone ()
114- space_count = space_result [0 ]
112+ space_count = conn .execute (stmt , parameters = {"candidate" : "SPACES" }).scalar ()
115113
116114 return {
117115 "space_count" : space_count ,
@@ -140,7 +138,7 @@ def save_vote(db: sqlalchemy.engine.base.Engine, team: str) -> Response:
140138 # Using a with statement ensures that the connection is always released
141139 # back into the pool at the end of statement (even if an error occurs)
142140 with db .connect () as conn :
143- conn .execute (stmt , time_cast = time_cast , candidate = team )
141+ conn .execute (stmt , parameters = { " time_cast" : time_cast , " candidate" : team } )
144142 except Exception as e :
145143 # If something goes wrong, handle the error in this section. This might
146144 # involve retrying or adjusting parameters depending on the situation.
0 commit comments