Branchwell

AI Resource Pro / website / scripts / create-exec-sql-function.sql.txt

1-- SQL to create the exec_sql function
2-- Open Supabase Dashboard > SQL Editor and execute this script
3
4-- Create a function to execute SQL statements
5-- This function requires superuser privileges
6-- It should be executed by a Supabase admin
7
8CREATE OR REPLACE FUNCTION exec_sql(sql text)
9RETURNS JSONB
10LANGUAGE plpgsql
11SECURITY DEFINER
12AS $$
13DECLARE
14 result JSONB;
15BEGIN
16 EXECUTE sql;
17 result := jsonb_build_object('success', true, 'message', 'SQL executed successfully');
18 RETURN result;
19EXCEPTION WHEN OTHERS THEN
20 result := jsonb_build_object(
21 'success', false,
22 'message', 'Error executing SQL',
23 'error', SQLERRM,
24 'state', SQLSTATE
25 );
26 RETURN result;
27END;
28$$;
29
30-- Grant execute permission to the service_role
31GRANT EXECUTE ON FUNCTION exec_sql(text) TO service_role;
32
33-- Comment on the function
34COMMENT ON FUNCTION exec_sql(text) IS 'Executes arbitrary SQL. Use with caution - requires service_role key.';
35
36-- After creating this function, you can run:
37-- npm run supabase:fix-recursion