Hydraulic structures: Remove obsolete hydraulic structure output fields

new_design_pol
Dylan Jeannin 2026-09-01 14:02:53 +02:00
parent a81a641d41
commit 32db5795da
3 changed files with 64 additions and 111 deletions

View File

@ -47,9 +47,7 @@ class HydraulicStructure(SQLSubModel):
self._name = name self._name = name
self._input_section = None self._input_section = None
self._output_section = None
self._input_reach = None self._input_reach = None
self._output_reach = None
self._enabled = True self._enabled = True
self._data = [] self._data = []
@ -62,17 +60,12 @@ class HydraulicStructure(SQLSubModel):
name TEXT NOT NULL, name TEXT NOT NULL,
enabled BOOLEAN NOT NULL, enabled BOOLEAN NOT NULL,
input_reach INTEGER, input_reach INTEGER,
output_reach INTEGER,
input_section INTEGER, input_section INTEGER,
output_section INTEGER,
{Scenario.create_db_add_scenario()}, {Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()}, {Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(input_reach) REFERENCES river_reach(pamhyr_id), FOREIGN KEY(input_reach) REFERENCES river_reach(pamhyr_id),
FOREIGN KEY(output_reach) REFERENCES river_reach(pamhyr_id),
FOREIGN KEY(input_section) FOREIGN KEY(input_section)
REFERENCES geometry_profileXYZ(pamhyr_id), REFERENCES geometry_profileXYZ(pamhyr_id),
FOREIGN KEY(output_section)
REFERENCES geometry_profileXYZ(pamhyr_id),
PRIMARY KEY(pamhyr_id, scenario) PRIMARY KEY(pamhyr_id, scenario)
) )
""") """)
@ -92,13 +85,12 @@ class HydraulicStructure(SQLSubModel):
return True return True
if rl < 11: if rl < 11:
for v in ["input", "output"]: execute(
execute( """
f""" ALTER TABLE hydraulic_structures
ALTER TABLE hydraulic_structures RENAME COLUMN input_kp TO input_rk
RENAME COLUMN {v}_kp TO {v}_rk """
""" )
)
cls._db_update_to_0_2_0(execute, data) cls._db_update_to_0_2_0(execute, data)
@ -112,8 +104,28 @@ class HydraulicStructure(SQLSubModel):
"ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE" "ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE"
) )
if major == "0" and (int(minor) < 2 or
(minor == "2" and rl < 10)):
cls._db_update_to_0_2_10(execute)
return cls._update_submodel(execute, version, data) return cls._update_submodel(execute, version, data)
@classmethod
def _db_update_to_0_2_10(cls, execute):
table = "hydraulic_structures"
cls._db_create(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, deleted, name, enabled, input_section, " +
"input_reach, scenario) " +
"SELECT pamhyr_id, deleted, name, enabled, input_section, " +
f"input_reach, scenario FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod @classmethod
def _db_update_to_0_2_0(cls, execute, data): def _db_update_to_0_2_0(cls, execute, data):
table = "hydraulic_structures" table = "hydraulic_structures"
@ -131,11 +143,10 @@ class HydraulicStructure(SQLSubModel):
execute( execute(
f"INSERT INTO {table}_tmp " + f"INSERT INTO {table}_tmp " +
"(pamhyr_id, name, enabled, input_section, output_section, " + "(pamhyr_id, name, enabled, input_section, " +
"input_reach, output_reach, scenario) " + "input_reach, scenario) " +
"SELECT pamhyr_id, name, enabled, " + "SELECT pamhyr_id, name, enabled, " +
"input_section, output_section, " + "input_section, input_reach, scenario " +
"input_reach, output_reach, scenario " +
f"FROM {table}" f"FROM {table}"
) )
@ -145,44 +156,38 @@ class HydraulicStructure(SQLSubModel):
@classmethod @classmethod
def _db_update_to_0_2_0_set_reach_pid(cls, execute, table, reachs): def _db_update_to_0_2_0_set_reach_pid(cls, execute, table, reachs):
els = execute( els = execute(
f"SELECT pamhyr_id, input_reach, output_reach FROM {table}" f"SELECT pamhyr_id, input_reach FROM {table}"
) )
for row in els: for row in els:
it = iter(row) it = iter(row)
pid = next(it) pid = next(it)
in_reach_id = next(it) in_reach_id = next(it)
out_reach_id = next(it)
if in_reach_id == -1: if in_reach_id == -1:
return return
if out_reach_id == -1:
out_reach_id = in_reach_id
execute( execute(
f"UPDATE {table} " + f"UPDATE {table} " +
f"SET input_reach = {reachs[in_reach_id]}, " + f"SET input_reach = {reachs[in_reach_id]} " +
f"output_reach = {reachs[out_reach_id]} " +
f"WHERE pamhyr_id = {pid}" f"WHERE pamhyr_id = {pid}"
) )
@classmethod @classmethod
def _db_update_to_0_1_1(cls, execute, data, def _db_update_to_0_1_1(cls, execute, data,
origin_version="0.1.0"): origin_version="0.1.0"):
for v in ["input", "output"]: execute(
execute( "ALTER TABLE hydraulic_structures " +
"ALTER TABLE hydraulic_structures " + "ADD COLUMN input_section INTEGER"
f"ADD COLUMN {v}_section INTEGER" )
)
cls._db_update_to_0_1_1_assoc_section_from_rk( cls._db_update_to_0_1_1_assoc_section_from_rk(
execute, "hydraulic_structures", execute, "hydraulic_structures",
reach_column=f"{v}_reach", reach_column="input_reach",
rk_column=f"{v}_rk", rk_column="input_rk",
section_column=f"{v}_section", section_column="input_section",
origin_version=origin_version origin_version=origin_version
) )
@classmethod @classmethod
def _db_load(cls, execute, data=None): def _db_load(cls, execute, data=None):
@ -196,8 +201,7 @@ class HydraulicStructure(SQLSubModel):
table = execute( table = execute(
"SELECT pamhyr_id, deleted, name, enabled, " + "SELECT pamhyr_id, deleted, name, enabled, " +
"input_section, output_section, " + "input_section, input_reach, scenario " +
"input_reach, output_reach, scenario " +
"FROM hydraulic_structures " + "FROM hydraulic_structures " +
f"WHERE scenario = {scenario.id} " + f"WHERE scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})" f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})"
@ -227,12 +231,7 @@ class HydraulicStructure(SQLSubModel):
input_section_id = ( input_section_id = (
-1 if input_section_id is None else input_section_id -1 if input_section_id is None else input_section_id
) )
output_section_id = next(it)
output_section_id = (
-1 if output_section_id is None else output_section_id
)
input_reach_id = next(it) input_reach_id = next(it)
output_reach_id = next(it)
owner_scenario = next(it) owner_scenario = next(it)
hs = cls( hs = cls(
@ -243,27 +242,19 @@ class HydraulicStructure(SQLSubModel):
hs.set_as_deleted() hs.set_as_deleted()
hs.enabled = enabled hs.enabled = enabled
hs.input_reach, hs.output_reach = reduce( hs.input_reach = reduce(
lambda acc, n: ( lambda acc, n: n if n.pamhyr_id == input_reach_id else acc,
n if n.pamhyr_id == input_reach_id else acc[0],
n if n.pamhyr_id == output_reach_id else acc[1]
),
data["edges"], data["edges"],
[None, None] None
) )
sections = [] sections = []
if hs.input_reach is not None: if hs.input_reach is not None:
sections += hs.input_reach.reach.profiles sections += hs.input_reach.reach.profiles
if hs.output_reach is not None:
sections += hs.output_reach.reach.profiles
hs.input_section, hs.output_section = reduce( hs.input_section = reduce(
lambda acc, s: ( lambda acc, s: s if s.pamhyr_id == input_section_id else acc,
s if s.pamhyr_id == input_section_id else acc[0],
s if s.pamhyr_id == output_section_id else acc[1]
),
sections, sections,
[None, None] None
) )
loaded.add(hs_id) loaded.add(hs_id)
@ -287,24 +278,15 @@ class HydraulicStructure(SQLSubModel):
if self._input_reach is not None: if self._input_reach is not None:
input_reach_id = self._input_reach.pamhyr_id input_reach_id = self._input_reach.pamhyr_id
output_reach_id = -1
if self._output_reach is not None:
output_reach_id = self._output_reach.pamhyr_id
input_section = 'NULL' input_section = 'NULL'
if self.input_section is not None: if self.input_section is not None:
input_section = self.input_section.pamhyr_id input_section = self.input_section.pamhyr_id
output_section = 'NULL'
if self.output_section is not None:
output_section = self.output_section.pamhyr_id
execute( execute(
"INSERT INTO " + "INSERT INTO " +
"hydraulic_structures(" + "hydraulic_structures(" +
" pamhyr_id, deleted, name, enabled, " + " pamhyr_id, deleted, name, enabled, " +
" input_section, output_section, " + " input_section, input_reach, " +
" input_reach, output_reach, " +
" scenario" + " scenario" +
") " + ") " +
"VALUES (" + "VALUES (" +
@ -312,8 +294,7 @@ class HydraulicStructure(SQLSubModel):
f"{self._db_format(self.is_deleted())}, " + f"{self._db_format(self.is_deleted())}, " +
f"'{self._db_format(self._name)}', " + f"'{self._db_format(self._name)}', " +
f"{self._db_format(self.enabled)}, " + f"{self._db_format(self.enabled)}, " +
f"{input_section}, {output_section}, " + f"{input_section}, {input_reach_id}, " +
f"{input_reach_id}, {output_reach_id}, " +
f"{self._status.scenario_id}" f"{self._status.scenario_id}"
")" ")"
) )
@ -371,13 +352,6 @@ class HydraulicStructure(SQLSubModel):
return self._input_section return self._input_section
@property
def output_rk(self):
if self._output_section is None:
return None
return self._output_section
@property @property
def input_section(self): def input_section(self):
return self._input_section return self._input_section
@ -387,15 +361,6 @@ class HydraulicStructure(SQLSubModel):
self._input_section = input_section self._input_section = input_section
self.modified() self.modified()
@property
def output_section(self):
return self._output_section
@output_section.setter
def output_section(self, output_section):
self._output_section = output_section
self.modified()
@property @property
def enabled(self): def enabled(self):
return self._enabled return self._enabled
@ -414,27 +379,15 @@ class HydraulicStructure(SQLSubModel):
self._input_reach = input_reach self._input_reach = input_reach
self.modified() self.modified()
@property
def output_reach(self):
return self._output_reach
@output_reach.setter
def output_reach(self, output_reach):
self._output_reach = output_reach
self.modified()
@property @property
def basic_structures(self): def basic_structures(self):
return self.lst.copy() return self.lst.copy()
def cloned_for(self, input_reach, input_section, def cloned_for(self, input_reach, input_section):
output_reach, output_section):
new = HydraulicStructure(name=self._name, status=self._status) new = HydraulicStructure(name=self._name, status=self._status)
new._enabled = self._enabled new._enabled = self._enabled
new._input_reach = input_reach new._input_reach = input_reach
new._input_section = input_section new._input_section = input_section
new._output_reach = output_reach
new._output_section = output_section
new._data = [structure.cloned() for structure in self.lst] new._data = [structure.cloned() for structure in self.lst]
new.modified() new.modified()
return new return new

View File

@ -87,8 +87,7 @@ class HydraulicStructureList(PamhyrModelList):
structures = [ structures = [
structure structure
for structure in self.lst for structure in self.lst
if (structure.input_reach is reach or if structure.input_reach is reach
structure.output_reach is reach)
] ]
for structure in structures: for structure in structures:
@ -96,15 +95,9 @@ class HydraulicStructureList(PamhyrModelList):
structure.input_reach, structure.input_reach,
structure.input_section structure.input_section
) )
output_reach, output_section = split_endpoint(
structure.output_reach,
structure.output_section
)
self._lst.append(structure.cloned_for( self._lst.append(structure.cloned_for(
input_reach, input_reach,
input_section, input_section
output_reach,
output_section
)) ))
if len(structures) != 0: if len(structures) != 0:

View File

@ -46,7 +46,7 @@ logger = logging.getLogger()
class Study(SQLModel): class Study(SQLModel):
_version = "0.2.9" _version = "0.2.10"
_sub_classes = [ _sub_classes = [
Scenario, Scenario,
@ -290,7 +290,14 @@ class Study(SQLModel):
if major == "0" and int(minor) < 2: if major == "0" and int(minor) < 2:
self._add_into_info_if_not_exists('current_scenario', '0') self._add_into_info_if_not_exists('current_scenario', '0')
if major == "0" and int(minor) < 2: needs_foreign_keys_disabled = (
major == "0" and (
int(minor) < 2 or
(minor == "2" and int(release) < 10)
)
)
if needs_foreign_keys_disabled:
# Need to temporary disable the sqlite foreign keys # Need to temporary disable the sqlite foreign keys
# checking to update db dans change the table id fk to # checking to update db dans change the table id fk to
# table pamhyr_id fk # table pamhyr_id fk
@ -300,7 +307,7 @@ class Study(SQLModel):
ok = self._update_submodel(version[0], data={}) ok = self._update_submodel(version[0], data={})
if major == "0" and int(minor) < 2: if needs_foreign_keys_disabled:
# Reactivate foreign keys checking # Reactivate foreign keys checking
self.execute( self.execute(
"PRAGMA foreign_keys = ON;" "PRAGMA foreign_keys = ON;"