diff --git a/src/Model/Geometry/ProfileXYZ.py b/src/Model/Geometry/ProfileXYZ.py index 0731bdb2..a2edf1c6 100644 --- a/src/Model/Geometry/ProfileXYZ.py +++ b/src/Model/Geometry/ProfileXYZ.py @@ -123,6 +123,7 @@ class ProfileXYZ(Profile, SQLSubModel): @classmethod def _db_update_to_0_1_0(cls, execute, data): table = "geometry_profileXYZ" + reachs = data['id2pid']['river_reach'] cls.update_db_add_pamhyr_id(execute, table, data) Scenario.update_db_add_scenario(execute, table) @@ -141,6 +142,8 @@ class ProfileXYZ(Profile, SQLSubModel): execute(f"DROP TABLE {table}") execute(f"ALTER TABLE {table}_tmp RENAME TO {table}") + cls._db_update_to_0_1_0_set_reach_pid(execute, table, reachs) + @classmethod def _db_load(cls, execute, data=None): profiles = [] diff --git a/src/Model/HydraulicStructures/HydraulicStructures.py b/src/Model/HydraulicStructures/HydraulicStructures.py index d5d21c25..7be1def9 100644 --- a/src/Model/HydraulicStructures/HydraulicStructures.py +++ b/src/Model/HydraulicStructures/HydraulicStructures.py @@ -104,6 +104,7 @@ class HydraulicStructure(SQLSubModel): @classmethod def _db_update_to_0_1_0(cls, execute, data): table = "hydraulic_structures" + reachs = data['id2pid']['river_reach'] cls.update_db_add_pamhyr_id(execute, table, data) Scenario.update_db_add_scenario(execute, table) @@ -122,6 +123,33 @@ class HydraulicStructure(SQLSubModel): execute(f"DROP TABLE {table}") execute(f"ALTER TABLE {table}_tmp RENAME TO {table}") + cls._db_update_to_0_1_0_set_reach_pid(execute, table, reachs) + + @classmethod + def _db_update_to_0_1_0_set_reach_pid(cls, execute, table, reachs): + els = execute( + f"SELECT pamhyr_id, input_reach, output_reach FROM {table}" + ) + + for row in els: + it = iter(row) + pid = next(it) + in_reach_id = next(it) + out_reach_id = next(it) + + if in_reach_id == -1: + return + + if out_reach_id == -1: + out_reach_id = in_reach_id + + execute( + f"UPDATE {table} " + + f"SET input_reach = {reachs[in_reach_id]}, " + + f"output_reach = {reachs[out_reach_id]} " + + f"WHERE pamhyr_id = {pid}" + ) + @classmethod def _db_load(cls, execute, data=None): new = [] diff --git a/src/Model/InitialConditions/InitialConditions.py b/src/Model/InitialConditions/InitialConditions.py index 254d366b..52cd7f6b 100644 --- a/src/Model/InitialConditions/InitialConditions.py +++ b/src/Model/InitialConditions/InitialConditions.py @@ -93,6 +93,7 @@ class Data(SQLSubModel): @classmethod def _db_update_to_0_1_0(cls, execute, data): table = "initial_conditions" + reachs = data['id2pid']['river_reach'] cls.update_db_add_pamhyr_id(execute, table, data) Scenario.update_db_add_scenario(execute, table) @@ -111,6 +112,8 @@ class Data(SQLSubModel): execute(f"DROP TABLE {table}") execute(f"ALTER TABLE {table}_tmp RENAME TO {table}") + cls._db_update_to_0_1_0_set_reach_pid(execute, table, reachs) + @classmethod def _db_load(cls, execute, data=None): id = data["reach"].pamhyr_id diff --git a/src/Model/LateralContribution/LateralContribution.py b/src/Model/LateralContribution/LateralContribution.py index 95f99f23..e3bec9c6 100644 --- a/src/Model/LateralContribution/LateralContribution.py +++ b/src/Model/LateralContribution/LateralContribution.py @@ -214,6 +214,7 @@ class LateralContribution(SQLSubModel): @classmethod def _db_update_to_0_1_0(cls, execute, data): table = "lateral_contribution" + reachs = data['id2pid']['river_reach'] cls.update_db_add_pamhyr_id(execute, table, data) Scenario.update_db_add_scenario(execute, table) @@ -230,6 +231,8 @@ class LateralContribution(SQLSubModel): execute(f"DROP TABLE {table}") execute(f"ALTER TABLE {table}_tmp RENAME TO {table}") + cls._db_update_to_0_1_0_set_reach_pid(execute, table, reachs) + @classmethod def _get_ctor_from_type(cls, t): from Model.LateralContribution.LateralContributionTypes import ( diff --git a/src/Model/Tools/PamhyrDB.py b/src/Model/Tools/PamhyrDB.py index 05175e51..0466db48 100644 --- a/src/Model/Tools/PamhyrDB.py +++ b/src/Model/Tools/PamhyrDB.py @@ -216,6 +216,9 @@ class SQLSubModel(PamhyrID): pid = next(it) node_id = next(it) + if node_id == -1: + return + execute( f"UPDATE {table} " + f"SET node = {nodes[node_id]} " + @@ -233,6 +236,9 @@ class SQLSubModel(PamhyrID): pid = next(it) reach_id = next(it) + if reach_id == -1: + return + execute( f"UPDATE {table} " + f"SET reach = {reachs[reach_id]} " +