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 | |
| 8 | CREATE OR REPLACE FUNCTION exec_sql(sql text) |
| 9 | RETURNS JSONB |
| 10 | LANGUAGE plpgsql |
| 11 | SECURITY DEFINER |
| 12 | AS $$ |
| 13 | DECLARE |
| 14 | result JSONB; |
| 15 | BEGIN |
| 16 | EXECUTE sql; |
| 17 | result := jsonb_build_object('success', true, 'message', 'SQL executed successfully'); |
| 18 | RETURN result; |
| 19 | EXCEPTION 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; |
| 27 | END; |
| 28 | $$; |
| 29 | |
| 30 | -- Grant execute permission to the service_role |
| 31 | GRANT EXECUTE ON FUNCTION exec_sql(text) TO service_role; |
| 32 | |
| 33 | -- Comment on the function |
| 34 | COMMENT 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 |