Skip to content

Commit 5dc0a34

Browse files
committed
fix: add accumulated_weather_per_month function and fix RLS for trees_adopted
- Recreated SQL function `accumulated_weather_per_month` to aggregate weather data by month. - Implemented a policy for authenticated users to select their own adoptions in the `trees_adopted` table. - Dropped the previous policy allowing everyone to select all adoptions for better security. These changes should fix a diff between staging and production
1 parent 0964ed2 commit 5dc0a34

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
CREATE OR REPLACE FUNCTION public.accumulated_weather_per_month(limit_monts integer) RETURNS TABLE(
2+
measure_day text,
3+
sum_precipitation_mm_per_sqm double precision,
4+
avg_temperature_celsius double precision,
5+
avg_pressure_msl double precision,
6+
sum_sunshine_minutes double precision,
7+
avg_wind_direction_deg double precision,
8+
avg_wind_speed_kmh double precision,
9+
avg_cloud_cover_percentage double precision,
10+
avg_dew_point_celcius double precision,
11+
avg_relative_humidity_percentage double precision,
12+
avg_visibility_m double precision,
13+
avg_wind_gust_direction_deg double precision,
14+
avg_wind_gust_speed_kmh double precision
15+
) LANGUAGE 'plpgsql' COST 100 VOLATILE PARALLEL UNSAFE ROWS 1000 AS $BODY$ BEGIN RETURN query
16+
SELECT to_char(daily_weather_data.measure_day, 'YYYY-MM'),
17+
sum(daily_weather_data.sum_precipitation_mm_per_sqm) AS sum_precipitation_mm_per_sqm,
18+
avg(daily_weather_data.avg_temperature_celsius) AS avg_temperature_celsius,
19+
avg(daily_weather_data.avg_pressure_msl) AS avg_pressure_msl,
20+
sum(daily_weather_data.sum_sunshine_minutes) AS sum_sunshine_minutes,
21+
avg(daily_weather_data.avg_wind_direction_deg) AS avg_wind_direction_deg,
22+
avg(daily_weather_data.avg_wind_speed_kmh) AS avg_wind_speed_kmh,
23+
avg(daily_weather_data.avg_cloud_cover_percentage) AS avg_cloud_cover_percentage,
24+
avg(daily_weather_data.avg_dew_point_celcius) AS avg_dew_point_celcius,
25+
avg(
26+
daily_weather_data.avg_relative_humidity_percentage
27+
) AS avg_relative_humidity_percentage,
28+
avg(daily_weather_data.avg_visibility_m) AS avg_visibility_m,
29+
avg(daily_weather_data.avg_wind_gust_direction_deg) AS avg_wind_gust_direction_deg,
30+
avg(daily_weather_data.avg_wind_gust_speed_kmh) AS avg_wind_gust_speed_kmh
31+
FROM daily_weather_data
32+
GROUP BY to_char(daily_weather_data.measure_day, 'YYYY-MM')
33+
ORDER BY to_char(daily_weather_data.measure_day, 'YYYY-MM') DESC
34+
LIMIT limit_monts;
35+
END;
36+
$BODY$;
37+
ALTER FUNCTION public.accumulated_weather_per_month(integer) OWNER TO postgres;
38+
GRANT EXECUTE ON FUNCTION public.accumulated_weather_per_month(integer) TO PUBLIC;
39+
GRANT EXECUTE ON FUNCTION public.accumulated_weather_per_month(integer) TO anon;
40+
GRANT EXECUTE ON FUNCTION public.accumulated_weather_per_month(integer) TO authenticated;
41+
GRANT EXECUTE ON FUNCTION public.accumulated_weather_per_month(integer) TO postgres;
42+
GRANT EXECUTE ON FUNCTION public.accumulated_weather_per_month(integer) TO service_role;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE POLICY "Authenticated users can select their own adoptions again - staging fix" ON public.trees_adopted AS PERMISSIVE FOR
2+
SELECT TO authenticated USING (((auth.uid())::text = uuid));
3+
DROP POLICY IF EXISTS "Everyon can select all adoptions" ON public.trees_adopted;

0 commit comments

Comments
 (0)