HS: Prepare update db 0.1.0.

This commit is contained in:
Pierre-Antoine Rouby committed 2024-07-29 15:47:17 +02:00
1 parent 1bae755f1c
commit 9ec080a107
4 files changed
+150 -79

No files matched your search

+46 -18
View File
@@ -23,9 +23,10 @@ class BHSValue(SQLSubModel):
_sub_classes = []
_id_cnt = 0
def __init__(self, name: str = "", type=float, value=0.0,
def __init__(self, id: int = -1, name: str = "",
type=float, value=0.0,
status=None):
super(BHSValue, self).__init__()
super(BHSValue, self).__init__(id)
self._status = status
@@ -34,15 +35,18 @@ class BHSValue(SQLSubModel):
self._value = type(value)
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE hydraulic_structures_basic_value(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE hydraulic_structures_basic_value{ext} (
{cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL,
type TEXT NOT NULL,
value TEXT NOT NULL,
bhs INTEGER,
FOREIGN KEY(bhs) REFERENCES hydraulic_structures_basic(id)
FOREIGN KEY(bhs) REFERENCES hydraulic_structures_basic(id),
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
PRIMARY KEY(pamhyr_id, scenario)
)
""")
@@ -55,7 +59,28 @@ class BHSValue(SQLSubModel):
if int(release) < 6:
cls._db_create(execute)
return True
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 = "hydraulic_structures_basic_value"
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}_tmp " +
"(pamhyr_id, name, type, value, bhs, scenario) " +
"SELECT pamhyr_id, name, type, value, bhs, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod
def _str_to_type(cls, type):
@@ -89,20 +114,22 @@ class BHSValue(SQLSubModel):
bhs_id = data["bhs_id"]
table = execute(
"SELECT name, type, value " +
"SELECT pamhyr_id, name, type, value " +
"FROM hydraulic_structures_basic_value " +
f"WHERE bhs = '{bhs_id}'"
)
for row in table:
name = row[0]
type = cls._str_to_type(row[1])
value = row[2]
it = iter(row)
pid = next(it)
name = next(it)
type = cls._str_to_type(next(it))
value = next(it)
val = cls(
name=name,
type=type,
value=value,
id=pid, name=name,
type=type, value=value,
status=data['status']
)
@@ -113,17 +140,18 @@ class BHSValue(SQLSubModel):
def _db_save(self, execute, data=None):
bhs_id = data["bhs_id"]
sql = (
execute(
"INSERT INTO " +
"hydraulic_structures_basic_value(name, type, value, bhs) " +
"hydraulic_structures_basic_value" +
"(pamhyr_id, name, type, value, bhs) " +
"VALUES (" +
f"{self._pamhyr_id}, " +
f"'{self._db_format(self._name)}', " +
f"'{self._db_format(self._type_to_str(self._type))}', " +
f"'{self._db_format(self._value)}', " +
f"{bhs_id}" +
")"
)
execute(sql)
return True