From f17ff750dfb0c488623e761b776f4fe06070656a Mon Sep 17 00:00:00 2001 From: David Dudas Date: Tue, 12 Nov 2024 18:28:43 -0800 Subject: [PATCH] [Issue 2665] Add db name to connection url and remove logging (#2826) ## Summary Partially Fixes #2665 ### Time to review: __1 min__ ## Changes proposed > What was added, updated, or removed in this PR. Adds db name to Postgres connection url; removes logging ## Context for reviewers > Testing instructions, background context, more in-depth details of the implementation, and anything else you'd like to call out or ask reviewers. Explain how the changes were verified. ## Additional information > Screenshots, GIF demos, code examples or output to help show the changes working as expected. --- analytics/config.py | 1 + analytics/src/analytics/integrations/db.py | 9 ++------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/analytics/config.py b/analytics/config.py index 08bce54b9..9c5cd0bbf 100644 --- a/analytics/config.py +++ b/analytics/config.py @@ -12,6 +12,7 @@ class PydanticBaseEnvConfig(BaseSettings): class DBSettings(PydanticBaseEnvConfig): db_host: str = Field(alias="DB_HOST") + name: str = Field(alias="DB_NAME") port: int = Field(5432,alias="DB_PORT") user: str = Field (alias="DB_USER") password: Optional[str] = Field(None, alias="DB_PASSWORD") diff --git a/analytics/src/analytics/integrations/db.py b/analytics/src/analytics/integrations/db.py index c2160cf15..f8ca4dd90 100644 --- a/analytics/src/analytics/integrations/db.py +++ b/analytics/src/analytics/integrations/db.py @@ -1,8 +1,6 @@ # pylint: disable=invalid-name, line-too-long """Get a connection to the database using a SQLAlchemy engine object.""" -import os - import boto3 from sqlalchemy import Engine, create_engine @@ -27,15 +25,12 @@ def get_db() -> Engine: db = get_db_settings() # inspired by simpler-grants-gov/blob/main/api/src/adapters/db/clients/postgres_client.py token = db.password if db.local_env is True else generate_iam_auth_token(db) - url = f"postgresql+psycopg://{db.user}:{token}@{db.db_host}:{db.port}?sslmode={db.ssl_mode}" - print(f"TEMP DEBUG: environment = {os.getenv('ENVIRONMENT', 'local')}") - print(f"TEMP DEBUG: db settings = {db}") - print(f"TEMP DEBUG: token has non-zero len? {len(str(token)) > 0}") + url = f"postgresql+psycopg://{db.user}:{token}@{db.db_host}:{db.port}/{db.name}?sslmode={db.ssl_mode}" return create_engine( url, pool_pre_ping=True, - hide_parameters=False, + hide_parameters=True, )