diff --git a/scripts-available/CDB_PG_Federated_Tables.sql b/scripts-available/CDB_PG_Federated_Tables.sql index 46e6871..fdd08e4 100644 --- a/scripts-available/CDB_PG_Federated_Tables.sql +++ b/scripts-available/CDB_PG_Federated_Tables.sql @@ -2,6 +2,25 @@ -- Federated Tables management functions ---------------------------------------------------------------------- +-- Take a server_config jsonb and transform it to an input suitable +-- for _CDB_SetUp_User_PG_FDW_Server +CREATE OR REPLACE FUNCTION @extschema@.__cdb_credentials_to_user_mapping(input_config jsonb) +RETURNS jsonb +AS $$ +DECLARE + user_mapping jsonb; +BEGIN + user_mapping := json_build_object('user_mapping', + jsonb_build_object( + 'user', input_config->'credentials'->'username', + 'password', input_config->'credentials'->'password' + ) + ); + RETURN (input_config - 'credentials')::jsonb || user_mapping; +END +$$ +LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE; + -- -- Set up a federated server for later connection of tables/views @@ -17,11 +36,14 @@ -- "password": "secret" -- } -- }'); -CREATE OR REPLACE FUNCTION @extschema@.CDB_SetUp_PG_Federated_Server(server_alias text, server_config json) +CREATE OR REPLACE FUNCTION @extschema@.CDB_SetUp_PG_Federated_Server(server_alias text, server_config jsonb) RETURNS void AS $$ +DECLARE + final_config jsonb; BEGIN - PERFORM cartodb._CDB_SetUp_User_PG_FDW_Server(server_alias, server_config); + final_config := @extschema@.__cdb_credentials_to_user_mapping(server_config); + PERFORM cartodb._CDB_SetUp_User_PG_FDW_Server(server_alias, final_config::json); END $$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE; diff --git a/test/extension/test.sh b/test/extension/test.sh index eaef068..3581db8 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -683,12 +683,17 @@ function test_federated_tables() { "host": "localhost", "port": ${PGPORT:-5432} }, - "user_mapping": { - "user": "fdw_user", + "credentials": { + "username": "fdw_user", "password": "foobarino" } } EOF + + # Unit-test this helper method + sql postgres "SELECT cartodb.__cdb_credentials_to_user_mapping('$federated_server_config')" \ + should '{"server": {"host": "localhost", "port": 5432, "dbname": "fdw_target"}, "user_mapping": {"user": "fdw_user", "password": "foobarino"}}' + # There must be a function with the expected interface sql postgres "SELECT cartodb.CDB_SetUp_PG_Federated_Server('my_server', '$federated_server_config');" @@ -699,6 +704,7 @@ EOF sql cdb_testmember_1 'SELECT * from "cdb_fdw_my_server".foo;' sql cdb_testmember_1 'SELECT a from "cdb_fdw_my_server".foo LIMIT 1;' should 42 + # Tear down sql postgres "SELECT cartodb._CDB_Drop_User_PG_FDW_Server('my_server', /* force = */ true)" tear_down_fdw_target }