Skip to content

Commit

Permalink
Merge pull request #223 from cymed/import_reset_sequence
Browse files Browse the repository at this point in the history
set obj_id sequences when importing
  • Loading branch information
ponceta authored Oct 25, 2024
2 parents 0c4ef9b + 967f466 commit 0af9641
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datamodel/app/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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

0 comments on commit 0af9641

Please sign in to comment.