2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-30 05:27:55 +00:00

[#2166] Added session value setter functions

src/bin/admin/tests/pgsql_tests.sh.in
    pgsql_upgrade_6_2_to_7_0()  - added session variable tests

src/share/database/scripts/pgsql/dhcpdb_create.pgsql
src/share/database/scripts/pgsql/upgrade_6.2_to_7.0.sh.in
    Added set_session_value functions
    Cleaned up get_session_* functions
    Whitespace cleanup

src/share/database/scripts/pgsql/dhcpdb_drop.pgsql
    Added session variable functions
This commit is contained in:
Thomas Markwalder 2021-11-03 15:20:42 -04:00 committed by Tomek Mrugalski
parent 658a424d03
commit ab3fcdd674
4 changed files with 273 additions and 141 deletions

View File

@ -310,6 +310,28 @@ pgsql_upgrade_6_2_to_7_0() {
pgsql_execute "$select_sql" pgsql_execute "$select_sql"
assert_eq 0 "${EXIT_CODE}" "the dhcp6_server table is broken or missing. (expected status code %d, returned %d)" assert_eq 0 "${EXIT_CODE}" "the dhcp6_server table is broken or missing. (expected status code %d, returned %d)"
# Verify that session variable setting is present and functional.
# First we try fetching big and small int values that do not exist, we should get 0.
# Then we set an integer value and fetch it as big and small.
session_sql="\
select get_session_small_int('kea.test_val'); \
select get_session_small_int('kea.test_val'); \
select set_session_value('kea.test_val', '987'); \
select get_session_value('kea.test_val'); \
select get_session_big_int('kea.test_val'); \
select get_session_small_int('kea.test_val'); \
select set_session_value('kea.test_val', cast('984' as SMALLINT)); \
select get_session_small_int('kea.test_val'); \
select set_session_value('kea.test_val', cast('1984' as BIGINT)); \
select get_session_big_int('kea.test_val'); \
"
run_command \
pgsql_execute "$session_sql"
assert_eq 0 "${EXIT_CODE}" "session variable handling broken. (expected status code %d, returned %d)"
clean_out=`echo ${OUTPUT} | tr '\n' ' '`
assert_str_eq "0 0 987 987 987 984 1984 " "${clean_out}" "session variable output incorrect"
} }
pgsql_upgrade_test() { pgsql_upgrade_test() {

View File

@ -1715,13 +1715,67 @@ CREATE TRIGGER dhcp4_audit_modification_ts_update
CREATE INDEX dhcp4_audit_idx1 ON dhcp4_audit (modification_type); CREATE INDEX dhcp4_audit_idx1 ON dhcp4_audit (modification_type);
CREATE INDEX dhcp4_audit_idx2 ON dhcp4_audit (revision_id); CREATE INDEX dhcp4_audit_idx2 ON dhcp4_audit (revision_id);
-- Stores a TEXT value to a session variable
-- name name of session variable to set
-- value TEXT value to store
CREATE OR REPLACE FUNCTION set_session_value(name text, value TEXT)
RETURNS VOID
AS $$
DECLARE
BEGIN
PERFORM set_config(name, value, false);
RETURN;
EXCEPTION
WHEN OTHERS THEN
RAISE EXCEPTION 'set_session_value(%) : value:[%] failed, sqlstate: %', name, value, sqlstate;
END;$$
LANGUAGE plpgsql;
-- Stores a BIGINT value to a session variable
-- Note the value converted to TEXT and then stored as Postgresql does
-- not support any other data type in session variables.
-- name name of session variable to set
-- value BIGINT value to store
CREATE OR REPLACE FUNCTION set_session_value(name text, value BIGINT)
RETURNS VOID
AS $$
BEGIN
PERFORM set_config(name, cast(value as text), false);
RETURN;
EXCEPTION
WHEN OTHERS THEN
RAISE EXCEPTION 'set_session_value(%) : value:[%] failed, sqlstate: %', name, value, sqlstate;
END;$$
LANGUAGE plpgsql;
-- Stores a SMALLINT value to a session variable
-- Note the value converted to TEXT and then stored as Postgresql does
-- not support any other data type in session variables.
-- name name of session variable to set
-- value SMALLINT value to store
CREATE OR REPLACE FUNCTION set_session_value(name text, value SMALLINT)
RETURNS VOID
AS $$
BEGIN
PERFORM set_config(name, cast(value as text), false);
RETURN;
EXCEPTION
WHEN OTHERS THEN
RAISE EXCEPTION 'set_session_value(%) : value:[%] failed, sqlstate: %', name, value, sqlstate;
END;$$
LANGUAGE plpgsql;
-- Fetches a text value from the session configuration. -- Fetches a text value from the session configuration.
-- param name name of the session variable to fetch -- param name name of the session variable to fetch
-- If the name is not found it returns NULL. -- If the name is not found it returns NULL.
-- Postgresql allows you to store custom session values -- Postgresql allows you to store custom session values
-- but throws an exception if they have not first been -- but throws an exception if they have not first been
-- set. This allows us to be a bit more graceful. -- set. This allows us to be a bit more graceful.
CREATE OR REPLACE FUNCTION get_session_value(name text) CREATE OR REPLACE FUNCTION get_session_value(name TEXT)
RETURNS TEXT RETURNS TEXT
AS $$ AS $$
DECLARE DECLARE
@ -1731,9 +1785,11 @@ BEGIN
RETURN(text_value); RETURN(text_value);
EXCEPTION EXCEPTION
WHEN OTHERS THEN WHEN undefined_object THEN
RAISE NOTICE 'get_session_value(%) failed, sqlstate: %', name, sqlstate; -- Variable has not been initialized so return NULL
RETURN NULL; RETURN NULL;
WHEN OTHERS THEN
RAISE EXCEPTION 'get_session_value(%, TEXT) failed, sqlstate: %', name, sqlstate;
END;$$ END;$$
LANGUAGE plpgsql; LANGUAGE plpgsql;
@ -1772,8 +1828,7 @@ AS $$
DECLARE DECLARE
int_value SMALLINT := 0; int_value SMALLINT := 0;
text_value TEXT := ''; text_value TEXT := '';
BEGIN BEGIN text_value = get_session_value(name);
text_value = get_session_value(name);
IF text_value is NULL or text_value = '' THEN IF text_value is NULL or text_value = '' THEN
RETURN(0); RETURN(0);
END IF; END IF;
@ -1840,8 +1895,8 @@ BEGIN
VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id; VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id;
-- Update pertienent session variables. -- Update pertienent session variables.
PERFORM set_config('kea.audit_revision_id', cast(audit_revision_id as text), false); PERFORM set_session_value('kea.audit_revision_id', audit_revision_id);
PERFORM set_config('kea.cascade_transaction', cast(cascade_transaction as text), false); PERFORM set_session_value('kea.cascade_transaction', cascade_transaction);
END IF; END IF;
RETURN; RETURN;
END;$$; END;$$;
@ -1988,7 +2043,7 @@ BEGIN
-- Note this does not work prior to Postgres 9.2 unless the variables are -- Note this does not work prior to Postgres 9.2 unless the variables are
-- defined in postgresql.conf. I think for now we put up with CB not supported -- defined in postgresql.conf. I think for now we put up with CB not supported
-- prior to 9.2 or we tell people how to edit the conf file. -- prior to 9.2 or we tell people how to edit the conf file.
PERFORM set_config('kea.depend_on_known_indirectly', cast(depend_on_known_indirectly as text), false); PERFORM set_session_value('kea.depend_on_known_indirectly', depend_on_known_indirectly);
-- Bail if the class is updated without re-positioning. -- Bail if the class is updated without re-positioning.
IF( IF(
@ -2106,8 +2161,8 @@ CREATE TRIGGER dhcp4_client_class_AINS
-- ----------------------------------------------------------------------- -- -----------------------------------------------------------------------
CREATE OR REPLACE FUNCTION func_dhcp4_client_class_AUPD() RETURNS trigger AS $dhcp4_client_class_AUPD$ CREATE OR REPLACE FUNCTION func_dhcp4_client_class_AUPD() RETURNS trigger AS $dhcp4_client_class_AUPD$
BEGIN BEGIN
PERFORM set_config('kea.depend_on_known_directly', cast(OLD.depend_on_known_directly as text), false); PERFORM set_session_value('kea.depend_on_known_directly', OLD.depend_on_known_directly);
PERFORM set_config('kea.client_class_id', cast(NEW.id as text), false); PERFORM set_session_value('kea.client_class_id', NEW.id);
PERFORM setClientClass4Order(NEW.id, NEW.follow_class_name, OLD.follow_class_name); PERFORM setClientClass4Order(NEW.id, NEW.follow_class_name, OLD.follow_class_name);
PERFORM createAuditEntryDHCP4('dhcp4_client_class', NEW.id, 'update'); PERFORM createAuditEntryDHCP4('dhcp4_client_class', NEW.id, 'update');
RETURN NULL; RETURN NULL;
@ -2449,8 +2504,8 @@ BEGIN
VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id; VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id;
-- Update pertienent session variables. -- Update pertienent session variables.
PERFORM set_config('kea.audit_revision_id', cast(audit_revision_id as text), false); PERFORM set_session_value('kea.audit_revision_id', audit_revision_id);
PERFORM set_config('kea.cascade_transaction', cast(cascade_transaction as text), false); PERFORM set_session_value('kea.cascade_transaction', cascade_transaction);
END IF; END IF;
RETURN; RETURN;
END;$$; END;$$;
@ -2592,7 +2647,7 @@ BEGIN
-- Note this does not work prior to Postgres 9.2 unless the variables are -- Note this does not work prior to Postgres 9.2 unless the variables are
-- defined in postgresql.conf. I think for now we put up with CB not supported -- defined in postgresql.conf. I think for now we put up with CB not supported
-- prior to 9.2 or we tell people how to edit the conf file. -- prior to 9.2 or we tell people how to edit the conf file.
PERFORM set_config('kea.depend_on_known_indirectly', cast(depend_on_known_indirectly as text), false); PERFORM set_session_value('kea.depend_on_known_indirectly', depend_on_known_indirectly);
-- Bail if the class is updated without re-positioning. -- Bail if the class is updated without re-positioning.
IF( IF(
@ -2709,8 +2764,8 @@ CREATE TRIGGER dhcp6_client_class_AINS
-- ----------------------------------------------------------------------- -- -----------------------------------------------------------------------
CREATE OR REPLACE FUNCTION func_dhcp6_client_class_AUPD() RETURNS trigger AS $dhcp6_client_class_AUPD$ CREATE OR REPLACE FUNCTION func_dhcp6_client_class_AUPD() RETURNS trigger AS $dhcp6_client_class_AUPD$
BEGIN BEGIN
PERFORM set_config('kea.depend_on_known_directly', cast(OLD.depend_on_known_directly as text), false); PERFORM set_session_value('kea.depend_on_known_directly', OLD.depend_on_known_directly);
PERFORM set_config('kea.client_class_id', cast(NEW.id as text), false); PERFORM set_session_value('kea.client_class_id', NEW.id);
PERFORM setClientClass6Order(NEW.id, NEW.follow_class_name, OLD.follow_class_name); PERFORM setClientClass6Order(NEW.id, NEW.follow_class_name, OLD.follow_class_name);
PERFORM createAuditEntryDHCP6('dhcp6_client_class', NEW.id, 'update'); PERFORM createAuditEntryDHCP6('dhcp6_client_class', NEW.id, 'update');
RETURN NULL; RETURN NULL;

View File

@ -165,5 +165,6 @@ DROP FUNCTION IF EXISTS modification_ts_update();
DROP FUNCTION IF EXISTS get_session_small_int(name text); DROP FUNCTION IF EXISTS get_session_small_int(name text);
DROP FUNCTION IF EXISTS get_session_big_int(name text); DROP FUNCTION IF EXISTS get_session_big_int(name text);
DROP FUNCTION IF EXISTS get_session_value(name text); DROP FUNCTION IF EXISTS get_session_value(name text);
DROP FUNCTION IF EXISTS set_session_value(name text, value TEXT);
DROP FUNCTION IF EXISTS set_session_value(name text, value BIGINT);
DROP FUNCTION IF EXISTS set_session_value(name text, value SMALLINT);

View File

@ -721,13 +721,66 @@ CREATE TRIGGER dhcp4_audit_modification_ts_update
CREATE INDEX dhcp4_audit_idx1 ON dhcp4_audit (modification_type); CREATE INDEX dhcp4_audit_idx1 ON dhcp4_audit (modification_type);
CREATE INDEX dhcp4_audit_idx2 ON dhcp4_audit (revision_id); CREATE INDEX dhcp4_audit_idx2 ON dhcp4_audit (revision_id);
-- Stores a TEXT value to a session variable
-- name name of session variable to set
-- value TEXT value to store
CREATE OR REPLACE FUNCTION set_session_value(name text, value TEXT)
RETURNS VOID
AS \$\$
DECLARE
BEGIN
PERFORM set_config(name, value, false);
RETURN;
EXCEPTION
WHEN OTHERS THEN
RAISE EXCEPTION 'set_session_value(%) : value:[%] failed, sqlstate: %', name, value, sqlstate;
END;\$\$
LANGUAGE plpgsql;
-- Stores a BIGINT value to a session variable
-- Note the value converted to TEXT and then stored as Postgresql does
-- not support any other data type in session variables.
-- name name of session variable to set
-- value BIGINT value to store
CREATE OR REPLACE FUNCTION set_session_value(name text, value BIGINT)
RETURNS VOID
AS \$\$
BEGIN
PERFORM set_config(name, cast(value as text), false);
RETURN;
EXCEPTION
WHEN OTHERS THEN
RAISE EXCEPTION 'set_session_value(%) : value:[%] failed, sqlstate: %', name, value, sqlstate;
END;\$\$
LANGUAGE plpgsql;
-- Stores a SMALLINT value to a session variable
-- Note the value converted to TEXT and then stored as Postgresql does
-- not support any other data type in session variables.
-- name name of session variable to set
-- value SMALLINT value to store
CREATE OR REPLACE FUNCTION set_session_value(name text, value SMALLINT)
RETURNS VOID
AS \$\$
BEGIN
PERFORM set_config(name, cast(value as text), false);
RETURN;
EXCEPTION
WHEN OTHERS THEN
RAISE EXCEPTION 'set_session_value(%) : value:[%] failed, sqlstate: %', name, value, sqlstate;
END;\$\$
LANGUAGE plpgsql;
-- Fetches a text value from the session configuration. -- Fetches a text value from the session configuration.
-- param name name of the session variable to fetch -- param name name of the session variable to fetch
-- If the name is not found it returns NULL. -- If the name is not found it returns NULL.
-- Postgresql allows you to store custom session values -- Postgresql allows you to store custom session values
-- but throws an exception if they have not first been -- but throws an exception if they have not first been
-- set. This allows us to be a bit more graceful. -- set. This allows us to be a bit more graceful.
CREATE OR REPLACE FUNCTION get_session_value(name text) CREATE OR REPLACE FUNCTION get_session_value(name TEXT)
RETURNS TEXT RETURNS TEXT
AS \$\$ AS \$\$
DECLARE DECLARE
@ -735,11 +788,12 @@ DECLARE
BEGIN BEGIN
text_value = current_setting(name); text_value = current_setting(name);
RETURN(text_value); RETURN(text_value);
EXCEPTION EXCEPTION
WHEN OTHERS THEN WHEN undefined_object THEN
RAISE NOTICE 'get_session_value(%) failed, sqlstate: %', name, sqlstate; -- Variable has not been initialized so return NULL
RETURN NULL; RETURN NULL;
WHEN OTHERS THEN
RAISE EXCEPTION 'get_session_value(%, TEXT) failed, sqlstate: %', name, sqlstate;
END;\$\$ END;\$\$
LANGUAGE plpgsql; LANGUAGE plpgsql;
@ -846,8 +900,8 @@ BEGIN
VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id; VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id;
-- Update pertienent session variables. -- Update pertienent session variables.
PERFORM set_config('kea.audit_revision_id', cast(audit_revision_id as text), false); PERFORM set_session_value('kea.audit_revision_id', audit_revision_id);
PERFORM set_config('kea.cascade_transaction', cast(cascade_transaction as text), false); PERFORM set_session_value('kea.cascade_transaction', cascade_transaction);
END IF; END IF;
RETURN; RETURN;
END;\$\$; END;\$\$;
@ -994,7 +1048,7 @@ BEGIN
-- Note this does not work prior to Postgres 9.2 unless the variables are -- Note this does not work prior to Postgres 9.2 unless the variables are
-- defined in postgresql.conf. I think for now we put up with CB not supported -- defined in postgresql.conf. I think for now we put up with CB not supported
-- prior to 9.2 or we tell people how to edit the conf file. -- prior to 9.2 or we tell people how to edit the conf file.
PERFORM set_config('kea.depend_on_known_indirectly', cast(depend_on_known_indirectly as text), false); PERFORM set_session_value('kea.depend_on_known_indirectly', depend_on_known_indirectly);
-- Bail if the class is updated without re-positioning. -- Bail if the class is updated without re-positioning.
IF( IF(
@ -1112,8 +1166,8 @@ CREATE TRIGGER dhcp4_client_class_AINS
-- ----------------------------------------------------------------------- -- -----------------------------------------------------------------------
CREATE OR REPLACE FUNCTION func_dhcp4_client_class_AUPD() RETURNS trigger AS \$dhcp4_client_class_AUPD\$ CREATE OR REPLACE FUNCTION func_dhcp4_client_class_AUPD() RETURNS trigger AS \$dhcp4_client_class_AUPD\$
BEGIN BEGIN
PERFORM set_config('kea.depend_on_known_directly', cast(OLD.depend_on_known_directly as text), false); PERFORM set_session_value('kea.depend_on_known_directly', OLD.depend_on_known_directly);
PERFORM set_config('kea.client_class_id', cast(NEW.id as text), false); PERFORM set_session_value('kea.client_class_id', NEW.id);
PERFORM setClientClass4Order(NEW.id, NEW.follow_class_name, OLD.follow_class_name); PERFORM setClientClass4Order(NEW.id, NEW.follow_class_name, OLD.follow_class_name);
PERFORM createAuditEntryDHCP4('dhcp4_client_class', NEW.id, 'update'); PERFORM createAuditEntryDHCP4('dhcp4_client_class', NEW.id, 'update');
RETURN NULL; RETURN NULL;
@ -1455,8 +1509,8 @@ BEGIN
VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id; VALUES (audit_ts, srv_id, audit_log_message) returning id INTO audit_revision_id;
-- Update pertienent session variables. -- Update pertienent session variables.
PERFORM set_config('kea.audit_revision_id', cast(audit_revision_id as text), false); PERFORM set_session_value('kea.audit_revision_id', audit_revision_id);
PERFORM set_config('kea.cascade_transaction', cast(cascade_transaction as text), false); PERFORM set_session_value('kea.cascade_transaction', cascade_transaction);
END IF; END IF;
RETURN; RETURN;
END;\$\$; END;\$\$;
@ -1598,7 +1652,7 @@ BEGIN
-- Note this does not work prior to Postgres 9.2 unless the variables are -- Note this does not work prior to Postgres 9.2 unless the variables are
-- defined in postgresql.conf. I think for now we put up with CB not supported -- defined in postgresql.conf. I think for now we put up with CB not supported
-- prior to 9.2 or we tell people how to edit the conf file. -- prior to 9.2 or we tell people how to edit the conf file.
PERFORM set_config('kea.depend_on_known_indirectly', cast(depend_on_known_indirectly as text), false); PERFORM set_session_value('kea.depend_on_known_indirectly', depend_on_known_indirectly);
-- Bail if the class is updated without re-positioning. -- Bail if the class is updated without re-positioning.
IF( IF(
@ -1715,8 +1769,8 @@ CREATE TRIGGER dhcp6_client_class_AINS
-- ----------------------------------------------------------------------- -- -----------------------------------------------------------------------
CREATE OR REPLACE FUNCTION func_dhcp6_client_class_AUPD() RETURNS trigger AS \$dhcp6_client_class_AUPD\$ CREATE OR REPLACE FUNCTION func_dhcp6_client_class_AUPD() RETURNS trigger AS \$dhcp6_client_class_AUPD\$
BEGIN BEGIN
PERFORM set_config('kea.depend_on_known_directly', cast(OLD.depend_on_known_directly as text), false); PERFORM set_session_value('kea.depend_on_known_directly', OLD.depend_on_known_directly);
PERFORM set_config('kea.client_class_id', cast(NEW.id as text), false); PERFORM set_session_value('kea.client_class_id', NEW.id);
PERFORM setClientClass6Order(NEW.id, NEW.follow_class_name, OLD.follow_class_name); PERFORM setClientClass6Order(NEW.id, NEW.follow_class_name, OLD.follow_class_name);
PERFORM createAuditEntryDHCP6('dhcp6_client_class', NEW.id, 'update'); PERFORM createAuditEntryDHCP6('dhcp6_client_class', NEW.id, 'update');
RETURN NULL; RETURN NULL;