Skip to content

Commit

Permalink
Add connection info string to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
zacdezgeo committed Aug 14, 2024
1 parent fe23989 commit 64cc3f6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
4 changes: 4 additions & 0 deletions space2stats_api/src/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ class Settings(BaseSettings):
DB_USER: str
DB_PASSWORD: str
DB_TABLE_NAME: str

@property
def DB_CONNECTION_STRING(self) -> str:
return f"host={self.DB_HOST} port={self.DB_PORT} dbname={self.DB_NAME} user={self.DB_USER} password={self.DB_PASSWORD}"
13 changes: 3 additions & 10 deletions space2stats_api/src/app/utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@

settings = Settings()

DB_HOST = settings.DB_HOST
DB_PORT = settings.DB_PORT
DB_NAME = settings.DB_NAME
DB_USER = settings.DB_USER
DB_PASSWORD = settings.DB_PASSWORD
DB_TABLE_NAME = settings.DB_TABLE_NAME or "space2stats"

conninfo = f"host={DB_HOST} port={DB_PORT} dbname={DB_NAME} user={DB_USER} password={DB_PASSWORD}"
conninfo = settings.DB_CONNECTION_STRING
pool = ConnectionPool(conninfo=conninfo, min_size=1, max_size=10, open=True)


Expand All @@ -24,7 +17,7 @@ def get_summaries(fields, h3_ids):
FROM {1}
WHERE hex_id = ANY (%s)
"""
).format(pg.sql.SQL(", ").join(cols), pg.sql.Identifier(DB_TABLE_NAME))
).format(pg.sql.SQL(", ").join(cols), pg.sql.Identifier(settings.DB_TABLE_NAME))
try:
# Convert h3_ids to a list to ensure compatibility with psycopg
h3_ids = list(h3_ids)
Expand Down Expand Up @@ -56,7 +49,7 @@ def get_available_fields():
cur.execute(
sql_query,
[
DB_TABLE_NAME,
settings.DB_TABLE_NAME,
],
)
columns = [row[0] for row in cur.fetchall() if row[0] != "hex_id"]
Expand Down

0 comments on commit 64cc3f6

Please sign in to comment.