Repository navigation
Expand file tree
/
Copy pathcreate-missing-rpc-functions.sql
More file actions
60 lines (56 loc) · 1.71 KB
/
Copy pathcreate-missing-rpc-functions.sql
File metadata and controls
60 lines (56 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- Create missing RPC functions for the test enrollment page
-- Function to check if a trigger exists
CREATE OR REPLACE FUNCTION check_trigger_exists(trigger_name TEXT)
RETURNS BOOLEAN AS $$
BEGIN
RETURN EXISTS (
SELECT 1
FROM information_schema.triggers
WHERE trigger_name = $1
);
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Function to get trigger information
CREATE OR REPLACE FUNCTION get_trigger_info(trigger_name TEXT)
RETURNS TABLE(
trigger_name TEXT,
table_name TEXT,
trigger_timing TEXT,
trigger_event TEXT,
function_name TEXT
) AS $$
BEGIN
RETURN QUERY
SELECT
t.trigger_name::TEXT,
t.event_object_table::TEXT,
t.action_timing::TEXT,
t.event_manipulation::TEXT,
t.action_statement::TEXT
FROM information_schema.triggers t
WHERE t.trigger_name = $1;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Function to check database health
CREATE OR REPLACE FUNCTION check_database_health()
RETURNS TABLE(
table_name TEXT,
row_count BIGINT,
has_rls BOOLEAN
) AS $$
BEGIN
RETURN QUERY
SELECT
t.tablename::TEXT,
(SELECT COUNT(*) FROM pg_stat_user_tables WHERE relname = t.tablename)::BIGINT,
t.rowsecurity::BOOLEAN
FROM pg_tables t
WHERE t.schemaname = 'public'
AND t.tablename IN ('profiles', 'tracks', 'cohorts', 'student_enrollments', 'week_progress', 'paid_learner_whitelist')
ORDER BY t.tablename;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Grant execute permissions
GRANT EXECUTE ON FUNCTION check_trigger_exists(TEXT) TO authenticated;
GRANT EXECUTE ON FUNCTION get_trigger_info(TEXT) TO authenticated;
GRANT EXECUTE ON FUNCTION check_database_health() TO authenticated;