Skip to content

Commit e9a2717

Browse files
authored
fix: psycopg pool deprecation (#38)
Corrects a psycopg deprecation error
1 parent 20baa47 commit e9a2717

File tree

8 files changed

+88
-117
lines changed

8 files changed

+88
-117
lines changed

docs/examples/litestar_asyncpg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""
1616
# /// script
1717
# dependencies = [
18-
# "sqlspec[asyncpg,performance] @ git+https://github.com/litestar-org/sqlspec.git@main",
18+
# "sqlspec[asyncpg,performance]",
1919
# "litestar[standard]",
2020
# ]
2121
# ///

docs/examples/litestar_duckllm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# /// script
1010
# dependencies = [
11-
# "sqlspec[duckdb,performance] @ git+https://github.com/litestar-org/sqlspec.git@main",
11+
# "sqlspec[duckdb,performance]",
1212
# "litestar[standard]",
1313
# ]
1414
# ///

docs/examples/litestar_multi_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99
# /// script
1010
# dependencies = [
11-
# "sqlspec[aiosqlite,duckdb] @ git+https://github.com/litestar-org/sqlspec.git@main",
11+
# "sqlspec[aiosqlite,duckdb]",
1212
# "litestar[standard]",
1313
# ]
1414
# ///

docs/examples/litestar_psycopg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99
# /// script
1010
# dependencies = [
11-
# "sqlspec[psycopg] @ git+https://github.com/litestar-org/sqlspec.git@main",
11+
# "sqlspec[psycopg]",
1212
# "litestar[standard]",
1313
# ]
1414
# ///

docs/examples/standalone_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# /// script
33
# dependencies = [
4-
# "sqlspec[duckdb,performance] @ git+https://github.com/litestar-org/sqlspec.git@main",
4+
# "sqlspec[duckdb,performance]",
55
# "rich>=13.0.0",
66
# "rich-click>=1.7.0",
77
# "faker>=24.0.0",

docs/examples/standalone_duckdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# /// script
77
# dependencies = [
8-
# "sqlspec[duckdb,performance] @ git+https://github.com/litestar-org/sqlspec.git@main",
8+
# "sqlspec[duckdb,performance]",
99
# ]
1010
# ///
1111

sqlspec/adapters/psycopg/config.py

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -606,74 +606,58 @@ def pool_config_dict(self) -> dict[str, Any]:
606606

607607
async def _create_pool(self) -> "AsyncConnectionPool":
608608
"""Create the actual async connection pool."""
609-
logger.info("Creating async Psycopg connection pool", extra={"adapter": "psycopg"})
610609

611-
try:
612-
# Get all config (creates a new dict)
613-
all_config = self.pool_config_dict.copy()
610+
# Get all config (creates a new dict)
611+
all_config = self.pool_config_dict.copy()
612+
613+
# Separate pool-specific parameters that AsyncConnectionPool accepts directly
614+
pool_params = {
615+
"min_size": all_config.pop("min_size", 4),
616+
"max_size": all_config.pop("max_size", None),
617+
"name": all_config.pop("name", None),
618+
"timeout": all_config.pop("timeout", 30.0),
619+
"max_waiting": all_config.pop("max_waiting", 0),
620+
"max_lifetime": all_config.pop("max_lifetime", 3600.0),
621+
"max_idle": all_config.pop("max_idle", 600.0),
622+
"reconnect_timeout": all_config.pop("reconnect_timeout", 300.0),
623+
"num_workers": all_config.pop("num_workers", 3),
624+
}
614625

615-
# Separate pool-specific parameters that AsyncConnectionPool accepts directly
616-
pool_params = {
617-
"min_size": all_config.pop("min_size", 4),
618-
"max_size": all_config.pop("max_size", None),
619-
"name": all_config.pop("name", None),
620-
"timeout": all_config.pop("timeout", 30.0),
621-
"max_waiting": all_config.pop("max_waiting", 0),
622-
"max_lifetime": all_config.pop("max_lifetime", 3600.0),
623-
"max_idle": all_config.pop("max_idle", 600.0),
624-
"reconnect_timeout": all_config.pop("reconnect_timeout", 300.0),
625-
"num_workers": all_config.pop("num_workers", 3),
626-
}
626+
# Create a configure callback to set row_factory
627+
async def configure_connection(conn: "PsycopgAsyncConnection") -> None:
628+
# Set DictRow as the row factory
629+
conn.row_factory = dict_row
627630

628-
# Create a configure callback to set row_factory
629-
async def configure_connection(conn: "PsycopgAsyncConnection") -> None:
630-
# Set DictRow as the row factory
631-
conn.row_factory = dict_row
631+
pool_params["configure"] = all_config.pop("configure", configure_connection)
632632

633-
pool_params["configure"] = all_config.pop("configure", configure_connection)
633+
# Remove None values from pool_params
634+
pool_params = {k: v for k, v in pool_params.items() if v is not None}
634635

635-
# Remove None values from pool_params
636-
pool_params = {k: v for k, v in pool_params.items() if v is not None}
636+
# Handle conninfo vs individual connection parameters
637+
conninfo = all_config.pop("conninfo", None)
638+
if conninfo:
639+
# If conninfo is provided, use it directly
640+
# Don't pass kwargs when using conninfo string
641+
pool = AsyncConnectionPool(conninfo, open=False, **pool_params)
642+
else:
643+
# Otherwise, pass connection parameters via kwargs
644+
# Remove any non-connection parameters
645+
# row_factory is already popped out earlier
646+
all_config.pop("row_factory", None)
647+
# Remove pool-specific settings that may have been left
648+
all_config.pop("kwargs", None)
649+
pool = AsyncConnectionPool("", kwargs=all_config, open=False, **pool_params)
637650

638-
# Handle conninfo vs individual connection parameters
639-
conninfo = all_config.pop("conninfo", None)
640-
if conninfo:
641-
# If conninfo is provided, use it directly
642-
# Don't pass kwargs when using conninfo string
643-
pool = AsyncConnectionPool(conninfo, **pool_params)
644-
else:
645-
# Otherwise, pass connection parameters via kwargs
646-
# Remove any non-connection parameters
647-
# row_factory is already popped out earlier
648-
all_config.pop("row_factory", None)
649-
# Remove pool-specific settings that may have been left
650-
all_config.pop("kwargs", None)
651-
pool = AsyncConnectionPool("", kwargs=all_config, **pool_params)
651+
await pool.open()
652652

653-
await pool.open()
654-
logger.info("Async Psycopg connection pool created successfully", extra={"adapter": "psycopg"})
655-
except Exception as e:
656-
logger.exception(
657-
"Failed to create async Psycopg connection pool", extra={"adapter": "psycopg", "error": str(e)}
658-
)
659-
raise
660653
return pool
661654

662655
async def _close_pool(self) -> None:
663656
"""Close the actual async connection pool."""
664657
if not self.pool_instance:
665658
return
666659

667-
logger.info("Closing async Psycopg connection pool", extra={"adapter": "psycopg"})
668-
669-
try:
670-
await self.pool_instance.close()
671-
logger.info("Async Psycopg connection pool closed successfully", extra={"adapter": "psycopg"})
672-
except Exception as e:
673-
logger.exception(
674-
"Failed to close async Psycopg connection pool", extra={"adapter": "psycopg", "error": str(e)}
675-
)
676-
raise
660+
await self.pool_instance.close()
677661

678662
async def create_connection(self) -> "PsycopgAsyncConnection": # pyright: ignore
679663
"""Create a single async connection (not from pool).

0 commit comments

Comments
 (0)