IC: Add prepare db 0.1.0.

scenarios
Pierre-Antoine Rouby 2024-07-24 10:59:56 +02:00
parent aea308a5a2
commit 9f4b297d23
2 changed files with 53 additions and 24 deletions

View File

@ -23,17 +23,18 @@ from tools import trace, timer
from functools import reduce
from Model.Tools.PamhyrDB import SQLSubModel
from Model.Scenario import Scenario
logger = logging.getLogger()
class Data(SQLSubModel):
def __init__(self, name: str = "",
def __init__(self, id: int = -1, name: str = "",
comment: str = "", reach=None,
rk: float = 0.0, discharge: float = 0.0,
height: float = 0.0,
status=None):
super(Data, self).__init__()
super(Data, self).__init__(id)
self._status = status
@ -56,10 +57,10 @@ class Data(SQLSubModel):
self._update_from_discharge()
@classmethod
def _db_create(cls, execute):
execute("""
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE initial_conditions(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
{cls.create_db_add_pamhyr_id()},
ind INTEGER NOT NULL,
name TEXT NOT NULL,
comment TEXT NOT NULL,
@ -67,7 +68,10 @@ class Data(SQLSubModel):
rk REAL NOT NULL,
discharge REAL NOT NULL,
height REAL NOT NULL,
FOREIGN KEY(reach) REFERENCES river_reach(id)
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(reach) REFERENCES river_reach(pamhyr_id),
PRIMARY KEY(pamhyr_id, scenario)
)
""")
@ -82,31 +86,54 @@ class Data(SQLSubModel):
"ALTER TABLE initial_conditions RENAME COLUMN kp TO rk"
)
cls._db_update_to_0_1_0(execute, data)
return cls._update_submodel(execute, version, data)
@classmethod
def _db_update_to_0_1_0(cls, execute, data):
table = "initial_conditions"
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create(execute, ext="_tmp")
execute(
f"INSERT INTO {table} " +
"(pamhyr_id, name, comment, tab, reach, rk, scenario) " +
"SELECT pamhyr_id, name, comment, tab, reach, rk, 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):
id = data["reach"].id
id = data["reach"].pamhyr_id
table = execute(
"SELECT ind, name, comment, rk, discharge, height " +
"SELECT pamhyr_id, ind, name, comment, rk, discharge, height " +
"FROM initial_conditions " +
f"WHERE reach = {id}"
f"WHERE reach = {id} " +
"ORDER BY ind ASC"
)
new = []
for _ in table:
new.append(None)
for row in table:
ind = row[0]
name = row[1]
comment = row[2]
rk = row[3]
discharge = row[4]
height = row[5]
it = iter(row)
pid = next(it)
ind = next(it)
name = next(it)
comment = next(it)
rk = next(it)
discharge = next(it)
height = next(it)
d = cls(
id=pid,
reach=data["reach"],
status=data["status"],
name=name,
@ -116,7 +143,7 @@ class Data(SQLSubModel):
height=height,
)
new[ind] = d
new.append(d)
return new
@ -125,9 +152,10 @@ class Data(SQLSubModel):
execute(
"INSERT INTO " +
"initial_conditions(ind, name, comment, rk, " +
"initial_conditions(pamhyr_id, ind, name, comment, rk, " +
"discharge, height, reach) " +
"VALUES (" +
f"{self.pamhyr_id}, " +
f"{ind}, '{self._db_format(self.name)}', " +
f"'{self._db_format(self._comment)}', " +
f"{self._rk}, {self._discharge}, {self._height}, " +

View File

@ -175,7 +175,8 @@ class LateralContribution(SQLSubModel):
FOREIGN KEY(begin_section)
REFERENCES geometry_profileXYZ(pamhyr_id),
FOREIGN KEY(end_section)
REFERENCES geometry_profileXYZ(pamhyr_id)
REFERENCES geometry_profileXYZ(pamhyr_id),
PRIMARY KEY(pamhyr_id, scenario)
)
""")
@ -206,15 +207,15 @@ class LateralContribution(SQLSubModel):
"""
)
cls._db_update_to_0_1_0(execute)
cls._db_update_to_0_1_0(execute, data)
return cls._update_submodel(execute, version, data)
@classmethod
def _db_update_to_0_1_0(cls, execute):
def _db_update_to_0_1_0(cls, execute, data):
table = "lateral_contribution"
cls.update_db_add_pamhyr_id(execute, table)
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create(execute, ext="_tmp")