forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hdb_catalog.current_setting abstraction for reading Hasura settings
As the comment in the function’s definition explains, this is needed to work around an awkward Postgres behavior.
- Loading branch information
1 parent
bf37b39
commit 66e85ab
Showing
5 changed files
with
124 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
37 | ||
38 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
CREATE FUNCTION hdb_catalog.current_setting(setting_name text) RETURNS json | ||
LANGUAGE plpgsql AS $$ | ||
DECLARE | ||
server_version_num int; | ||
setting_value text; | ||
BEGIN | ||
server_version_num := current_setting('server_version_num'); | ||
IF server_version_num >= 90600 THEN | ||
setting_value := current_setting('hasura.' || setting_name, true); | ||
ELSE | ||
-- We still support Postgres 9.5, which doesn’t support the second | ||
-- argument to current_setting, so emulate it by just catching the error. | ||
BEGIN | ||
setting_value := current_setting('hasura.' || setting_name); | ||
EXCEPTION WHEN OTHERS THEN | ||
setting_value := NULL; | ||
END; | ||
END IF; | ||
-- Note: we test for the empty string here to work around a Postgres bug. | ||
-- When SET LOCAL is used to set the value of a setting that does not | ||
-- currently exist, Postgres implicitly creates a /session-wide/ definition | ||
-- of the setting with the empty string as its value. This is annoying, but | ||
-- fortunately an empty string is not a legal JSON value, so it always means | ||
-- no value was set in the current transaction. | ||
IF setting_value IS NULL OR setting_value = '' THEN | ||
RETURN NULL; | ||
ELSE | ||
RETURN setting_value::json; | ||
END IF; | ||
END $$; | ||
|
||
CREATE OR REPLACE FUNCTION hdb_catalog.insert_event_log(schema_name text, table_name text, trigger_name text, op text, row_data json) | ||
RETURNS text LANGUAGE plpgsql AS $$ | ||
DECLARE | ||
id text; | ||
payload json; | ||
BEGIN | ||
id := gen_random_uuid(); | ||
payload := json_build_object( | ||
'op', op, | ||
'data', row_data, | ||
'session_variables', hdb_catalog.current_setting('user'), | ||
'trace_context', hdb_catalog.current_setting('tracecontext') | ||
); | ||
INSERT INTO hdb_catalog.event_log | ||
(id, schema_name, table_name, trigger_name, payload) | ||
VALUES (id, schema_name, table_name, trigger_name, payload); | ||
RETURN id; | ||
END $$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
CREATE OR REPLACE FUNCTION hdb_catalog.insert_event_log(schema_name text, table_name text, trigger_name text, op text, row_data json) | ||
RETURNS text AS $$ | ||
DECLARE | ||
id text; | ||
payload json; | ||
session_variables json; | ||
server_version_num int; | ||
trace_context json; | ||
BEGIN | ||
id := gen_random_uuid(); | ||
server_version_num := current_setting('server_version_num'); | ||
IF server_version_num >= 90600 THEN | ||
session_variables := current_setting('hasura.user', 't'); | ||
trace_context := current_setting('hasura.tracecontext', 't'); | ||
ELSE | ||
BEGIN | ||
session_variables := current_setting('hasura.user'); | ||
EXCEPTION WHEN OTHERS THEN | ||
session_variables := NULL; | ||
END; | ||
BEGIN | ||
trace_context := current_setting('hasura.tracecontext'); | ||
EXCEPTION WHEN OTHERS THEN | ||
trace_context := NULL; | ||
END; | ||
END IF; | ||
payload := json_build_object( | ||
'op', op, | ||
'data', row_data, | ||
'session_variables', session_variables, | ||
'trace_context', trace_context | ||
); | ||
INSERT INTO hdb_catalog.event_log | ||
(id, schema_name, table_name, trigger_name, payload) | ||
VALUES | ||
(id, schema_name, table_name, trigger_name, payload); | ||
RETURN id; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
DROP FUNCTION hdb_catalog.current_setting(text); |