Skip to content

Commit

Permalink
Refactor Postgres setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
ktlim committed Apr 27, 2024
1 parent 4b313f6 commit 458d145
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
16 changes: 2 additions & 14 deletions python/lsst/consdb/hinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from lsst.resources import ResourcePath
from sqlalchemy import MetaData, Table, create_engine
from sqlalchemy.dialects.postgresql import insert
from utils import setup_postgres

###############################
# Header Processing Functions #
Expand Down Expand Up @@ -287,20 +288,7 @@ def get_kafka_config() -> KafkaConfig:
instrument_mapping = LSSTCAM_MAPPING
logging.info(f"Instrument = {instrument}")

host = os.environ.get("DB_HOST")
passwd = os.environ.get("DB_PASS")
user = os.environ.get("DB_USER")
dbname = os.environ.get("DB_NAME")
pg_url = ""
if host and passwd and user and dbname:
logging.info(f"Connecting to {host} as {user} to {dbname}")
pg_url = f"postgresql://{user}:{passwd}@{host}/{dbname}"
else:
pg_url = os.environ.get(
"POSTGRES_URL", "postgresql://usdf-butler.slac.stanford.edu:5432/lsstdb1"
)
logging.info(f"Using POSTGRES_URL {user} {host} {dbname}")
engine = create_engine(pg_url)
engine = setup_postgres()
metadata_obj = MetaData(schema=f"cdb_{instrument.lower()}")
exposure_table = Table("exposure", metadata_obj, autoload_with=engine)

Expand Down
3 changes: 2 additions & 1 deletion python/lsst/consdb/pqserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from sqlalchemy import create_engine, MetaData
import sqlalchemy.exc

from utils import setup_postgres

app = Flask(__name__)
engine = create_engine("postgresql://usdf-butler.slac.stanford.edu:5432/lsstdb1")
engine = setup_postgres()
metadata_obj = MetaData(schema="cdb_latiss")
metadata_obj.reflect(engine)

Expand Down
20 changes: 20 additions & 0 deletions python/lsst/consdb/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging
import os

import sqlalchemy

def setup_postgres() -> sqlalchemy.Engine:
host = os.environ.get("DB_HOST")
passwd = os.environ.get("DB_PASS")
user = os.environ.get("DB_USER")
dbname = os.environ.get("DB_NAME")
pg_url = ""
if host and passwd and user and dbname:
logging.info(f"Connecting to {host} as {user} to {dbname}")
pg_url = f"postgresql://{user}:{passwd}@{host}/{dbname}"
else:
pg_url = os.environ.get(
"POSTGRES_URL", "postgresql://usdf-butler.slac.stanford.edu:5432/lsstdb1"
)
logging.info(f"Using POSTGRES_URL {user} {host} {dbname}")
return create_engine(pg_url)

0 comments on commit 458d145

Please sign in to comment.