Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set obj_id sequences when importing #223

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datamodel/app/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def create_app(

run_sql("CREATE SCHEMA tww_app;", pg_service)

run_sql_file("functions/oid_functions.sql", pg_service, variables)
run_sql_file("functions/oid_functions.sql", pg_service)
run_sql_file("functions/modification_functions.sql", pg_service)
run_sql_file("functions/symbology_functions.sql", pg_service)
run_sql_file("functions/reach_direction_change.sql", pg_service, variables)
Expand Down
45 changes: 45 additions & 0 deletions datamodel/app/functions/oid_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,48 @@ END;
$BODY$
LANGUAGE plpgsql STABLE
COST 100;

CREATE OR REPLACE FUNCTION tww_app.reset_od_seqval()
RETURNS void
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE

tbl_name text;
rgx text;
BEGIN
BEGIN
FOR tbl_name,rgx IN (
SELECT dot.tablename,pfx.prefix||dot.shortcut_en
FROM information_schema.sequences seq
LEFT JOIN tww_sys.dictionary_od_table dot ON seq.sequence_name = 'seq_'||dot.tablename||'_oid'
LEFT JOIN (SELECT prefix FROM tww_sys.oid_prefixes WHERE active) pfx on True
WHERE seq.sequence_schema = 'tww_od' AND dot.tablename IS NOT NULL) LOOP
EXECUTE FORMAT('SELECT SETVAL(''tww_od.seq_%1$I_oid'',(SELECT max(seqs) FROM(
SELECT RIGHT(obj_id, 6)::int as seqs FROM tww_od.%1$I WHERE regexp_match(obj_id, ''%2$s\d{6}$'') IS NOT NULL
UNION
SELECT last_value as seqs FROM tww_od.seq_%1$I_oid)foo));',tbl_name,rgx);
END LOOP;
END;
END;
$BODY$;

CREATE OR REPLACE FUNCTION tww_app.tr_reset_od_seqval()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
BEGIN
PERFORM tww_app.reset_od_seqval();
RETURN NULL;
END;
$BODY$;

CREATE TRIGGER update_od_seqval_from_prefixes
AFTER UPDATE
ON tww_sys.oid_prefixes
FOR EACH STATEMENT
EXECUTE FUNCTION tww_app.tr_reset_od_seqval();
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def interlis_import(self, xtf_file_input, show_selection_dialog=False, logs_next
tww_session.commit()
tww_session.close()

# Update the sequence values
self._progress_done(92, "Update sequence values...")
self._import_set_od_sequences()

# Update main_cover and main_wastewater_node
self._progress_done(95, "Update main cover and refresh materialized views...")
self._import_update_main_cover_and_refresh_mat_views()
Expand Down Expand Up @@ -262,6 +266,10 @@ def _import_from_intermediate_schema(self, import_model):

return interlisImporterToIntermediateSchema.session_tww

def _import_set_od_sequences(self):
logger.info("Set Sequence values")
DatabaseUtils.execute("SELECT tww_app.reset_od_seqval();")

def _import_manage_organisations(self):
logger.info("Update organisation tww_active")
DatabaseUtils.execute("SELECT tww_app.set_organisations_active();")
Expand Down