From b5b91d41c90e93cc981408fc808c55b6211e9299 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Mon, 14 Sep 2020 01:36:40 +0200 Subject: [PATCH] Reset event serial ids after re-import. When the database is recreated, the sequences used in the events_timed and events_seq tables will be at their initial values, which will almost certainly conflict with existing data when it is imported via COPY. With this commit, we set the current value for those sequences to something usable. Fixes #33. --- bin/system_imports.py | 2 ++ etc/db/schema-template.sql | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/bin/system_imports.py b/bin/system_imports.py index 0627916..2f01cd2 100755 --- a/bin/system_imports.py +++ b/bin/system_imports.py @@ -46,5 +46,7 @@ if __name__ == '__main__': # If we don't commit the data does not actually get copied db.conn.commit() + # Update the sequences that generate event ids + cur.execute("SELECT reset_events_serials();") print("Done") diff --git a/etc/db/schema-template.sql b/etc/db/schema-template.sql index 49dad31..5c26149 100644 --- a/etc/db/schema-template.sql +++ b/etc/db/schema-template.sql @@ -90,6 +90,22 @@ All values are returned as being of type "text" and need casting before use. '; +-- +-- Name: reset_events_serials(); Type: FUNCTION; Schema: _SURVEY__TEMPLATE_; Owner: postgres +-- + +CREATE FUNCTION _SURVEY__TEMPLATE_.reset_events_serials() RETURNS void + LANGUAGE plpgsql + AS $$ +BEGIN +PERFORM setval('events_timed_id_seq', (SELECT max(id)+1 FROM events_timed)); +PERFORM setval('events_seq_id_seq', (SELECT max(id)+1 FROM events_seq)); +END; +$$; + + +ALTER FUNCTION _SURVEY__TEMPLATE_.reset_events_serials() OWNER TO postgres; + -- -- Name: to_binning_grid(public.geometry); Type: FUNCTION; Schema: _SURVEY__TEMPLATE_; Owner: postgres --