Skip to content

Commit bc1c3ae

Browse files
authored
Merge branch 'piccolo-orm:master' into composite_index
2 parents 2b9ae4d + 53b7628 commit bc1c3ae

7 files changed

Lines changed: 102 additions & 99 deletions

File tree

docs/src/piccolo/api_reference/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ Date
106106
.. autoclass:: DateOffset
107107
:members:
108108

109+
110+
UUID
111+
~~~~
112+
113+
.. autoclass:: UUID4
114+
:members:
115+
109116
-------------------------------------------------------------------------------
110117

111118
Testing

docs/src/piccolo/engines/cockroach_engine.rst

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,55 +27,10 @@ to learn more.
2727

2828
-------------------------------------------------------------------------------
2929

30-
Connection pool
30+
Connection Pool
3131
---------------
3232

33-
To use a connection pool, you need to first initialise it. The best place to do
34-
this is in the startup event handler of whichever web framework you are using.
35-
36-
Here's an example using Starlette. Notice that we also close the connection
37-
pool in the shutdown event handler.
38-
39-
.. code-block:: python
40-
41-
from piccolo.engine import engine_finder
42-
from starlette.applications import Starlette
43-
44-
45-
app = Starlette()
46-
47-
48-
@app.on_event('startup')
49-
async def open_database_connection_pool():
50-
engine = engine_finder()
51-
await engine.start_connection_pool()
52-
53-
54-
@app.on_event('shutdown')
55-
async def close_database_connection_pool():
56-
engine = engine_finder()
57-
await engine.close_connection_pool()
58-
59-
.. hint:: Using a connection pool helps with performance, since connections
60-
are reused instead of being created for each query.
61-
62-
Once a connection pool has been started, the engine will use it for making
63-
queries.
64-
65-
.. hint:: If you're running several instances of an app on the same server,
66-
you may prefer an external connection pooler - like pgbouncer.
67-
68-
Configuration
69-
~~~~~~~~~~~~~
70-
71-
The connection pool uses the same configuration as your engine. You can also
72-
pass in additional parameters, which are passed to the underlying database
73-
adapter. Here's an example:
74-
75-
.. code-block:: python
76-
77-
# To increase the number of connections available:
78-
await engine.start_connection_pool(max_size=20)
33+
See :ref:`ConnectionPool`.
7934

8035
-------------------------------------------------------------------------------
8136

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.. _ConnectionPool:
2+
3+
Connection Pool
4+
===============
5+
6+
.. hint:: Connection pools can be used with Postgres and CockroachDB.
7+
8+
Setup
9+
~~~~~
10+
11+
To use a connection pool, you need to first initialise it. The best place to do
12+
this is in the startup event handler of whichever web framework you are using.
13+
We also want to close the connection pool in the shutdown event handler.
14+
15+
The recommended way for Starlette and FastAPI apps is to use the ``lifespan``
16+
parameter:
17+
18+
.. code-block:: python
19+
20+
from contextlib import asynccontextmanager
21+
from piccolo.engine import engine_finder
22+
from starlette.applications import Starlette
23+
24+
25+
@asynccontextmanager
26+
async def lifespan(app: Starlette):
27+
engine = engine_finder()
28+
assert engine
29+
await engine.start_connection_pool()
30+
yield
31+
await engine.close_connection_pool()
32+
33+
34+
app = Starlette(lifespan=lifespan)
35+
36+
In older versions of Starlette and FastAPI, you may need event handlers
37+
instead:
38+
39+
.. code-block:: python
40+
41+
from piccolo.engine import engine_finder
42+
from starlette.applications import Starlette
43+
44+
45+
app = Starlette()
46+
47+
48+
@app.on_event('startup')
49+
async def open_database_connection_pool():
50+
engine = engine_finder()
51+
await engine.start_connection_pool()
52+
53+
54+
@app.on_event('shutdown')
55+
async def close_database_connection_pool():
56+
engine = engine_finder()
57+
await engine.close_connection_pool()
58+
59+
.. hint:: Using a connection pool helps with performance, since connections
60+
are reused instead of being created for each query.
61+
62+
Once a connection pool has been started, the engine will use it for making
63+
queries.
64+
65+
.. hint:: If you're running several instances of an app on the same server,
66+
you may prefer an external connection pooler - like pgbouncer.
67+
68+
-------------------------------------------------------------------------------
69+
70+
Configuration
71+
~~~~~~~~~~~~~
72+
73+
The connection pool uses the same configuration as your engine. You can also
74+
pass in additional parameters, which are passed to the underlying database
75+
adapter. Here's an example:
76+
77+
.. code-block:: python
78+
79+
# To increase the number of connections available:
80+
await engine.start_connection_pool(max_size=20)

docs/src/piccolo/engines/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,4 @@ Engine types
127127
./sqlite_engine
128128
./postgres_engine
129129
./cockroach_engine
130+
./connection_pool

docs/src/piccolo/engines/postgres_engine.rst

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,10 @@ to learn more.
2626

2727
-------------------------------------------------------------------------------
2828

29-
Connection pool
29+
Connection Pool
3030
---------------
3131

32-
To use a connection pool, you need to first initialise it. The best place to do
33-
this is in the startup event handler of whichever web framework you are using.
34-
35-
Here's an example using Starlette. Notice that we also close the connection
36-
pool in the shutdown event handler.
37-
38-
.. code-block:: python
39-
40-
from piccolo.engine import engine_finder
41-
from starlette.applications import Starlette
42-
43-
44-
app = Starlette()
45-
46-
47-
@app.on_event('startup')
48-
async def open_database_connection_pool():
49-
engine = engine_finder()
50-
await engine.start_connection_pool()
51-
52-
53-
@app.on_event('shutdown')
54-
async def close_database_connection_pool():
55-
engine = engine_finder()
56-
await engine.close_connection_pool()
57-
58-
.. hint:: Using a connection pool helps with performance, since connections
59-
are reused instead of being created for each query.
60-
61-
Once a connection pool has been started, the engine will use it for making
62-
queries.
63-
64-
.. hint:: If you're running several instances of an app on the same server,
65-
you may prefer an external connection pooler - like pgbouncer.
66-
67-
Configuration
68-
~~~~~~~~~~~~~
69-
70-
The connection pool uses the same configuration as your engine. You can also
71-
pass in additional parameters, which are passed to the underlying database
72-
adapter. Here's an example:
73-
74-
.. code-block:: python
75-
76-
# To increase the number of connections available:
77-
await engine.start_connection_pool(max_size=20)
32+
See :ref:`ConnectionPool`.
7833

7934
-------------------------------------------------------------------------------
8035

piccolo/columns/defaults/uuid.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88

99
class UUID4(Default):
10+
"""
11+
This makes the default value for a
12+
:class:`UUID <piccolo.columns.column_types.UUID>` column a randomly
13+
generated UUID v4 value. The advantage over using :func:`uuid.uuid4` from
14+
the standard library, is the default is set on the column definition in the
15+
database too.
16+
"""
17+
1018
@property
1119
def postgres(self):
1220
return "uuid_generate_v4()"

piccolo/columns/m2m.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,8 @@ async def run(self):
311311
transaction, or wrapped in a new transaction.
312312
"""
313313
engine = self.rows[0]._meta.db
314-
if engine.transaction_exists():
315-
await self._run()
316-
else:
317-
async with engine.transaction():
318-
await self._run()
314+
async with engine.transaction():
315+
return await self._run()
319316

320317
def run_sync(self):
321318
return run_sync(self.run())

0 commit comments

Comments
 (0)