Skip to content

Commit f4e0605

Browse files
BordaCopilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent a624ebd commit f4e0605

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

tests/conftest.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _build_worker_url(original_url: str, schema_name: str) -> str:
4545

4646

4747
@pytest.fixture(scope="session")
48-
def worker_sql_connection() -> Optional[str]:
48+
def worker_sql_connection(request: pytest.FixtureRequest) -> Optional[str]:
4949
"""Create the worker-specific PostgreSQL schema once per xdist worker session.
5050
5151
Returns the worker-specific connection URL, or None when schema isolation is not
@@ -58,6 +58,11 @@ def worker_sql_connection() -> Optional[str]:
5858
that depend on the schema will fail at the DB level with a diagnostic error.
5959
6060
"""
61+
# Avoid touching SQL backends entirely when no SQL tests are collected.
62+
has_sql_tests = any("sql" in item.keywords for item in request.session.items)
63+
if not has_sql_tests:
64+
return None
65+
6166
worker_id = os.environ.get("PYTEST_XDIST_WORKER", "master")
6267
if worker_id == "master":
6368
return None
@@ -73,16 +78,19 @@ def worker_sql_connection() -> Optional[str]:
7378

7479
new_url = _build_worker_url(original_url, schema_name)
7580

81+
engine = None
7682
try:
7783
from sqlalchemy import create_engine, text
7884

7985
engine = create_engine(original_url)
8086
with engine.connect() as conn:
8187
conn.execute(text(f"CREATE SCHEMA IF NOT EXISTS {schema_name}"))
8288
conn.commit()
83-
engine.dispose()
8489
except Exception as e:
8590
logger.debug("Failed to create schema %s: %s", schema_name, e)
91+
finally:
92+
if engine is not None:
93+
engine.dispose()
8694

8795
return new_url
8896

tests/test_conftest_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_url_with_existing_options_appends(self):
4545
assert "statement_timeout" in result
4646

4747
def test_url_encoded_roundtrip(self):
48-
"""Values with spaces survive encode/decode without corruption."""
48+
"""Preserve existing URL-encoded options while adding the worker search path."""
4949
url = "postgresql://user:pass@localhost/testdb?options=-cwork_mem%3D64MB"
5050
result = _build_worker_url(url, "test_worker_2")
5151
# Scheme, host, and path must be preserved

0 commit comments

Comments
 (0)