diff --git a/src/Model/BoundaryCondition/BoundaryCondition.py b/src/Model/BoundaryCondition/BoundaryCondition.py index 90f4936a..6b8ce3c3 100644 --- a/src/Model/BoundaryCondition/BoundaryCondition.py +++ b/src/Model/BoundaryCondition/BoundaryCondition.py @@ -74,6 +74,7 @@ class Data(SQLSubModel): @classmethod def _db_update_to_0_1_0(cls, execute, data): table = "boundary_condition_data" + bcs = data['id2pid']['boundary_condition'] cls.update_db_add_pamhyr_id( execute, table, @@ -92,6 +93,25 @@ 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_bc_pid(execute, table, bcs) + + @classmethod + def _db_update_to_0_1_0_set_bc_pid(cls, execute, table, bcs): + els = execute( + f"SELECT pamhyr_id, bc FROM {table}" + ) + + for row in els: + it = iter(row) + pid = next(it) + bc_id = next(it) + + execute( + f"UPDATE {table} " + + f"SET bc = {bcs[bc_id]}, " + + f"WHERE pamhyr_id = {pid}" + ) + @classmethod def _db_load(cls, execute, data=None): new = [] @@ -208,23 +228,7 @@ class BoundaryCondition(SQLSubModel): execute(f"DROP TABLE {table}") execute(f"ALTER TABLE {table}_tmp RENAME TO {table}") - cls._db_update_to_0_1_0_set_node_pid(execute, nodes) - - @classmethod - def _db_update_to_0_1_0_set_node_pid(cls, execute, nodes): - bcs = execute( - "SELECT pamhyr_id, node FROM boundary_condition" - ) - - for row in bcs: - pid = row[0] - node_id = row[1] - - execute( - "UPDATE boundary_condition " + - f"SET node = {nodes[node_id]} " + - f"WHERE pamhyr_id = {pid}" - ) + cls._db_update_to_0_1_0_set_node_pid(execute, table, nodes) @classmethod def _get_ctor_from_type(cls, t): diff --git a/src/Model/Friction/Friction.py b/src/Model/Friction/Friction.py index 74f445ef..5f8a9a37 100644 --- a/src/Model/Friction/Friction.py +++ b/src/Model/Friction/Friction.py @@ -77,6 +77,7 @@ class Friction(SQLSubModel): @classmethod def _db_update_to_0_1_0(cls, execute, data): table = "friction" + reachs = data['id2pid']['river_reach'] cls.update_db_add_pamhyr_id(execute, table, data) Scenario.update_db_add_scenario(execute, table) @@ -95,6 +96,8 @@ class Friction(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): new = [] diff --git a/src/Model/Reservoir/Reservoir.py b/src/Model/Reservoir/Reservoir.py index 5addc096..5fd20725 100644 --- a/src/Model/Reservoir/Reservoir.py +++ b/src/Model/Reservoir/Reservoir.py @@ -63,6 +63,7 @@ class Data(SQLSubModel): @classmethod def _db_update_to_0_1_0(cls, execute, data): table = "reservoir_data" + reservoirs = data['id2pid']['reservoir'] cls.update_db_add_pamhyr_id( execute, table, @@ -81,6 +82,25 @@ 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_reservoir_pid(execute, table, reservoirs) + + @classmethod + def _db_update_to_0_1_0_set_reservoir_pid(cls, execute, table, reservoirs): + bcs = execute( + f"SELECT pamhyr_id, reservoir FROM {table}" + ) + + for row in bcs: + it = iter(row) + pid = next(it) + reservoir_id = next(it) + + execute( + f"UPDATE {table} " + + f"SET reservoir = {reservoirs[reservoir_id]}, " + + f"WHERE pamhyr_id = {pid}" + ) + @classmethod def _db_load(cls, execute, data=None): new = [] @@ -199,23 +219,6 @@ class Reservoir(SQLSubModel): cls._db_update_to_0_1_0_set_node_pid(execute, table, nodes) - @classmethod - def _db_update_to_0_1_0_set_node_pid(cls, execute, table, nodes): - bcs = execute( - f"SELECT pamhyr_id, node FROM {table}" - ) - - for row in bcs: - it = iter(row) - pid = next(it) - node_id = next(it) - - execute( - f"UPDATE {table} " + - f"SET node = {nodes[node_id]}, " + - f"WHERE pamhyr_id = {pid}" - ) - @classmethod def _db_load(cls, execute, data=None): new = [] diff --git a/src/Model/Tools/PamhyrDB.py b/src/Model/Tools/PamhyrDB.py index 93d7dde9..c6e9d37f 100644 --- a/src/Model/Tools/PamhyrDB.py +++ b/src/Model/Tools/PamhyrDB.py @@ -204,6 +204,41 @@ class SQLSubModel(PamhyrID): """ raise NotImplementedMethodeError(cls, cls._db_update) + + @classmethod + def _db_update_to_0_1_0_set_node_pid(cls, execute, table, nodes): + els = execute( + f"SELECT pamhyr_id, node FROM {table}" + ) + + for row in els: + it = iter(row) + pid = next(it) + node_id = next(it) + + execute( + f"UPDATE {table} " + + f"SET node = {nodes[node_id]}, " + + f"WHERE pamhyr_id = {pid}" + ) + + @classmethod + def _db_update_to_0_1_0_set_reach_pid(cls, execute, table, reachs): + els = execute( + f"SELECT pamhyr_id, reach FROM {table}" + ) + + for row in els: + it = iter(row) + pid = next(it) + reach_id = next(it) + + execute( + f"UPDATE {table} " + + f"SET reach = {reachs[reach_id]}, " + + f"WHERE pamhyr_id = {pid}" + ) + @classmethod def _db_load(cls, execute, data=None): """Load instance of this class from SQL data base