teksi / wastewater

[DEV] Future TEKSI wastewater module, adapted data model to fit VSA-DSS 2020 new standard
https://teksi.github.io/wastewater
GNU General Public License v3.0
2 stars 5 forks source link

Sequence number not correct after import #117

Open urskaufmann opened 8 months ago

urskaufmann commented 8 months ago

As we have already found a solution for the sequence number in qgep (https://github.com/QGEP/QGEP/issues/788) there is the same problem after migrating a qgep_database to tww_database: the sequence numbers must be updated (or we change the oid_prefix). I tried to adapt the function and sql from QGEP issue 788, but get an error because of tww_od.network_node does not exist (materialized views are in tww_app instead of tww_od). Should be fixed and the function installed in the database before users start migrating ...

cymed commented 8 months ago

The problem is that that function can only be called after an import from dss. My suggestion is to migrate the data into the same database as before (as there is no overlap in schema names that is no problem) and then call

SELECT setval(tww.schemaname'.'tww.sequencename, qgep.last_value, true)
FROM
(SELECT * --sequence_schema, sequence_name 
FROM pg_sequences
WHERE schemaname='tww_od'
) tww
LEFT JOIN
(SELECT * --sequence_schema, sequence_name 
FROM pg_sequences 
WHERE schemaname='qgep_od'
)qgep on qgep.sequencename=tww.sequencename
WHERE qgep.last_value is not null;

Network node and segment to be updated in two separate selects

urskaufmann commented 8 months ago

we will use this function also after import of not teksi-data.

The problem is not that tww_od.network_node not exists, but tww_od.network_node has no obj_id-field (just an id-field). In QGEP, the table was in qgep_network - schema and was therefor not a problem. It works like this:

do $$ declare f record; mytables_rec record; begin for f in SELECT tablename, hastriggers FROM pg_catalog.pg_tables WHERE schemaname = 'twwod' and NOT tablename like 'network%' and NOT tablename like 're_%' ORDER BY tablename loop raise notice '%', f.tablename; -- SELECT tww_sys.adjust_max_sequence ('mutation'); EXECUTE 'SELECT tww_sys.adjust_max_sequence ' || '(''' || f.tablename || ''')' INTO mytables_rec; end loop; end; $$

My solution: add the function to the datamodell and let the user start the adjust.sql (automatic after import and if oid_prefix is changed) adjust_max_sequence_tww.zip

By the way: the sequence-nr of this network-tables can/should be reset to 1 everytime the network topology is refreshed