Skip to content

Commit b6f1804

Browse files
committed
add an option for logging queries in SQLEngine
1 parent 0488cff commit b6f1804

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

piccolo/engine/postgres.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ class PostgresEngine(Engine):
215215
When the engine starts, it will try and create these extensions
216216
in Postgres.
217217
218+
:param log_queries:
219+
If True, all SQL and DDL statements are printed out before being run.
220+
Useful for debugging.
221+
218222
""" # noqa: E501
219223

220224
__slots__ = ("config", "extensions", "pool", "transaction_connection")
@@ -226,9 +230,11 @@ def __init__(
226230
self,
227231
config: t.Dict[str, t.Any],
228232
extensions: t.Sequence[str] = ["uuid-ossp"],
233+
log_queries: bool = False,
229234
) -> None:
230235
self.config = config
231236
self.extensions = extensions
237+
self.log_queries = log_queries
232238
self.pool: t.Optional[Pool] = None
233239
database_name = config.get("database", "Unknown")
234240
self.transaction_connection = contextvars.ContextVar(
@@ -367,6 +373,9 @@ async def run_querystring(
367373
engine_type=self.engine_type
368374
)
369375

376+
if self.log_queries:
377+
print(querystring)
378+
370379
# If running inside a transaction:
371380
connection = self.transaction_connection.get()
372381
if connection:

0 commit comments

Comments
 (0)