Commit 5a77f3fc authored by Rune Åvar Ødegård's avatar Rune Åvar Ødegård
Browse files

Fixed trigger_depth problem

parent dfb204b9
......@@ -5,4 +5,5 @@ xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="update_v1.sql" relativeToChangelogFile="true"/>
<include file="update_v2.sql" relativeToChangelogFile="true"/>
<include file="update_v3.sql" relativeToChangelogFile="true"/>
</databaseChangeLog>
\ No newline at end of file
CREATE OR REPLACE FUNCTION public.raven_update_time_and_fillmissing()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE
fromtime timestamp without time zone;
totime timestamp without time zone;
timesteplabel character varying;
step interval;
sone character varying;
BEGIN
IF pg_trigger_depth() <> 1 THEN
RETURN NEW;
END IF;
SELECT oc.from_time, oc.to_time, t.label
INTO fromtime, totime, timesteplabel
FROM observing_capabilities oc, eea_times t
WHERE sampling_point_id = NEW.sampling_point_id
AND oc.timestep = t.id;
-- UPDATE FROMTIME IN THE OBSERVING CAPABILITIES TABLE
IF fromtime IS NULL OR NEW.from_time < fromtime THEN
UPDATE observing_capabilities SET from_time = NEW.from_time WHERE sampling_point_id = NEW.sampling_point_id;
END IF;
-- UPDATE TOTIME IN THE OBSERVING CAPABILITIES TABLE
IF totime IS NULL OR NEW.to_time > totime THEN
UPDATE observing_capabilities SET to_time = NEW.to_time WHERE sampling_point_id = NEW.sampling_point_id;
END IF;
-- FILL IN MISSING GAPS IF NEEDED
-- CHECK FIRST IF TIMESTEP IS VARIABLE, IN THAT CASE, "FILL IN MISSING GAPS" WILL NOT BE DONE
SELECT et.label, ('1' || et.label)::interval INTO timesteplabel, step FROM observing_capabilities oc, eea_times et WHERE oc.timestep = et.id AND oc.sampling_point_id = NEW.sampling_point_id;
IF (timesteplabel) = 'Variable' THEN
RETURN NEW;
END IF;
-- "FILL IN MISSING GAPS" FOR THE OBSERVATION TIMESTEP BEFORE THIS TIME, IF IT IS MISSING
IF fromtime IS NOT NULL AND NEW.from_time > fromtime THEN
IF (SELECT COUNT(o.id) FROM observations o
WHERE o.sampling_point_id = NEW.sampling_point_id
AND o.from_time::timestamp = NEW.from_time::timestamp - step) = 0
THEN
sone = '+' || split_part(NEW.begin_position,'+', 2);
INSERT INTO public.observations(
sampling_point_id,
begin_position,
end_position,
value,
verification_flag,
validation_flag,
touched,
import_value)
VALUES (NEW.sampling_point_id,
to_char(NEW.from_time::timestamp - step, 'YYYY-MM-DD"T"HH24:MI:SS') || sone,
NEW.begin_position,
-9900,
3,
-1,
now(),
-9900);
END IF;
END IF;
RETURN NEW;
END;
$BODY$;
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment