Adists: Prepare for scenarios update (commit code is not working).

This commit is contained in:
parouby committed 2025-08-12 15:36:19 +02:00
1 parent c43a26870e
commit 37cf42c524
12 files changed
+362 -122

No files matched your search

+38 -14
View File
@@ -53,18 +53,20 @@ class DIFAdisTS(SQLSubModel):
self._data = []
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE dif_adists(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
method TEXT NOT NULL,
dif REAL NOT NULL,
b REAL,
c REAL,
enabled BOOLEAN NOT NULL
)
""")
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE dif_adists{ext}(
{cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL,
method TEXT NOT NULL,
dif REAL NOT NULL,
b REAL,
c REAL,
enabled BOOLEAN NOT NULL,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()}
)
""")
return cls._create_submodel(execute)
@@ -75,8 +77,30 @@ class DIFAdisTS(SQLSubModel):
if int(release) < 6:
cls._db_create(execute)
if major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "dif_adists"
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, name, method, dif, b, c, enabled, scenario) " +
"SELECT pamhyr_id, name, method, dif, b, c, enabled, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod
def _db_load(cls, execute, data=None):
new = []
@@ -137,7 +161,7 @@ class DIFAdisTS(SQLSubModel):
sql = (
"INSERT INTO " +
"dif_adists(" +
"id, name, method, dif, b, c, enabled" +
"pamhyr_id, name, method, dif, b, c, enabled" +
") " +
"VALUES (" +
f"{self.id}, '{self._db_format(self._name)}', " +
@@ -150,7 +174,7 @@ class DIFAdisTS(SQLSubModel):
data['dif_default_id'] = self.id
execute(
"DELETE FROM dif_spec " +
"DELETE FROM dif_adists_spec " +
f"WHERE dif_default = {self.id}"
)
+4 -1
View File
@@ -42,7 +42,10 @@ class DIFAdisTSList(PamhyrModelList):
return new
def _db_save(self, execute, data=None):
execute("DELETE FROM dif_adists")
execute(
"DELETE FROM dif_adists" +
f"WHERE scenario = {self._status.scenario_id}"
)
if data is None:
data = {}
+44 -16
View File
@@ -55,22 +55,24 @@ class DIFAdisTSSpec(SQLSubModel):
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE dif_spec(
id INTEGER NOT NULL PRIMARY KEY,
dif_default INTEGER NOT NULL,
method TEXT NOT NULL,
reach INTEGER NOT NULL,
start_rk REAL NOT NULL,
end_rk REAL NOT NULL,
dif REAL NOT NULL,
b REAL,
c REAL,
enabled BOOLEAN NOT NULL,
FOREIGN KEY(dif_default) REFERENCES dif_adists(id),
FOREIGN KEY(reach) REFERENCES river_reach(id)
)
""")
execute(f"""
CREATE TABLE dif_adists_spec{ext}(
{cls.create_db_add_pamhyr_id()},
dif_default INTEGER NOT NULL,
method TEXT NOT NULL,
reach INTEGER NOT NULL,
start_rk REAL NOT NULL,
end_rk REAL NOT NULL,
dif REAL NOT NULL,
b REAL,
c REAL,
enabled BOOLEAN NOT NULL,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(dif_default) REFERENCES dif_adists(id),
FOREIGN KEY(reach) REFERENCES river_reach(id)
)
""")
return cls._create_submodel(execute)
@@ -81,8 +83,34 @@ class DIFAdisTSSpec(SQLSubModel):
if int(release) < 6:
cls._db_create(execute)
if major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "dif_spec"
table_new = "dif_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")
execute(
f"INSERT INTO {table_new}_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_new}_tmp RENAME TO {table_new}")
cls._db_update_to_0_2_0_set_reach_pid(execute, table, reachs)
@classmethod
def _db_load(cls, execute, data=None):
new = []