Adists: IC: Fix update for scenario.

scenarios
Pierre-Antoine 2025-08-15 14:11:21 +02:00
parent 6e79fe4cd5
commit 6cce02650b
2 changed files with 25 additions and 18 deletions

View File

@ -78,12 +78,16 @@ class InitialConditionsAdisTS(SQLSubModel):
@classmethod
def _db_update(cls, execute, version, data=None):
major, minor, release = version.strip().split(".")
created = False
if major == "0" and int(minor) <= 1:
if int(release) < 6:
cls._db_create(execute)
created = True
elif major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
if major == "0" and int(minor) < 2:
if not created:
cls._db_update_to_0_2_0(execute, data)
return True
@ -95,22 +99,20 @@ class InitialConditionsAdisTS(SQLSubModel):
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca(execute, ext="_tmp")
cls._db_create(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, pollutant, name, concentration, " +
"eg, em, ed, scenario) " +
"eg, em, ed, enabled, scenario) " +
"SELECT pamhyr_id, pollutant, name, concentration, " +
"eg, em, ed, scenario " +
"eg, em, ed, enabled, 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 = []
@ -161,7 +163,7 @@ class InitialConditionsAdisTS(SQLSubModel):
ic._data = ICAdisTSSpec._db_load(execute, data)
loaded.add(pid)
new.append(IC)
new.append(ic)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)

View File

@ -50,7 +50,7 @@ class ICAdisTSSpec(SQLSubModel):
@classmethod
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE initial_conditions_spec{ext}(
CREATE TABLE initial_conditions_adists_spec{ext}(
{cls.create_db_add_pamhyr_id()},
ic_default INTEGER NOT NULL,
name TEXT NOT NULL,
@ -76,27 +76,32 @@ class ICAdisTSSpec(SQLSubModel):
@classmethod
def _db_update(cls, execute, version, data=None):
major, minor, release = version.strip().split(".")
created = False
if major == 0 and minor < 1:
if int(release) < 6:
cls._db_create(execute)
created =True
elif major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
if major == "0" and int(minor) < 2:
if not created:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "initial_conditions_spec"
table_new = "initial_conditions_adists_spec"
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")
cls._db_create(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
f"INSERT INTO {table_new}_tmp " +
"(pamhyr_id, ic_default, name, reach, start_rk, end_rk, " +
"concentration, eg, em, ed, rate, enabled, scenario) " +
"SELECT pamhyr_id, ic_default, name, reach, start_rk, end_rk, " +
@ -105,9 +110,9 @@ class ICAdisTSSpec(SQLSubModel):
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
execute(f"ALTER TABLE {table_new}_tmp RENAME TO {table_new}")
cls._db_update_to_0_2_0_set_reach_pid(execute, table, reachs)
cls._db_update_to_0_2_0_set_reach_pid(execute, table_new, reachs)
@classmethod
def _db_load(cls, execute, data=None):
@ -121,9 +126,9 @@ class ICAdisTSSpec(SQLSubModel):
return new
table = execute(
"SELECT id, name, reach, start_rk, end_rk, " +
"SELECT pamhyr_id, name, reach, start_rk, end_rk, " +
"concentration, eg, em, ed, rate, enabled, scenario " +
"FROM initial_conditions_spec " +
"FROM initial_conditions_adists_spec " +
f"WHERE ic_default = {data['ic_default_id']} " +
f"AND scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
@ -179,7 +184,7 @@ class ICAdisTSSpec(SQLSubModel):
sql = (
"INSERT INTO " +
"initial_conditions_spec(id, ic_default, name, reach, " +
"initial_conditions_adists_spec(id, ic_default, name, reach, " +
"start_rk, end_rk, concentration, eg, em, ed, rate, " +
"enabled, scenario) " +
"VALUES (" +