-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfunction-clear-api_log.sql
42 lines (33 loc) · 1.14 KB
/
function-clear-api_log.sql
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
CREATE OR REPLACE FUNCTION miovision_api.clear_api_log(
_start_date date,
_end_date date,
intersections integer [] DEFAULT ARRAY[]::integer []
)
RETURNS void
LANGUAGE plpgsql
VOLATILE
COST 100
AS $BODY$
DECLARE
target_intersections integer [] = miovision_api.get_intersections_uids(intersections);
n_deleted integer;
BEGIN
WITH deleted AS (
DELETE FROM miovision_api.api_log
WHERE
intersection_uid = ANY(target_intersections)
AND start_date >= _start_date
AND end_date < _end_date
RETURNING *
)
-- FOR NOTICE PURPOSES ONLY
SELECT COUNT(*) INTO n_deleted
FROM deleted;
RAISE NOTICE 'Deleted % rows from miovision_api.api_log.', n_deleted;
END;
$BODY$;
ALTER FUNCTION miovision_api.clear_api_log(date, date, integer []) OWNER TO miovision_admins;
GRANT EXECUTE ON FUNCTION miovision_api.clear_api_log(date, date, integer []) TO miovision_api_bot;
COMMENT ON FUNCTION miovision_api.clear_api_log(date, date, integer [])
IS '''Clears data from `miovision_api.api_log` in order to facilitate re-pulling.
`intersections` param defaults to all intersections.''';