merge scenario (missing tables)

disable_edition_parent_scenario
Dylan Jeannin 2026-05-20 10:18:56 +02:00
commit 2dd9003721
5 changed files with 107 additions and 38 deletions

View File

@ -71,7 +71,7 @@ class Data(SQLSubModel):
major, minor, release = version.strip().split(".") major, minor, release = version.strip().split(".")
created = False created = False
if major == "0" and int(minor) <= 1: if major == "0" and minor == "0":
if int(release) < 7: if int(release) < 7:
cls._db_create(execute) cls._db_create(execute)
created = True created = True
@ -249,7 +249,7 @@ class BoundaryConditionAdisTS(SQLSubModel):
major, minor, release = version.strip().split(".") major, minor, release = version.strip().split(".")
created = False created = False
if major == "0" and int(minor) <= 1: if major == "0" and minor == "0":
if int(release) < 7: if int(release) < 7:
cls._db_create(execute) cls._db_create(execute)
created = True created = True

View File

@ -278,8 +278,10 @@ class PollutantCharacteristics(SQLSubModel):
class Pollutants(SQLSubModel): class Pollutants(SQLSubModel):
_sub_classes = [PollutantCharacteristics, _sub_classes = [
BoundaryConditionAdisTS] PollutantCharacteristics,
# BoundaryConditionAdisTS
]
def __init__(self, id: int = -1, name: str = "", def __init__(self, id: int = -1, name: str = "",
status=None, owner_scenario=-1): status=None, owner_scenario=-1):
@ -426,10 +428,6 @@ class Pollutants(SQLSubModel):
new_pollutant._data = PollutantCharacteristics._db_load( new_pollutant._data = PollutantCharacteristics._db_load(
execute, data=data execute, data=data
) )
new_pollutant._boundary_conditions_adists = BoundaryConditionAdisTS._db_load(
execute, data=data
)
new_pollutant._boundary_conditions_adists = ( new_pollutant._boundary_conditions_adists = (
BoundaryConditionAdisTS._db_load( BoundaryConditionAdisTS._db_load(

View File

@ -17,16 +17,51 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import logging
from tools import logger_exception from tools import logger_exception
from Model.Tools.PamhyrDB import SQLSubModel from Model.Tools.PamhyrDB import SQLSubModel
logger = logging.getLogger()
class Scenario(SQLSubModel): class Scenario(SQLSubModel):
_id_cnt = 0 _id_cnt = 0
_sub_classes = [] _sub_classes = []
tables_with_deleted_column = [
# Adists
"output_rk_adists",
"boundary_condition_adists", "boundary_condition_data_adists",
"lateral_contribution_adists", "lateral_contribution_data_adists",
"initial_conditions_adists", "initial_conditions_adists_data",
"d90_adists", "d90_adists_spec",
"dif_adists_spec",
"pollutants", "pollutants_characteristics",
# Hydraulic
"additional_files",
"boundary_condition", "boundary_condition_data",
"lateral_contribution", "lateral_contribution_data",
"friction", "stricklers",
"hydraulic_structures",
"hydraulic_structures_basic",
"initial_conditions",
"sedimentary_layer", "sedimentary_layer_layer",
"reservoir", "reservoir_data",
"rep_lines",
"geometry_pointXYZ", "geometry_profileXYZ",
"river_reach", "river_node",
"geotiff", "reservoir", "reservoir_data",
]
related_tables = tables_with_deleted_column + [
"dif_adists",
"solver_parameter",
"hydraulic_structures_basic_value",
"results", "results_data", "results_add_data",
]
def __init__(self, def __init__(self,
id: int = -1, id: int = -1,
name: str = "", name: str = "",
@ -171,33 +206,7 @@ class Scenario(SQLSubModel):
def drop_all(self, execute): def drop_all(self, execute):
execute(f"DELETE FROM scenario WHERE id = {self.id}") execute(f"DELETE FROM scenario WHERE id = {self.id}")
tables = [ tables = self.related_tables
# Adists
"output_rk_adists",
"boundary_condition_adists", "boundary_condition_data_adists",
"lateral_contribution_adists", "lateral_contribution_data_adists",
"initial_conditions_adists", "initial_conditions_adists_data",
"d90_adists", "d90_adists_spec",
"dif_adists", "dif_adists_spec",
"pollutants", "pollutants_characteristics",
# Hydraulic
"additional_files",
"boundary_condition", "boundary_condition_data",
"lateral_contribution", "lateral_contribution_data",
"friction", "stricklers",
"hydraulic_structures",
"hydraulic_structures_basic", "hydraulic_structures_basic_value",
"initial_conditions",
"sedimentary_layer", "sedimentary_layer_layer",
"reservoir", "reservoir_data",
"rep_lines",
"solver_parameter",
"geometry_pointXYZ", "geometry_profileXYZ",
"river_reach", "river_node",
"geotiff", "reservoir", "reservoir_data",
# Results
"results", "results_data", "results_add_data",
]
for table in tables: for table in tables:
execute( execute(
@ -207,6 +216,58 @@ class Scenario(SQLSubModel):
return True return True
def get_parent_branch(self):
def aux(scenario, acc):
if scenario is None:
return acc
return aux(scenario.parent, [scenario.id] + acc)
return aux(self, [])
def drop_deleted_data(self, execute):
tables = self.tables_with_deleted_column
branch = self.get_parent_branch()
for table in tables:
if self.parent is None:
# This scenario is the default scenario, so we can
# delete all data marked as deleted
execute(
f"DELETE FROM {table} " +
f"WHERE scenario = {self.id} " +
"AND deleted = TRUE"
)
continue
# Select pamhyr_id for each deleted data in this scenario
# who do not exists into parents scenarios
ids = execute(
f"""
SELECT pamhyr_id FROM {table}
WHERE deleted = TRUE
AND scenario = {self.id}
AND pamhyr_id NOT IN (
SELECT pamhyr_id FROM {table}
WHERE scenario IN
({', '.join(map(str, branch[:-1]))})
)
""", fetch_one=False
)
if ids is None or len(ids) == 0:
continue
logger.debug(
f"(s{self.id}) Drop deleted data into '{table}' : {ids}"
)
execute(
f"DELETE FROM {table} " +
f"WHERE scenario = {self.id} " +
"AND pamhyr_id IN " +
f"({', '.join(map(lambda x: str(x[0]), ids))})"
)
@property @property
def id(self): def id(self):
return self._id return self._id

View File

@ -449,6 +449,13 @@ class Study(SQLModel):
[self.scenarios, self._river], [self.scenarios, self._river],
data=progress data=progress
) )
# Clear DB for each scenarios
for scenar in self.scenarios.lst:
scenar.drop_deleted_data(self.execute)
progress()
self.commit() self.commit()
def sql_save_request_count(self, *args, **kargs): def sql_save_request_count(self, *args, **kargs):
@ -459,7 +466,7 @@ class Study(SQLModel):
[self.scenarios, self._river] [self.scenarios, self._river]
) )
logger.debug(cnt) logger.debug(cnt)
return cnt + 7 return cnt + 8
def close(self): def close(self):
"""Close db connection """Close db connection
@ -518,7 +525,8 @@ class Study(SQLModel):
data["study"] = self data["study"] = self
river._db_load_results(sql_exec, data=data) river._db_load_results(sql_exec, data=data)
self._river_scenario_cache[scenario] = river # FIXME: Disable scenario cache to save memory usage
# self._river_scenario_cache[scenario] = river
self.status.scenario = old_scenario self.status.scenario = old_scenario
return river return river

View File

@ -845,7 +845,9 @@ class AdisTSwc(AdisTS):
# seventh line (useless) # seventh line (useless)
for k in range(0, ismax, kbl): for k in range(0, ismax, kbl):
data = np.fromfile(f, dtype=np.int32, count=1) # (start) data = np.fromfile(f, dtype=np.int32, count=1) # (start)
zero = np.fromfile(f, dtype=np.int32, count=min(k + kbl, ismax) - k) zero = np.fromfile(
f, dtype=np.int32, count=min(k + kbl, ismax) - k
)
data = np.fromfile(f, dtype=np.int32, count=1) # (end) data = np.fromfile(f, dtype=np.int32, count=1) # (end)
# end header # end header
# data # data