Skip to content

Commit

Permalink
resolved PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
qcdyx committed Dec 17, 2024
1 parent 88c0528 commit 996298c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 29 deletions.
29 changes: 15 additions & 14 deletions api/src/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from sqlalchemy.orm import sessionmaker
import logging

lock = threading.Lock()


def generate_unique_id() -> str:
"""
Expand Down Expand Up @@ -112,7 +110,8 @@ def __init__(self, echo_sql=False):
database_url = os.getenv("FEEDS_DATABASE_URL")
if database_url is None:
raise Exception("Database URL not provided.")
self.engine = create_engine(database_url, echo=echo_sql, pool_size=10, max_overflow=0)
self.pool_size = int(os.getenv("DB_POOL_SIZE", 10))
self.engine = create_engine(database_url, echo=echo_sql, pool_size=self.pool_size, max_overflow=0)
# creates a session factory
self.Session = sessionmaker(bind=self.engine, autoflush=False)

Expand Down Expand Up @@ -154,17 +153,19 @@ def select(
group_by: Callable = None,
):
"""
Executes a query on the database
:param model: the sqlalchemy model to query
:param query: the sqlalchemy ORM query execute
:param conditions: list of conditions (filters for the query)
:param attributes: list of model's attribute names that you want to fetch. If not given, fetches all attributes.
:param update_session: option to update session before running the query (defaults to True)
:param limit: the optional number of rows to limit the query with
:param offset: the optional number of rows to offset the query with
:param group_by: an optional function, when given query results will group by return value of group_by function.
Query needs to order the return values by the key being grouped by
:return: None if database is inaccessible, the results of the query otherwise
Executes a query on the database.
:param session: The SQLAlchemy session object used to interact with the database.
:param model: The SQLAlchemy model to query. If not provided, the query parameter must be given.
:param query: The SQLAlchemy ORM query to execute. If not provided, a query will be created using the model.
:param conditions: A list of conditions (filters) to apply to the query. Each condition should be a SQLAlchemy
expression.
:param attributes: A list of model's attribute names to fetch. If not provided, all attributes will be fetched.
:param limit: An optional integer to limit the number of rows returned by the query.
:param offset: An optional integer to offset the number of rows returned by the query.
:param group_by: An optional function to group the query results by the return value of the function. The query
needs to order the return values by the key being grouped by.
:return: None if the database is inaccessible, otherwise the results of the query.
"""
try:
if query is None:
Expand Down
2 changes: 0 additions & 2 deletions api/tests/test_utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,5 @@ def empty_database(db, url):
delete_stmt = delete(table)
session.execute(delete_stmt)

session.commit()

except Exception as error:
logging.error(f"Error while deleting from test db: {error}")
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
get_tlnd_authentication_type,
create_new_feed,
)
from helpers.database import start_db_session, configure_polymorphic_mappers
from helpers.database import configure_polymorphic_mappers
from helpers.feed_sync.models import TransitFeedSyncPayload
from test_utils.database_utils import default_db_url
from test_utils.database_utils import default_db_url, get_testing_session


@patch("requests.head")
Expand Down Expand Up @@ -68,7 +68,7 @@ def test_create_new_feed_gtfs_rt():
}
feed_payload = TransitFeedSyncPayload(**payload)
configure_polymorphic_mappers()
session = start_db_session(default_db_url, echo=False)
session = get_testing_session()
new_feed = create_new_feed(session, "tld-102_tu", feed_payload)
session.delete(new_feed)
assert new_feed.stable_id == "tld-102_tu"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,7 @@ def test_process_feed_event_pubsub_error(
mock_session
)

with patch(
"feed_sync_process_transitland.src.main.start_db_session",
return_value=mock_session,
):
process_feed_event(cloud_event)
process_feed_event(cloud_event)

def test_process_feed_event_malformed_cloud_event(self, mock_logging):
"""Test feed event processing with malformed cloud event."""
Expand Down
4 changes: 0 additions & 4 deletions functions-python/helpers/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

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

DB_REUSE_SESSION: Final[str] = "DB_REUSE_SESSION"
LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -173,9 +172,6 @@ def start_db_session(self, echo: bool = True):
finally:
session.close()

def is_session_reusable():
return os.getenv("%s" % DB_REUSE_SESSION, "false").lower() == "true"


def refresh_materialized_view(session: "Session", view_name: str) -> bool:
"""
Expand Down
2 changes: 1 addition & 1 deletion scripts/api-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# relative path
SCRIPT_PATH="$(dirname -- "${BASH_SOURCE[0]}")"
PORT=8080
(cd $SCRIPT_PATH/../api/src && uvicorn main:app --host 0.0.0.0 --port $PORT 1 --env-file ../../config/.env.local)
(cd $SCRIPT_PATH/../api/src && uvicorn main:app --host 0.0.0.0 --port $PORT --env-file ../../config/.env.local)

0 comments on commit 996298c

Please sign in to comment.