Skip to content

Commit ddd7aa5

Browse files
committed
Allow global engine reuse in database utils
1 parent 8ea56e7 commit ddd7aa5

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

src/_pytask/database_utils.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
DatabaseSession = sessionmaker()
34-
_ENGINE_BOX: dict[str, Engine] = {}
34+
_ENGINE: Engine | None = None
3535

3636

3737
class BaseTable(DeclarativeBase):
@@ -50,13 +50,12 @@ class State(BaseTable):
5050

5151
def create_database(url: str) -> None:
5252
"""Create the database."""
53-
engine = _ENGINE_BOX.get("engine")
54-
if engine is not None:
55-
engine.dispose()
56-
engine = create_engine(url)
57-
_ENGINE_BOX["engine"] = engine
58-
BaseTable.metadata.create_all(bind=engine)
59-
DatabaseSession.configure(bind=engine)
53+
global _ENGINE # noqa: PLW0603
54+
if _ENGINE is not None:
55+
_ENGINE.dispose()
56+
_ENGINE = create_engine(url)
57+
BaseTable.metadata.create_all(bind=_ENGINE)
58+
DatabaseSession.configure(bind=_ENGINE)
6059

6160

6261
def _create_or_update_state(first_key: str, second_key: str, hash_: str) -> None:

0 commit comments

Comments
 (0)