Skip to content

Commit

Permalink
Handle current_setting parameters with empty strings in insert_event_log
Browse files Browse the repository at this point in the history
PR-URL: hasura/graphql-engine-mono#11143
GitOrigin-RevId: a04c8c9d04557817a1249de78ef5c6f915d3e706
  • Loading branch information
rakeshkky authored and hasura-bot committed Feb 17, 2025
1 parent 0406058 commit 13dd224
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ initialSourceCatalogVersion :: SourceCatalogVersion pgKind
initialSourceCatalogVersion = Version.SourceCatalogVersion 0

latestSourceCatalogVersion :: SourceCatalogVersion pgKind
latestSourceCatalogVersion = Version.SourceCatalogVersion 3
latestSourceCatalogVersion = Version.SourceCatalogVersion 4

previousSourceCatalogVersions :: [SourceCatalogVersion pgKind]
previousSourceCatalogVersions = [initialSourceCatalogVersion .. pred latestSourceCatalogVersion]
Expand Down
7 changes: 5 additions & 2 deletions server/src-rsr/init_pg_source.sql
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ CREATE OR REPLACE FUNCTION
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');
-- In some cases postgres sets the setting to an empty string, which is not a valid json.
-- NULLIF will convert the empty string to NULL.
-- Ref: https://github.com/hasura/graphql-engine/issues/8498
session_variables := NULLIF(current_setting('hasura.user', 't'), '');
trace_context := NULLIF(current_setting('hasura.tracecontext', 't'), '');
ELSE
BEGIN
session_variables := current_setting('hasura.user');
Expand Down
43 changes: 43 additions & 0 deletions server/src-rsr/pg_source_migrations/3_to_4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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
-- In some cases postgres sets the setting to an empty string, which is not a valid json.
-- NULLIF will convert the empty string to NULL.
-- Ref: https://github.com/hasura/graphql-engine/issues/8498
session_variables := NULLIF(current_setting('hasura.user', 't'), '');
trace_context := NULLIF(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;

0 comments on commit 13dd224

Please sign in to comment.