File tree Expand file tree Collapse file tree 2 files changed +6
-33
lines changed Expand file tree Collapse file tree 2 files changed +6
-33
lines changed Original file line number Diff line number Diff line change 11BEGIN ;
22
3- -- Drop helper functions (updated signatures due to composite type returns )
3+ -- Drop helper functions (now using RETURNS TABLE syntax )
44DROP FUNCTION IF EXISTS decrypt_api_key(BYTEA , TEXT );
55DROP FUNCTION IF EXISTS encrypt_api_key(TEXT , TEXT );
66
7- -- Drop composite types
8- DROP TYPE IF EXISTS decrypted_api_key_result;
9- DROP TYPE IF EXISTS encrypted_api_key_result;
10-
117-- Drop trigger and function
128DROP TRIGGER IF EXISTS update_api_keys_timestamp_trigger ON api_keys;
139DROP FUNCTION IF EXISTS update_api_keys_timestamp();
Original file line number Diff line number Diff line change @@ -42,55 +42,32 @@ CREATE INDEX IF NOT EXISTS idx_api_keys_name ON api_keys(name);
4242CREATE INDEX IF NOT EXISTS idx_api_keys_metadata ON api_keys USING gin(metadata);
4343CREATE INDEX IF NOT EXISTS idx_api_keys_deleted_at ON api_keys(deleted_at) WHERE deleted_at IS NULL ;
4444
45- -- Create composite types for function return values (so they can be tracked by Hasura)
46- DO $$
47- BEGIN
48- IF NOT EXISTS (
49- SELECT 1 FROM pg_type WHERE typname = ' encrypted_api_key_result'
50- ) THEN
51- CREATE TYPE encrypted_api_key_result AS (
52- encrypted_value BYTEA
53- );
54- END IF;
55- END $$;
56-
57- DO $$
58- BEGIN
59- IF NOT EXISTS (
60- SELECT 1 FROM pg_type WHERE typname = ' decrypted_api_key_result'
61- ) THEN
62- CREATE TYPE decrypted_api_key_result AS (
63- decrypted_value TEXT
64- );
65- END IF;
66- END $$;
67-
6845-- Helper functions for encryption/decryption (following secrets pattern)
69- -- Modified to return SETOF composite types so they return tables that Hasura can track
46+ -- Modified to return TABLE so they can be tracked by Hasura
7047CREATE OR REPLACE FUNCTION encrypt_api_key (
7148 p_value TEXT ,
7249 p_key TEXT
73- ) RETURNS SETOF encrypted_api_key_result AS $$
50+ ) RETURNS TABLE(encrypted_value BYTEA ) AS $$
7451BEGIN
7552 RETURN QUERY SELECT
7653 pgp_sym_encrypt(
7754 p_value,
7855 p_key,
7956 ' cipher-algo=aes256'
80- ) AS encrypted_value ;
57+ );
8158END;
8259$$ LANGUAGE plpgsql SECURITY DEFINER;
8360
8461CREATE OR REPLACE FUNCTION decrypt_api_key (
8562 p_encrypted_value BYTEA ,
8663 p_key TEXT
87- ) RETURNS SETOF decrypted_api_key_result AS $$
64+ ) RETURNS TABLE(decrypted_value TEXT ) AS $$
8865BEGIN
8966 RETURN QUERY SELECT
9067 pgp_sym_decrypt(
9168 p_encrypted_value,
9269 p_key
93- ) AS decrypted_value ;
70+ );
9471END;
9572$$ LANGUAGE plpgsql SECURITY DEFINER;
9673
You can’t perform that action at this time.
0 commit comments