Skip to content

Commit 996298c

Browse files
committed
resolved PR comments
1 parent 88c0528 commit 996298c

File tree

6 files changed

+20
-29
lines changed

6 files changed

+20
-29
lines changed

api/src/database/database.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from sqlalchemy.orm import sessionmaker
1212
import logging
1313

14-
lock = threading.Lock()
15-
1614

1715
def generate_unique_id() -> str:
1816
"""
@@ -112,7 +110,8 @@ def __init__(self, echo_sql=False):
112110
database_url = os.getenv("FEEDS_DATABASE_URL")
113111
if database_url is None:
114112
raise Exception("Database URL not provided.")
115-
self.engine = create_engine(database_url, echo=echo_sql, pool_size=10, max_overflow=0)
113+
self.pool_size = int(os.getenv("DB_POOL_SIZE", 10))
114+
self.engine = create_engine(database_url, echo=echo_sql, pool_size=self.pool_size, max_overflow=0)
116115
# creates a session factory
117116
self.Session = sessionmaker(bind=self.engine, autoflush=False)
118117

@@ -154,17 +153,19 @@ def select(
154153
group_by: Callable = None,
155154
):
156155
"""
157-
Executes a query on the database
158-
:param model: the sqlalchemy model to query
159-
:param query: the sqlalchemy ORM query execute
160-
:param conditions: list of conditions (filters for the query)
161-
:param attributes: list of model's attribute names that you want to fetch. If not given, fetches all attributes.
162-
:param update_session: option to update session before running the query (defaults to True)
163-
:param limit: the optional number of rows to limit the query with
164-
:param offset: the optional number of rows to offset the query with
165-
:param group_by: an optional function, when given query results will group by return value of group_by function.
166-
Query needs to order the return values by the key being grouped by
167-
:return: None if database is inaccessible, the results of the query otherwise
156+
Executes a query on the database.
157+
158+
:param session: The SQLAlchemy session object used to interact with the database.
159+
:param model: The SQLAlchemy model to query. If not provided, the query parameter must be given.
160+
:param query: The SQLAlchemy ORM query to execute. If not provided, a query will be created using the model.
161+
:param conditions: A list of conditions (filters) to apply to the query. Each condition should be a SQLAlchemy
162+
expression.
163+
:param attributes: A list of model's attribute names to fetch. If not provided, all attributes will be fetched.
164+
:param limit: An optional integer to limit the number of rows returned by the query.
165+
:param offset: An optional integer to offset the number of rows returned by the query.
166+
:param group_by: An optional function to group the query results by the return value of the function. The query
167+
needs to order the return values by the key being grouped by.
168+
:return: None if the database is inaccessible, otherwise the results of the query.
168169
"""
169170
try:
170171
if query is None:

api/tests/test_utils/db_utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,5 @@ def empty_database(db, url):
210210
delete_stmt = delete(table)
211211
session.execute(delete_stmt)
212212

213-
session.commit()
214-
215213
except Exception as error:
216214
logging.error(f"Error while deleting from test db: {error}")

functions-python/feed_sync_process_transitland/tests/test_feed_processor_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
get_tlnd_authentication_type,
1010
create_new_feed,
1111
)
12-
from helpers.database import start_db_session, configure_polymorphic_mappers
12+
from helpers.database import configure_polymorphic_mappers
1313
from helpers.feed_sync.models import TransitFeedSyncPayload
14-
from test_utils.database_utils import default_db_url
14+
from test_utils.database_utils import default_db_url, get_testing_session
1515

1616

1717
@patch("requests.head")
@@ -68,7 +68,7 @@ def test_create_new_feed_gtfs_rt():
6868
}
6969
feed_payload = TransitFeedSyncPayload(**payload)
7070
configure_polymorphic_mappers()
71-
session = start_db_session(default_db_url, echo=False)
71+
session = get_testing_session()
7272
new_feed = create_new_feed(session, "tld-102_tu", feed_payload)
7373
session.delete(new_feed)
7474
assert new_feed.stable_id == "tld-102_tu"

functions-python/feed_sync_process_transitland/tests/test_feed_sync_process.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,7 @@ def test_process_feed_event_pubsub_error(
300300
mock_session
301301
)
302302

303-
with patch(
304-
"feed_sync_process_transitland.src.main.start_db_session",
305-
return_value=mock_session,
306-
):
307-
process_feed_event(cloud_event)
303+
process_feed_event(cloud_event)
308304

309305
def test_process_feed_event_malformed_cloud_event(self, mock_logging):
310306
"""Test feed event processing with malformed cloud event."""

functions-python/helpers/database.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
from database_gen.sqlacodegen_models import Feed, Gtfsfeed, Gtfsrealtimefeed, Gbfsfeed
2727

28-
DB_REUSE_SESSION: Final[str] = "DB_REUSE_SESSION"
2928
LOGGER = logging.getLogger(__name__)
3029

3130

@@ -173,9 +172,6 @@ def start_db_session(self, echo: bool = True):
173172
finally:
174173
session.close()
175174

176-
def is_session_reusable():
177-
return os.getenv("%s" % DB_REUSE_SESSION, "false").lower() == "true"
178-
179175

180176
def refresh_materialized_view(session: "Session", view_name: str) -> bool:
181177
"""

scripts/api-start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
# relative path
66
SCRIPT_PATH="$(dirname -- "${BASH_SOURCE[0]}")"
77
PORT=8080
8-
(cd $SCRIPT_PATH/../api/src && uvicorn main:app --host 0.0.0.0 --port $PORT 1 --env-file ../../config/.env.local)
8+
(cd $SCRIPT_PATH/../api/src && uvicorn main:app --host 0.0.0.0 --port $PORT --env-file ../../config/.env.local)

0 commit comments

Comments
 (0)