Skip to content

Commit afd0eb5

Browse files
committed
test: log warning when url-use-legacy-target-names is enabled
1 parent 3ef1276 commit afd0eb5

6 files changed

Lines changed: 160 additions & 2 deletions

File tree

src/PostgREST/Logger.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ observationLogger loggerState logLevel obs = case obs of
8686
when (shouldLogResponse logLevel status) $
8787
logWithZTime loggerState $ observationMessages o
8888
o@LegacyTargetNameWarningObs {} ->
89-
when (logLevel >= LogWarn) $ do
89+
when (logLevel >= LogError) $ do
9090
logWithZTime loggerState $ observationMessages o
9191
o@PoolRequest ->
9292
when (logLevel >= LogDebug) $ do

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,59 @@
1+
- - - qiName: awards
2+
qiSchema: public
3+
- public
4+
- - relCardinality:
5+
relColumns:
6+
- - director_id
7+
- id
8+
relCons: awards_director_id_fkey
9+
tag: M2O
10+
relFTableIsView: false
11+
relForeignTable:
12+
qiName: directors
13+
qiSchema: public
14+
relIsSelf: false
15+
relTable:
16+
qiName: awards
17+
qiSchema: public
18+
relTableIsView: false
19+
tag: Relationship
20+
- relCardinality:
21+
relColumns:
22+
- - film_id
23+
- id
24+
relCons: awards_film_id_fkey
25+
tag: M2O
26+
relFTableIsView: false
27+
relForeignTable:
28+
qiName: films
29+
qiSchema: public
30+
relIsSelf: false
31+
relTable:
32+
qiName: awards
33+
qiSchema: public
34+
relTableIsView: false
35+
tag: Relationship
36+
137
- - - qiName: directors
238
qiSchema: public
339
- public
440
- - relCardinality:
41+
relColumns:
42+
- - id
43+
- director_id
44+
relCons: awards_director_id_fkey
45+
tag: O2M
46+
relFTableIsView: false
47+
relForeignTable:
48+
qiName: awards
49+
qiSchema: public
50+
relIsSelf: false
51+
relTable:
52+
qiName: directors
53+
qiSchema: public
54+
relTableIsView: false
55+
tag: Relationship
56+
- relCardinality:
557
relColumns:
658
- - id
759
- director_id
@@ -22,6 +74,22 @@
2274
qiSchema: public
2375
- public
2476
- - relCardinality:
77+
relColumns:
78+
- - id
79+
- film_id
80+
relCons: awards_film_id_fkey
81+
tag: O2M
82+
relFTableIsView: false
83+
relForeignTable:
84+
qiName: awards
85+
qiSchema: public
86+
relIsSelf: false
87+
relTable:
88+
qiName: films
89+
qiSchema: public
90+
relTableIsView: false
91+
tag: Relationship
92+
- relCardinality:
2593
relColumns:
2694
- - director_id
2795
- id

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,64 @@
126126
tableSchema: public
127127
tableUpdatable: false
128128

129+
- - qiName: awards
130+
qiSchema: public
131+
- tableColumns:
132+
director_id:
133+
colDefault: null
134+
colDescription: null
135+
colEnum: []
136+
colMaxLen: null
137+
colName: director_id
138+
colNominalType: integer
139+
colNullable: true
140+
colType: integer
141+
film_id:
142+
colDefault: null
143+
colDescription: null
144+
colEnum: []
145+
colMaxLen: null
146+
colName: film_id
147+
colNominalType: integer
148+
colNullable: true
149+
colType: integer
150+
id:
151+
colDefault: null
152+
colDescription: null
153+
colEnum: []
154+
colMaxLen: null
155+
colName: id
156+
colNominalType: integer
157+
colNullable: false
158+
colType: integer
159+
name:
160+
colDefault: null
161+
colDescription: null
162+
colEnum: []
163+
colMaxLen: null
164+
colName: name
165+
colNominalType: text
166+
colNullable: true
167+
colType: text
168+
year:
169+
colDefault: null
170+
colDescription: null
171+
colEnum: []
172+
colMaxLen: null
173+
colName: year
174+
colNominalType: integer
175+
colNullable: true
176+
colType: integer
177+
tableDeletable: true
178+
tableDescription: null
179+
tableInsertable: true
180+
tableIsView: false
181+
tableName: awards
182+
tablePKCols:
183+
- id
184+
tableSchema: public
185+
tableUpdatable: true
186+
129187
- - qiName: films
130188
qiSchema: public
131189
- tableColumns:

test/io/fixtures/privileges.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ GRANT USAGE ON SCHEMA test TO postgrest_test_anonymous;
33

44
GRANT SELECT ON authors_only TO postgrest_test_author;
55
GRANT SELECT ON projects TO postgrest_test_anonymous, postgrest_test_w_superuser_settings;
6-
GRANT SELECT ON directors, films TO postgrest_test_anonymous, postgrest_test_w_superuser_settings;
6+
GRANT SELECT ON directors, films, awards TO postgrest_test_anonymous, postgrest_test_w_superuser_settings;
77

88
GRANT ALL ON cats TO postgrest_test_anonymous;
99
GRANT ALL ON items_w_isolation_level TO postgrest_test_anonymous, postgrest_test_repeatable_read, postgrest_test_serializable;

test/io/fixtures/schema.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ CREATE TABLE films (
2525
on delete cascade
2626
);
2727

28+
CREATE TABLE awards (
29+
id int primary key,
30+
name text,
31+
year int,
32+
film_id int references films(id),
33+
director_id int references directors(id)
34+
);
35+
2836
-- data to test resource embedding
2937
TRUNCATE TABLE directors CASCADE;
3038
INSERT INTO directors

test/io/test_io.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,3 +2178,27 @@ def test_positive_pool_metric(defaultenv):
21782178
).group(1)
21792179
)
21802180
assert metrics >= 0
2181+
2182+
2183+
@pytest.mark.parametrize("enabled", ["true", "false"])
2184+
def test_use_legacy_target_names(enabled, defaultenv):
2185+
"Show a warning when a target name is used instead of an alias, only when config is enabled"
2186+
2187+
env = {**defaultenv, "PGRST_URL_USE_LEGACY_TARGET_NAMES": enabled}
2188+
2189+
with run(env=env) as postgrest:
2190+
response = postgrest.session.get(
2191+
"/directors?select=name,all_films:films(title),awards_2026:awards(name)&films.order=title&awards.year=eq.2026"
2192+
)
2193+
2194+
output = postgrest.read_stdout(nlines=2)
2195+
2196+
log_err_warning = "WARNING: Embedded resource was referenced by relation name even though it has an alias. This is deprecated and will stop working in a future release."
2197+
log_err_hint = "Please update the filters that use `films` to `all_films`, `awards` to `awards_2026` in `GET /directors?select=name,all_films:films(title),awards_2026:awards(name)&films.order=title&awards.year=eq.2026`"
2198+
2199+
if enabled == "false":
2200+
assert response.status_code == 400
2201+
assert any(log_err_warning and log_err_hint not in line for line in output)
2202+
else:
2203+
assert response.status_code == 200
2204+
assert any(log_err_warning and log_err_hint in line for line in output)

0 commit comments

Comments
 (0)