Adists: LC: Prepare update for scenarios.

scenarios
Pierre-Antoine 2025-08-12 10:44:58 +02:00
parent 5a6574921a
commit c43a26870e
2 changed files with 91 additions and 24 deletions

View File

@ -53,31 +53,42 @@ class LateralContributionAdisTS(SQLSubModel):
self._types = [self.time_convert, float]
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE lateral_contribution_adists(
id INTEGER NOT NULL PRIMARY KEY,
pollutant INTEGER NOT NULL,
edge INTEGER NOT NULL,
begin_rk REAL NOT NULL,
end_rk REAL NOT NULL,
FOREIGN KEY(pollutant) REFERENCES Pollutants(id),
FOREIGN KEY(edge) REFERENCES river_reach(id)
)
""")
execute("""
CREATE TABLE lateral_contribution_data_adists(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
data0 TEXT NOT NULL,
data1 TEXT NOT NULL,
lc INTEGER,
FOREIGN KEY(lc) REFERENCES lateral_contribution(id)
)
""")
def _db_create(cls, execute, ext=""):
cls._db_create_lca(execute, ext)
cls._db_create_lca_data(execute, ext)
return cls._create_submodel(execute)
@classmethod
def _db_create_lca(cls, execute, ext=""):
execute(f"""
CREATE TABLE lateral_contribution_adists{ext}(
{cls.create_db_add_pamhyr_id()},
pollutant INTEGER NOT NULL,
reach INTEGER NOT NULL,
begin_rk REAL NOT NULL,
end_rk REAL NOT NULL,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(pollutant) REFERENCES Pollutants(pamhyr_id),
FOREIGN KEY(reach) REFERENCES river_reach(pamhyr_id)
)
""")
@classmethod
def _db_create_lca_data(cls, execute, ext=""):
execute(f"""
CREATE TABLE lateral_contribution_data_adists{ext}(
{cls.create_db_add_pamhyr_id()},
data0 TEXT NOT NULL,
data1 TEXT NOT NULL,
lca INTEGER,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(lca) REFERENCES lateral_contribution_adists(pamhyr_id)
)
""")
@classmethod
def _db_update(cls, execute, version, data=None):
major, minor, release = version.strip().split(".")
@ -85,8 +96,58 @@ class LateralContributionAdisTS(SQLSubModel):
if int(release) < 7:
cls._db_create(execute)
if major == "0" and int(minor) <= 2:
if int(release) <= 0:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "lateral_contribution_adists"
reachs = data['id2pid']['river_reach']
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, pollutant, reach, begin_rk, end_rk, scenario) " +
"SELECT pamhyr_id, pollutant, edge, begin_rk, end_rk, scenario) " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
cls._db_update_to_0_2_0_set_reach_pid(execute, table, reachs)
@classmethod
def _db_update_to_0_2_0_data(cls, execute, data):
table = "lateral_contribution_data_adists"
reachs = data['id2pid']['river_reach']
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca_data(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, data0, data1, lca, scenario) " +
"SELECT pamhyr_id, data0, data1, lc, scenario) " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
cls._db_update_to_0_2_0_set_reach_pid(execute, table, reachs)
@classmethod
def _db_load(cls, execute, data=None):
new = []

View File

@ -45,8 +45,14 @@ class LateralContributionsAdisTSList(PamhyrModelList):
return new
def _db_save(self, execute, data=None):
execute("DELETE FROM lateral_contribution_adists")
execute("DELETE FROM lateral_contribution_data_adists")
execute(
"DELETE FROM lateral_contribution_adists" +
f"WHERE scenario = {self._status.scenario_id}"
)
execute(
"DELETE FROM lateral_contribution_data_adists" +
f"WHERE scenario = {self._status.scenario_id}"
)
if data is None:
data = {}