Skip to content

Commit 8cfaaec

Browse files
authored
Merge pull request #1111 from dmitryerr/fix-sql-connection
fixup: closing connection when load sql files
2 parents 15afd25 + 9bb3f1b commit 8cfaaec

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

newsfragments/+8bf3ab6b.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Close postgresql connection after loading sql files.
2+
3+
Fixes possible `ResourceWarning`

pytest_postgresql/loader.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def build_loader(load: Union[Callable, str, Path]) -> Callable:
2525

2626
def sql(sql_filename: Path, **kwargs: Any) -> None:
2727
"""Database loader for sql files."""
28-
db_connection = psycopg.connect(**kwargs)
29-
with open(sql_filename, "r") as _fd:
30-
with db_connection.cursor() as cur:
31-
cur.execute(_fd.read())
32-
db_connection.commit()
28+
with psycopg.connect(**kwargs) as db_connection:
29+
with open(sql_filename, "r") as _fd:
30+
with db_connection.cursor() as cur:
31+
cur.execute(_fd.read())
32+
db_connection.commit()

0 commit comments

Comments
 (0)