diff --git a/etc/db/schema-template.sql b/etc/db/schema-template.sql index 8fed11e..d261cf2 100644 --- a/etc/db/schema-template.sql +++ b/etc/db/schema-template.sql @@ -63,6 +63,38 @@ If the hash matches that of an existing entry, update the path of that entry to If the path matches that of an existing entry, delete that entry (which cascades) and insert the new one.'; +-- +-- Name: assoc_tstamp(); Type: FUNCTION; Schema: _SURVEY__TEMPLATE_; Owner: postgres +-- + +CREATE FUNCTION _SURVEY__TEMPLATE_.assoc_tstamp() RETURNS trigger + LANGUAGE plpgsql + AS $$ +DECLARE +geom jsonb; +BEGIN +IF NOT (NEW.meta ? 'geometry') THEN +-- Get the geometry from the real time event +-- closest in time and not more than one minute +-- away. +SELECT geometry::jsonb +INTO geom +FROM real_time_inputs rti +WHERE rti.tstamp <-> NEW.tstamp < interval '1 minute' +ORDER BY rti.tstamp <-> NEW.tstamp +LIMIT 1; + +IF geom IS NOT NULL THEN +NEW.meta := jsonb_set(NEW.meta, '{geometry}', geom); +END IF; +END IF; +RETURN NEW; +END; +$$; + + +ALTER FUNCTION _SURVEY__TEMPLATE_.assoc_tstamp() OWNER TO postgres; + -- -- Name: binning_parameters(); Type: FUNCTION; Schema: _SURVEY__TEMPLATE_; Owner: postgres -- @@ -1489,6 +1521,13 @@ CREATE TRIGGER events_tg AFTER INSERT OR DELETE OR UPDATE ON _SURVEY__TEMPLATE_. CREATE TRIGGER events_timed_seq_match_tg AFTER INSERT OR UPDATE ON _SURVEY__TEMPLATE_.events_timed FOR EACH ROW EXECUTE FUNCTION _SURVEY__TEMPLATE_.events_timed_seq_match(); +-- +-- Name: events_timed events_timed_tg; Type: TRIGGER; Schema: _SURVEY__TEMPLATE_; Owner: postgres +-- + +CREATE TRIGGER events_timed_tg BEFORE INSERT OR UPDATE ON _SURVEY__TEMPLATE_.events_timed FOR EACH ROW EXECUTE FUNCTION _SURVEY__TEMPLATE_.assoc_tstamp(); + + -- -- Name: events_seq_labels events_seq_labels_id_fkey; Type: FK CONSTRAINT; Schema: _SURVEY__TEMPLATE_; Owner: postgres --