Skip to content

Commit efd9d56

Browse files
authored
Merge pull request piccolo-orm#74 from piccolo-orm/connection_pool_typo
fixing typo with start_connection_pool and close_connection_pool
2 parents 577a24d + c04b087 commit efd9d56

File tree

6 files changed

+87
-20
lines changed

6 files changed

+87
-20
lines changed

docs/src/piccolo/engines/postgres_engine.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ pool in the shutdown event handler.
4747
@app.on_event('startup')
4848
async def open_database_connection_pool():
4949
engine = engine_finder()
50-
await engine.start_connnection_pool()
50+
await engine.start_connection_pool()
5151
5252
5353
@app.on_event('shutdown')
5454
async def close_database_connection_pool():
5555
engine = engine_finder()
56-
await engine.close_connnection_pool()
56+
await engine.close_connection_pool()
5757
5858
.. hint:: Using a connection pool helps with performance, since connections
5959
are reused instead of being created for each query.
@@ -74,7 +74,7 @@ adapter. Here's an example:
7474
.. code-block:: python
7575
7676
# To increase the number of connections available:
77-
await engine.start_connnection_pool(max_size=20)
77+
await engine.start_connection_pool(max_size=20)
7878
7979
-------------------------------------------------------------------------------
8080

piccolo/apps/asgi/commands/templates/starlette/_fastapi_app.py.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ async def delete_task(task_id: int):
7777
@app.on_event("startup")
7878
async def open_database_connection_pool():
7979
engine = engine_finder()
80-
await engine.start_connnection_pool()
80+
await engine.start_connection_pool()
8181

8282

8383
@app.on_event("shutdown")
8484
async def close_database_connection_pool():
8585
engine = engine_finder()
86-
await engine.close_connnection_pool()
86+
await engine.close_connection_pool()

piccolo/apps/asgi/commands/templates/starlette/_starlette_app.py.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ app = Starlette(
3030
@app.on_event("startup")
3131
async def open_database_connection_pool():
3232
engine = engine_finder()
33-
await engine.start_connnection_pool()
33+
await engine.start_connection_pool()
3434

3535

3636
@app.on_event("shutdown")
3737
async def close_database_connection_pool():
3838
engine = engine_finder()
39-
await engine.close_connnection_pool()
39+
await engine.close_connection_pool()

piccolo/engine/postgres.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import contextvars
33
from dataclasses import dataclass
44
import typing as t
5-
import warnings
65

76
import asyncpg # type: ignore
87
from asyncpg.connection import Connection # type: ignore
@@ -287,24 +286,44 @@ async def prep_database(self):
287286
)
288287

289288
###########################################################################
289+
# These typos existed in the codebase for a while, so leaving these proxy
290+
# methods for now to ensure backwards compatility.
290291

291292
async def start_connnection_pool(self, **kwargs) -> None:
293+
colored_warning(
294+
"`start_connnection_pool` is a typo - please change it to "
295+
"`start_connection_pool`.",
296+
category=DeprecationWarning,
297+
)
298+
return await self.start_connection_pool()
299+
300+
async def close_connnection_pool(self, **kwargs) -> None:
301+
colored_warning(
302+
"`close_connnection_pool` is a typo - please change it to "
303+
"`close_connection_pool`.",
304+
category=DeprecationWarning,
305+
)
306+
return await self.close_connection_pool()
307+
308+
###########################################################################
309+
310+
async def start_connection_pool(self, **kwargs) -> None:
292311
if self.pool:
293-
warnings.warn(
312+
colored_warning(
294313
"A pool already exists - close it first if you want to create "
295-
"a new pool."
314+
"a new pool.",
296315
)
297316
else:
298317
config = dict(self.config)
299318
config.update(**kwargs)
300319
self.pool = await asyncpg.create_pool(**config)
301320

302-
async def close_connnection_pool(self) -> None:
321+
async def close_connection_pool(self) -> None:
303322
if self.pool:
304323
await self.pool.close()
305324
self.pool = None
306325
else:
307-
warnings.warn("No pool is running.")
326+
colored_warning("No pool is running.")
308327

309328
###########################################################################
310329

piccolo/utils/warnings.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from __future__ import annotations
12
from enum import Enum
3+
import typing as t
24
import warnings
35

46
import colorama # type: ignore
@@ -14,7 +16,25 @@ class Level(Enum):
1416

1517

1618
def colored_warning(
17-
message: str, stacklevel: int = 3, level: Level = Level.medium
19+
message: str,
20+
category: t.Type[Warning] = Warning,
21+
stacklevel: int = 3,
22+
level: Level = Level.medium,
1823
):
24+
"""
25+
A wrapper around the stdlib's `warnings.warn`, which colours the output.
26+
27+
:param message:
28+
The message to display to the user
29+
:category:
30+
`Warning` has several subclasses which may be more appropriate, for
31+
example `DeprecationWarning`.
32+
:stacklevel:
33+
Used to determine there the source of the error within the source code.
34+
See the Python docs for more detail.
35+
https://docs.python.org/3/library/warnings.html#warnings.warn
36+
:level:
37+
Used to determine the colour of the text displayed to the user.
38+
"""
1939
colored_message = level.value + message + colorama.Fore.RESET
20-
warnings.warn(colored_message, stacklevel=stacklevel)
40+
warnings.warn(colored_message, category=category, stacklevel=stacklevel)

tests/engine/test_pool.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
import asyncio
22

3+
from piccolo.engine.postgres import PostgresEngine
4+
35
from ..base import DBTestCase, postgres_only
46
from ..example_app.tables import Manager
57

68

79
@postgres_only
810
class TestPool(DBTestCase):
911
async def _create_pool(self):
10-
await Manager._meta.db.start_connnection_pool()
11-
await Manager._meta.db.close_connnection_pool()
12+
engine: PostgresEngine = Manager._meta.db
13+
14+
await engine.start_connection_pool()
15+
assert engine.pool is not None
16+
17+
await engine.close_connection_pool()
18+
assert engine.pool is None
1219

1320
async def _make_query(self):
14-
await Manager._meta.db.start_connnection_pool()
21+
await Manager._meta.db.start_connection_pool()
1522

1623
await Manager(name="Bob").save().run()
1724
response = await Manager.select().run()
1825
self.assertTrue("Bob" in [i["name"] for i in response])
1926

20-
await Manager._meta.db.close_connnection_pool()
27+
await Manager._meta.db.close_connection_pool()
2128

2229
async def _make_many_queries(self):
23-
await Manager._meta.db.start_connnection_pool()
30+
await Manager._meta.db.start_connection_pool()
2431

2532
await Manager(name="Bob").save().run()
2633

@@ -30,7 +37,7 @@ async def get_data():
3037

3138
await asyncio.gather(*[get_data() for _ in range(500)])
3239

33-
await Manager._meta.db.close_connnection_pool()
40+
await Manager._meta.db.close_connection_pool()
3441

3542
def test_creation(self):
3643
"""
@@ -50,3 +57,24 @@ def test_many_queries(self):
5057
exceed a connection limit - queries should queue, then succeed.
5158
"""
5259
asyncio.run(self._make_many_queries())
60+
61+
62+
@postgres_only
63+
class TestPoolProxyMethods(DBTestCase):
64+
async def _create_pool(self):
65+
engine: PostgresEngine = Manager._meta.db
66+
67+
# Deliberate typo ('nnn'):
68+
await engine.start_connnection_pool()
69+
assert engine.pool is not None
70+
71+
# Deliberate typo ('nnn'):
72+
await engine.close_connnection_pool()
73+
assert engine.pool is None
74+
75+
def test_proxy_methods(self):
76+
"""
77+
There are some proxy methods, due to some old typos. Make sure they
78+
work, to ensure backwards compatibility.
79+
"""
80+
asyncio.run(self._create_pool())

0 commit comments

Comments
 (0)