|
31 | 31 | ) |
32 | 32 |
|
33 | 33 |
|
| 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 | + |
34 | 48 | def test_connect_with_dburi(dburi, defaultenv): |
35 | 49 | "Connecting with db-uri instead of LIPQ* environment variables should work." |
36 | 50 | defaultenv_without_libpq = { |
@@ -1204,6 +1218,61 @@ def test_notify_reloading_catalog_cache(defaultenv): |
1204 | 1218 | assert response.status_code == 200 |
1205 | 1219 |
|
1206 | 1220 |
|
| 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 | + |
1207 | 1276 | def test_role_settings(defaultenv): |
1208 | 1277 | "statement_timeout should be set per role" |
1209 | 1278 |
|
|
0 commit comments