From 2b112f46e2aa1977462cb74508c637b61ac88d9e Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 30 Nov 2026 14:03:05 +0100 Subject: [PATCH] Remove the no longer used `engine` and `connection` globals Only `session` is used in the package. Bound to the `engine`, it connects on its first query instead of when the package is imported. --- src/velofy/db/__init__.py | 2 -- src/velofy/db/connection.py | 14 +++++--------- tests/conftest.py | 5 ----- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/velofy/db/__init__.py b/src/velofy/db/__init__.py index 38a32a8..af7a334 100644 --- a/src/velofy/db/__init__.py +++ b/src/velofy/db/__init__.py @@ -4,8 +4,6 @@ from velofy.db.addresses import Address from velofy.db.addresses_addresses import Path from velofy.db.addresses_pixels import AddressPixelAssociation from velofy.db.cities import City -from velofy.db.connection import connection -from velofy.db.connection import engine from velofy.db.connection import session from velofy.db.couriers import Courier from velofy.db.customers import Customer diff --git a/src/velofy/db/connection.py b/src/velofy/db/connection.py index b3c0d69..3ca9d5c 100644 --- a/src/velofy/db/connection.py +++ b/src/velofy/db/connection.py @@ -1,9 +1,9 @@ """Provide connection utils for the ORM layer. -This module defines fully configured `engine`, `connection`, and `session` -objects to be used as globals within the `velofy` package. +This module defines a fully configured `session` object +to be used as a global within the `velofy` package. -If a database is not guaranteed to be available, they are set to `None`. +If a database is not guaranteed to be available, it is set to `None`. That is the case on the CI server. """ @@ -11,19 +11,15 @@ import os from typing import cast import sqlalchemy as sa -from sqlalchemy import engine as engine_mod from sqlalchemy import orm import velofy if os.getenv("TESTING"): - # Placeholders that the test fixtures replace - engine: engine_mod.Engine = None # type: ignore[assignment] - connection: engine_mod.Connection = None # type: ignore[assignment] + # Placeholder that the test fixtures replace session: orm.Session = None # type: ignore[assignment] else: # pragma: no cover engine = sa.create_engine(cast(str, velofy.config.DATABASE_URI)) - connection = engine.connect() - session = orm.sessionmaker(bind=connection)() + session = orm.Session(bind=engine) diff --git a/tests/conftest.py b/tests/conftest.py index e09637c..4ea0d8b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -47,11 +47,6 @@ def db_connection(request): engine = sa.create_engine(config.DATABASE_URI) connection = engine.connect() - # Monkey patch the package's global `engine` and `connection` objects, - # just in case if it is used somewhere in the code base. - db.engine = engine - db.connection = connection - if request.param == "all_at_once": with connection.begin(): connection.execute(sa.text(f"CREATE SCHEMA {config.CLEAN_SCHEMA};"))