Skip to content

Commit 92880c9

Browse files
committed
test: cover stale schema cache database error
Add an IO test that drops a table while schema cache reload is delayed. It verifies the stale cache path returns PostgreSQL 42P01 and the refreshed cache returns PGRST205.
1 parent 07938aa commit 92880c9

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

test/io/test_io.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@
3131
)
3232

3333

34+
def psql_as_superuser(query):
35+
subprocess.check_call(
36+
[
37+
"psql",
38+
"--username",
39+
"postgres",
40+
"--set",
41+
"ON_ERROR_STOP=1",
42+
"-c",
43+
query,
44+
]
45+
)
46+
47+
3448
def test_connect_with_dburi(dburi, defaultenv):
3549
"Connecting with db-uri instead of LIPQ* environment variables should work."
3650
defaultenv_without_libpq = {
@@ -1204,6 +1218,61 @@ def test_notify_reloading_catalog_cache(defaultenv):
12041218
assert response.status_code == 200
12051219

12061220

1221+
def test_stale_schema_cache_dropped_table_returns_database_error(defaultenv):
1222+
"dropped table should return a database error while schema cache is stale"
1223+
1224+
internal_sleep = 2
1225+
env = {
1226+
**defaultenv,
1227+
"PGRST_DB_POOL": "2",
1228+
"PGRST_DB_CHANNEL_ENABLED": "true",
1229+
"PGRST_INTERNAL_SCHEMA_CACHE_QUERY_SLEEP": str(internal_sleep * 1000),
1230+
}
1231+
1232+
try:
1233+
psql_as_superuser(
1234+
"""
1235+
drop table if exists stale_schema_cache_items;
1236+
create table stale_schema_cache_items(id int primary key);
1237+
insert into stale_schema_cache_items values (1);
1238+
grant select on stale_schema_cache_items to postgrest_test_anonymous;
1239+
"""
1240+
)
1241+
1242+
with run(env=env, wait_max_seconds=10) as postgrest:
1243+
response = postgrest.session.get("/stale_schema_cache_items")
1244+
assert response.status_code == 200
1245+
1246+
psql_as_superuser(
1247+
"""
1248+
drop table stale_schema_cache_items;
1249+
notify pgrst, 'reload schema';
1250+
"""
1251+
)
1252+
1253+
response = postgrest.session.get("/stale_schema_cache_items")
1254+
payload = response.json()
1255+
assert response.status_code == 404
1256+
assert payload["code"] == "42P01"
1257+
assert (
1258+
payload["message"]
1259+
== 'relation "public.stale_schema_cache_items" does not exist'
1260+
)
1261+
1262+
time.sleep(internal_sleep + 0.3)
1263+
1264+
response = postgrest.session.get("/stale_schema_cache_items")
1265+
payload = response.json()
1266+
assert response.status_code == 404
1267+
assert payload["code"] == "PGRST205"
1268+
assert (
1269+
payload["message"]
1270+
== "Could not find the table 'public.stale_schema_cache_items' in the schema cache"
1271+
)
1272+
finally:
1273+
psql_as_superuser("drop table if exists stale_schema_cache_items;")
1274+
1275+
12071276
def test_role_settings(defaultenv):
12081277
"statement_timeout should be set per role"
12091278

0 commit comments

Comments
 (0)