diff --git a/apps/wizard/utils/env.py b/apps/wizard/utils/env.py index d98a1e75e70..7e84c20b3f6 100644 --- a/apps/wizard/utils/env.py +++ b/apps/wizard/utils/env.py @@ -110,7 +110,7 @@ def name(self) -> str: return "local" elif self.env_type_id == "staging": return f"{self.conf.DB_HOST}" - raise ValueError("Unknown env_type_id") + raise ValueError(f"Unknown env_type_id (DB_NAME/DB_USER={self.conf.DB_NAME}/{self.conf.DB_USER})") @property def base_site(self) -> str | None: diff --git a/etl/grapher_helpers.py b/etl/grapher_helpers.py index 68d90e16f01..11914907b43 100644 --- a/etl/grapher_helpers.py +++ b/etl/grapher_helpers.py @@ -14,6 +14,7 @@ from pymysql import IntegrityError from sqlalchemy import text from sqlalchemy.engine import Engine +from sqlalchemy.orm import Session from etl.db import get_engine, read_sql from etl.files import checksum_str @@ -312,12 +313,12 @@ def _get_entities_from_db( def _get_and_create_entities_in_db(countries: Set[str], engine: Engine | None = None) -> Dict[str, int]: engine = engine or get_engine() - with engine.connect() as con: + with Session(engine) as session: log.info("Creating entities in DB", countries=countries) out = {} for name in countries: try: - con.execute( + session.execute( text( """ INSERT INTO entities @@ -328,12 +329,13 @@ def _get_and_create_entities_in_db(countries: Set[str], engine: Engine | None = ), {"name": name}, ) + session.commit() except IntegrityError: # If another process inserted the same entity before us, we can # safely ignore the error and fetch the ID pass - row = con.execute( + row = session.execute( text( """ SELECT id FROM entities