function exists
Opened this issue · 1 comments
stevemmarshall commented
Hi
This a nice package that I stumbled upon....
I want to check if a function exists and here's my code.. i thought it might be a namespace issue but also tried to_char and it failed... so maybe I'm doing something dumb..
CREATE OR REPLACE FUNCTION unit_tests.reformat_date_exists()
RETURNS test_result
AS
$$
DECLARE message test_result;
BEGIN
IF 1 = 1 THEN
SELECT assert.function_exists('reformat_date') INTO message;
RETURN message;
END IF;
SELECT assert.ok('End of test.') INTO message;
RETURN message;
END
$$
LANGUAGE plpgsql;
orlando-colamatteo commented
You can use the built-in function assert.function_exists:
--use this to determine the signature of your function
SELECT replace(nspname || '.' || proname || '(' || oidvectortypes(proargtypes) || ')', ' ' , '')::text as function_signature
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p ON pronamespace = n.oid
WHERE nspname = 'assert' and proname = 'function_exists'; -- test values <-- replace with your schema and function name
--then feed the function_signature into assert.function_exists
SELECT * FROM assert.function_exists('<function_signature>')