Skip to content

Commit 9f056ac

Browse files
authored
fix: not logging tx variables and pre-request (PostgREST#4341)
Addresses the first item on PostgREST#3934
1 parent 98de226 commit 9f056ac

File tree

6 files changed

+70
-11
lines changed

6 files changed

+70
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99

1010
- Fix not logging OpenAPI queries when `log-query=main-query` is enabled by @steve-chavez in #4226
1111
- Fix not logging explain query when `log-query=main-query` is enabled by @steve-chavez in #4319
12+
- Fix not logging transaction variables and db-pre-request function when `log-query=main-query` is enabled by @steve-chavez in #3934
1213

1314
### Added
1415

src/PostgREST/Logger.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ logWithZTime loggerState txt = do
123123

124124
logMainQ :: LoggerState -> MainQuery -> IO ()
125125
logMainQ loggerState MainQuery{mqOpenAPI=(x, y, z),..} =
126-
let snipts = renderSnippet <$> [mqMain, x, y, z, fromMaybe mempty mqExplain]
126+
let snipts = renderSnippet <$> [mqTxVars, fromMaybe mempty mqPreReq, mqMain, x, y, z, fromMaybe mempty mqExplain]
127127
-- Does not log SQL when it's empty (happens on OPTIONS requests and when the openapi queries are not generated)
128128
logQ q = when (q /= mempty) $ logWithZTime loggerState $ showOnSingleLine '\n' $ T.decodeUtf8 q in
129129
mapM_ logQ snipts

test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRoutines].yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,23 @@
196196
pdSchema: public
197197
pdVolatility: Volatile
198198

199+
- - qiName: do_nothing
200+
qiSchema: public
201+
- - pdDescription: null
202+
pdFuncSettings: []
203+
pdHasVariadic: false
204+
pdName: do_nothing
205+
pdParams: []
206+
pdReturnType:
207+
contents:
208+
contents:
209+
qiName: void
210+
qiSchema: pg_catalog
211+
tag: Scalar
212+
tag: Single
213+
pdSchema: public
214+
pdVolatility: Volatile
215+
199216
- - qiName: get_guc_value
200217
qiSchema: public
201218
- - pdDescription: null

test/io/fixtures.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ create function notify_do_nothing() returns void as $_$
9595
notify pgrst, 'nothing';
9696
$_$ language sql;
9797

98+
create function do_nothing() returns void as $_$
99+
$_$ language sql;
100+
98101
create function reset_invalid_role_claim_key() returns void as $_$
99102
begin
100103
alter role postgrest_test_authenticator reset pgrst.jwt_role_claim_key;

test/io/test_io.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,20 @@ def test_log_level(level, defaultenv):
10031003
def test_log_query(level, defaultenv):
10041004
"log_query=true should log the SQL query according to the log_level"
10051005

1006-
env = {**defaultenv, "PGRST_LOG_LEVEL": level, "PGRST_LOG_QUERY": "main-query"}
1006+
def drain_stdout(proc):
1007+
lines = []
1008+
while True:
1009+
chunk = proc.read_stdout(nlines=20)
1010+
if not chunk:
1011+
break
1012+
lines.extend(chunk)
1013+
return lines
1014+
1015+
env = {
1016+
**defaultenv,
1017+
"PGRST_LOG_LEVEL": level,
1018+
"PGRST_LOG_QUERY": "main-query",
1019+
}
10071020

10081021
with run(env=env) as postgrest:
10091022
response = postgrest.session.get("/")
@@ -1033,15 +1046,9 @@ def test_log_query(level, defaultenv):
10331046
root_tables_regx = r".+: SELECT n.nspname AS table_schema, .+ FROM pg_class c .+ ORDER BY table_schema, table_name"
10341047
root_procs_regx = r".+: WITH base_types AS \(.+\) SELECT pn.nspname AS proc_schema, .+ FROM pg_proc p.+AND p.pronamespace = \$1::regnamespace"
10351048
root_descr_regx = r".+: SELECT pg_catalog\.obj_description\(\$1::regnamespace, 'pg_namespace'\)"
1036-
1037-
def drain_stdout(proc):
1038-
lines = []
1039-
while True:
1040-
chunk = proc.read_stdout(nlines=20)
1041-
if not chunk:
1042-
break
1043-
lines.extend(chunk)
1044-
return lines
1049+
set_config_regx = (
1050+
r".+: select set_config\('search_path', \$1, true\), set_config\("
1051+
)
10451052

10461053
output = drain_stdout(postgrest)
10471054

@@ -1053,36 +1060,64 @@ def drain_stdout(proc):
10531060
root_tables = [line for line in output if re.match(root_tables_regx, line)]
10541061
root_procs = [line for line in output if re.match(root_procs_regx, line)]
10551062
root_descr = [line for line in output if re.match(root_descr_regx, line)]
1063+
set_configs = [line for line in output if re.match(set_config_regx, line)]
10561064

10571065
if level == "crit":
1066+
assert not set_configs
10581067
assert not project_queries
10591068
assert not project_counts
10601069
assert not infinite_queries
10611070
assert not root_tables
10621071
assert not root_procs
10631072
assert not root_descr
10641073
elif level in {"error", "warn"}:
1074+
assert len(set_configs) == 1
10651075
assert len(infinite_queries) == 1
10661076
assert not project_queries
10671077
assert not project_counts
10681078
assert not root_tables
10691079
assert not root_procs
10701080
assert not root_descr
10711081
elif level == "info":
1082+
assert len(set_configs) == 5
10721083
assert len(project_queries) == 3
10731084
assert len(project_counts) == 2
10741085
assert len(infinite_queries) == 1
10751086
assert len(root_tables) == 1
10761087
assert len(root_procs) == 1
10771088
assert len(root_descr) == 1
10781089
elif level == "debug":
1090+
assert len(set_configs) == 5
10791091
assert len(project_queries) == 3
10801092
assert len(project_counts) == 2
10811093
assert len(infinite_queries) == 1
10821094
assert len(root_tables) == 1
10831095
assert len(root_procs) == 1
10841096
assert len(root_descr) == 1
10851097

1098+
pre_req_env = {
1099+
**env,
1100+
"PGRST_DB_PRE_REQUEST": "do_nothing",
1101+
}
1102+
1103+
with run(env=pre_req_env) as postgrest:
1104+
response = postgrest.session.get("/projects")
1105+
assert response.status_code == 200
1106+
1107+
output = drain_stdout(postgrest)
1108+
1109+
pre_request_regx = r'.+: select "do_nothing"()'
1110+
pre_reqs = [line for line in output if re.match(pre_request_regx, line)]
1111+
1112+
if level == "crit":
1113+
assert not pre_reqs
1114+
elif level in {"error", "warn"}:
1115+
assert not pre_reqs
1116+
elif level == "info":
1117+
assert len(pre_reqs) == 1
1118+
elif level == "debug":
1119+
assert len(pre_reqs) == 1
1120+
10861121

10871122
def test_no_pool_connection_required_on_bad_http_logic(defaultenv):
10881123
"no pool connection should be consumed for failing on invalid http logic"

test/spec/fixtures/schema.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3843,3 +3843,6 @@ $$ language sql;
38433843
create or replace function test.delete_items_returns_void() returns void as $$
38443844
delete from items;
38453845
$$ language sql;
3846+
3847+
create function do_nothing() returns void as $_$
3848+
$_$ language sql;

0 commit comments

Comments
 (0)