feat: allow reloading config with NOTIFY
Enables reloading the config by doing: NOTIFY pgrst, 'reload config' Adds an alias for reloading the schema cache: NOTIFY pgrst, 'reload schema'
This commit is contained in:
committed by
Steve Chavez
parent
9c005fc683
commit
125ea8f6d9
Vendored
+1
-1
@@ -17,7 +17,7 @@ ALTER ROLE postgrest_test_authenticator SET pgrst."db-tx-end" = 'commit-allow-ov
|
||||
ALTER ROLE postgrest_test_authenticator SET pgrst."db-schemas" = 'test, tenant1, tenant2';
|
||||
ALTER ROLE postgrest_test_authenticator SET pgrst."db-root-spec" = 'root';
|
||||
ALTER ROLE postgrest_test_authenticator SET pgrst."db-prepared-statements" = 'false';
|
||||
ALTER ROLE postgrest_test_authenticator SET pgrst."db-pre-request" = 'custom_headers';
|
||||
ALTER ROLE postgrest_test_authenticator SET pgrst."db-pre-request" = 'test.custom_headers';
|
||||
ALTER ROLE postgrest_test_authenticator SET pgrst."db-max-rows" = '1000';
|
||||
ALTER ROLE postgrest_test_authenticator SET pgrst."db-extra-search-path" = 'public, extensions';
|
||||
|
||||
|
||||
Vendored
+25
-1
@@ -1925,9 +1925,33 @@ $$ language sql;
|
||||
create view prepared_statements as
|
||||
select * from pg_catalog.pg_prepared_statements;
|
||||
|
||||
create or replace function change_max_rows_config(val int) returns void as $_$
|
||||
create or replace function change_max_rows_config(val int, notify bool default false) returns void as $_$
|
||||
begin
|
||||
execute format($$
|
||||
alter role postgrest_test_authenticator set pgrst."db-max-rows" = %L;
|
||||
$$, val);
|
||||
if notify then
|
||||
perform pg_notify('pgrst', 'reload config');
|
||||
end if;
|
||||
end $_$ volatile security definer language plpgsql ;
|
||||
|
||||
create or replace function reset_max_rows_config() returns void as $_$
|
||||
begin
|
||||
alter role postgrest_test_authenticator set pgrst."db-max-rows" = '1000';
|
||||
end $_$ volatile security definer language plpgsql ;
|
||||
|
||||
create or replace function change_db_schema_and_full_reload(schemas text) returns void as $_$
|
||||
begin
|
||||
execute format($$
|
||||
alter role postgrest_test_authenticator set pgrst."db-schemas" = %L;
|
||||
$$, schemas);
|
||||
perform pg_notify('pgrst', 'reload config');
|
||||
perform pg_notify('pgrst', 'reload schema');
|
||||
end $_$ volatile security definer language plpgsql ;
|
||||
|
||||
create or replace function v1.reset_db_schema_config() returns void as $_$
|
||||
begin
|
||||
alter role postgrest_test_authenticator set pgrst."db-schemas" = 'test';
|
||||
perform pg_notify('pgrst', 'reload config');
|
||||
perform pg_notify('pgrst', 'reload schema');
|
||||
end $_$ volatile security definer language plpgsql ;
|
||||
|
||||
@@ -5,7 +5,7 @@ db-extra-search-path = "public,extensions"
|
||||
db-max-rows = 1000
|
||||
db-pool = 1
|
||||
db-pool-timeout = 100
|
||||
db-pre-request = "custom_headers"
|
||||
db-pre-request = "test.custom_headers"
|
||||
db-prepared-statements = false
|
||||
db-root-spec = "root"
|
||||
db-schemas = "test,tenant1,tenant2"
|
||||
|
||||
@@ -87,7 +87,7 @@ def defaultenv():
|
||||
"PGRST_DB_URI": os.environ["PGRST_DB_URI"],
|
||||
"PGRST_DB_SCHEMAS": os.environ["PGRST_DB_SCHEMAS"],
|
||||
"PGRST_DB_ANON_ROLE": os.environ["PGRST_DB_ANON_ROLE"],
|
||||
"PGRST_DB_LOAD_GUC_CONFIG": "false"
|
||||
"PGRST_DB_LOAD_GUC_CONFIG": "false",
|
||||
}
|
||||
|
||||
|
||||
@@ -187,7 +187,6 @@ def wait_until_ready(url):
|
||||
for _ in range(10):
|
||||
try:
|
||||
response = session.get(url, timeout=1)
|
||||
|
||||
if response.status_code == 200:
|
||||
return
|
||||
except (requests.ConnectionError, requests.ReadTimeout):
|
||||
@@ -508,6 +507,34 @@ def test_db_schema_reload(tmp_path, defaultenv):
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_db_schema_notify_reload(defaultenv):
|
||||
"DB schema and config should be reloaded when PostgREST is sent a NOTIFY"
|
||||
|
||||
env = {
|
||||
**defaultenv,
|
||||
"PGRST_DB_LOAD_GUC_CONFIG": "true",
|
||||
"PGRST_DB_CHANNEL_ENABLED": "true",
|
||||
"PGRST_DB_SCHEMAS": "test",
|
||||
}
|
||||
|
||||
with run(env=env) as postgrest:
|
||||
response = postgrest.session.get("/parents")
|
||||
assert response.status_code == 404
|
||||
|
||||
# change db-schemas config on the db and reload config and cache with notify
|
||||
postgrest.session.post(
|
||||
"/rpc/change_db_schema_and_full_reload", data={"schemas": "v1"}
|
||||
)
|
||||
|
||||
time.sleep(0.5)
|
||||
|
||||
response = postgrest.session.get("/parents?select=*,children(*)")
|
||||
assert response.status_code == 200
|
||||
|
||||
# reset db-schemas config on the db
|
||||
postgrest.session.post("/rpc/reset_db_schema_config")
|
||||
|
||||
|
||||
def test_max_rows_reload(defaultenv):
|
||||
"max-rows should be reloaded from role settings when PostgREST receives a SIGUSR2."
|
||||
config = CONFIGSDIR / "sigusr2-settings.config"
|
||||
@@ -530,4 +557,36 @@ def test_max_rows_reload(defaultenv):
|
||||
time.sleep(0.1)
|
||||
|
||||
response = postgrest.session.head("/projects")
|
||||
|
||||
assert response.headers["Content-Range"] == "0-0/*"
|
||||
|
||||
# reset max-rows config on the db
|
||||
postgrest.session.post("/rpc/reset_max_rows_config")
|
||||
|
||||
|
||||
def test_max_rows_notify_reload(defaultenv):
|
||||
"max-rows should be reloaded from role settings when PostgREST receives a NOTIFY"
|
||||
|
||||
env = {
|
||||
**defaultenv,
|
||||
"PGRST_DB_LOAD_GUC_CONFIG": "true",
|
||||
"PGRST_DB_CHANNEL_ENABLED": "true",
|
||||
}
|
||||
|
||||
with run(env=env) as postgrest:
|
||||
response = postgrest.session.head("/projects")
|
||||
assert response.headers["Content-Range"] == "0-4/*"
|
||||
|
||||
# change max-rows config on the db and reload with notify
|
||||
postgrest.session.post(
|
||||
"/rpc/change_max_rows_config", data={"val": 1, "notify": True}
|
||||
)
|
||||
|
||||
time.sleep(0.1)
|
||||
|
||||
response = postgrest.session.head("/projects")
|
||||
|
||||
assert response.headers["Content-Range"] == "0-0/*"
|
||||
|
||||
# reset max-rows config on the db
|
||||
postgrest.session.post("/rpc/reset_max_rows_config")
|
||||
|
||||
Reference in New Issue
Block a user