Pamhyr2: Some fixes for db update to 0.1.0.

scenarios
Pierre-Antoine Rouby 2024-07-31 10:29:04 +02:00
parent 5525fff1fe
commit d8fe1d8b81
12 changed files with 76 additions and 55 deletions

View File

@ -190,6 +190,7 @@ class BoundaryCondition(SQLSubModel):
@classmethod @classmethod
def _db_update_to_0_1_0(cls, execute, data): def _db_update_to_0_1_0(cls, execute, data):
table = "boundary_condition" table = "boundary_condition"
nodes = data['id2pid']['river_node']
cls.update_db_add_pamhyr_id(execute, table, data) cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table) Scenario.update_db_add_scenario(execute, table)

View File

@ -41,9 +41,6 @@ class BoundaryConditionList(PamhyrModelListWithTab):
def _db_load(cls, execute, data=None): def _db_load(cls, execute, data=None):
new = cls(status=data['status']) new = cls(status=data['status'])
if data is None:
data = {}
for tab in new._tabs: for tab in new._tabs:
data["tab"] = tab data["tab"] = tab
new._tabs[tab] = BoundaryCondition._db_load( new._tabs[tab] = BoundaryCondition._db_load(
@ -55,9 +52,6 @@ class BoundaryConditionList(PamhyrModelListWithTab):
def _db_save(self, execute, data=None): def _db_save(self, execute, data=None):
execute("DELETE FROM boundary_condition") execute("DELETE FROM boundary_condition")
if data is None:
data = {}
for tab in self._tabs: for tab in self._tabs:
data["tab"] = tab data["tab"] = tab
for bc in self._tabs[tab]: for bc in self._tabs[tab]:

View File

@ -27,8 +27,6 @@ logger = logging.getLogger()
class Friction(SQLSubModel): class Friction(SQLSubModel):
_id_cnt = 0
def __init__(self, id: int = -1, def __init__(self, id: int = -1,
status=None): status=None):
super(Friction, self).__init__(id) super(Friction, self).__init__(id)

View File

@ -122,7 +122,7 @@ class ProfileXYZ(Profile, SQLSubModel):
@classmethod @classmethod
def _db_update_to_0_1_0(cls, execute, data): def _db_update_to_0_1_0(cls, execute, data):
table = "stricklers" table = "geometry_profileXYZ"
cls.update_db_add_pamhyr_id(execute, table, data) cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table) Scenario.update_db_add_scenario(execute, table)

View File

@ -87,9 +87,6 @@ class Reach(SQLSubModel):
f"WHERE reach = {self.pamhyr_id}" f"WHERE reach = {self.pamhyr_id}"
) )
if data is None:
data = {}
ind = 0 ind = 0
for profile in profiles: for profile in profiles:
data["ind"] = ind data["ind"] = ind

View File

@ -101,8 +101,10 @@ class Data(SQLSubModel):
execute( execute(
f"INSERT INTO {table}_tmp " + f"INSERT INTO {table}_tmp " +
"(pamhyr_id, name, comment, tab, reach, rk, scenario) " + "(pamhyr_id, name, comment, reach, rk, " +
"SELECT pamhyr_id, name, comment, tab, reach, rk, scenario " + "discharge, height, scenario) " +
"SELECT pamhyr_id, name, comment, reach, rk, " +
"discharge, height, scenario " +
f"FROM {table}" f"FROM {table}"
) )

View File

@ -67,15 +67,15 @@ class Data(SQLSubModel):
major, minor, release = version.strip().split(".") major, minor, release = version.strip().split(".")
if major == minor == "0": if major == minor == "0":
cls._db_update_to_0_1_0(execute) cls._db_update_to_0_1_0(execute, data)
return cls._update_submodel(execute, version, data) return cls._update_submodel(execute, version, data)
@classmethod @classmethod
def _db_update_to_0_1_0(cls, execute): def _db_update_to_0_1_0(cls, execute, data=None):
table = "lateral_contribution_data" table = "lateral_contribution_data"
cls.update_db_add_pamhyr_id(execute, table) cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table) Scenario.update_db_add_scenario(execute, table)
cls._db_create(execute, ext="_tmp") cls._db_create(execute, ext="_tmp")

View File

@ -16,6 +16,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging
from tools import flatten, logger_exception from tools import flatten, logger_exception
from Model.Tools.PamhyrDB import SQLSubModel from Model.Tools.PamhyrDB import SQLSubModel
@ -46,6 +48,8 @@ from Model.REPLine.REPLineList import REPLineList
from Solver.Solvers import solver_type_list from Solver.Solvers import solver_type_list
logger = logging.getLogger()
class RiverNode(Node, SQLSubModel): class RiverNode(Node, SQLSubModel):
_sub_classes = [] _sub_classes = []
@ -66,7 +70,7 @@ class RiverNode(Node, SQLSubModel):
@classmethod @classmethod
def _db_create(cls, execute, ext=""): def _db_create(cls, execute, ext=""):
execute(f""" execute(f"""
CREATE TABLE river_node( CREATE TABLE river_node{ext}(
{cls.create_db_add_pamhyr_id()}, {cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL, name TEXT NOT NULL,
x REAL NOT NULL, x REAL NOT NULL,
@ -85,15 +89,15 @@ class RiverNode(Node, SQLSubModel):
major, minor, release = version.strip().split(".") major, minor, release = version.strip().split(".")
if major == minor == "0": if major == minor == "0":
cls._db_update_to_0_1_0(execute) cls._db_update_to_0_1_0(execute, data=data)
return cls._update_submodel(execute, version, data) return cls._update_submodel(execute, version, data)
@classmethod @classmethod
def _db_update_to_0_1_0(cls, execute): def _db_update_to_0_1_0(cls, execute, data=None):
table = "river_node" table = "river_node"
cls.update_db_add_pamhyr_id(execute, table) cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table) Scenario.update_db_add_scenario(execute, table)
cls._db_create(execute, ext="_tmp") cls._db_create(execute, ext="_tmp")
@ -164,9 +168,9 @@ class RiverReach(Edge, SQLSubModel):
self._frictions = FrictionList(status=self._status) self._frictions = FrictionList(status=self._status)
@classmethod @classmethod
def _db_create(cls, execute): def _db_create(cls, execute, ext=""):
execute(f""" execute(f"""
CREATE TABLE river_reach( CREATE TABLE river_reach{ext} (
{cls.create_db_add_pamhyr_id()}, {cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL, name TEXT NOT NULL,
enabled BOOLEAN NOT NULL, enabled BOOLEAN NOT NULL,
@ -187,16 +191,19 @@ class RiverReach(Edge, SQLSubModel):
def _db_update(cls, execute, version, data=None): def _db_update(cls, execute, version, data=None):
major, minor, release = version.strip().split(".") major, minor, release = version.strip().split(".")
logger.info(f"data = {data}")
if major == minor == "0": if major == minor == "0":
cls._db_update_to_0_1_0(execute) cls._db_update_to_0_1_0(execute, data)
return cls._update_submodel(execute, version, data) return cls._update_submodel(execute, version, data)
@classmethod @classmethod
def _db_update_to_0_1_0(cls, execute): def _db_update_to_0_1_0(cls, execute, data=None):
table = "river_reach" table = "river_reach"
nodes = data['id2pid']['river_node']
cls.update_db_add_pamhyr_id(execute, table) cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table) Scenario.update_db_add_scenario(execute, table)
cls._db_create(execute, ext="_tmp") cls._db_create(execute, ext="_tmp")
@ -212,13 +219,32 @@ class RiverReach(Edge, SQLSubModel):
execute(f"DROP TABLE {table}") execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}") execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
cls._db_update_to_0_1_0_set_node_pid(execute, table, nodes)
@classmethod
def _db_update_to_0_1_0_set_node_pid(cls, execute, table, nodes):
bcs = execute(
f"SELECT pamhyr_id, node1, node2 FROM {table}"
)
for row in bcs:
it = iter(row)
pid = next(it)
node1_id = next(it)
node2_id = next(it)
execute(
f"UPDATE {table} " +
f"SET node1 = {nodes[node1_id]}, " +
f"node2 = {nodes[node2_id]} " +
f"WHERE pamhyr_id = {pid}"
)
@classmethod @classmethod
def _db_load(cls, execute, data=None): def _db_load(cls, execute, data=None):
reachs = [] reachs = []
if data is None:
data = {}
table = execute( table = execute(
"SELECT pamhyr_id, name, enabled, node1, node2 FROM river_reach" "SELECT pamhyr_id, name, enabled, node1, node2 FROM river_reach"
) )
@ -230,15 +256,18 @@ class RiverReach(Edge, SQLSubModel):
pid = next(it) pid = next(it)
name = next(it) name = next(it)
enabled = (next(it) == 1) enabled = (next(it) == 1)
node1_pid = next(it)
node2_pid = next(it)
# Get nodes corresponding to db foreign key id # Get nodes corresponding to db foreign key id
node1 = next( node1 = next(
filter( filter(
lambda n: n.pamhyr_id == next(it), data["nodes"] lambda n: n.pamhyr_id == node1_pid, data["nodes"]
) )
) )
node2 = next( node2 = next(
filter( filter(
lambda n: n.pamhyr_id == next(it), data["nodes"] lambda n: n.pamhyr_id == node2_pid, data["nodes"]
) )
) )
@ -265,9 +294,6 @@ class RiverReach(Edge, SQLSubModel):
")" ")"
) )
if data is None:
data = {}
data["reach"] = self data["reach"] = self
objs = [self._reach, self._frictions] objs = [self._reach, self._frictions]

View File

@ -313,7 +313,7 @@ class SedimentLayer(SQLSubModel):
new = [] new = []
table = execute( table = execute(
"SELECT id, name, comment " + "SELECT pamhyr_id, name, comment " +
"FROM sedimentary_layer " "FROM sedimentary_layer "
) )

View File

@ -72,7 +72,6 @@ class SQLModel(SQL):
return True return True
def _create(self): def _create(self):
raise NotImplementedMethodeError(self, self._create) raise NotImplementedMethodeError(self, self._create)
def _update_submodel(self, version, data=None): def _update_submodel(self, version, data=None):
@ -82,12 +81,9 @@ class SQLModel(SQL):
commit=False commit=False
) )
if data is None:
data = {}
ok = True ok = True
for cls in self._sub_classes: for cls in self._sub_classes:
ok &= cls._db_update(fn, version, data) ok &= cls._db_update(fn, version, data=data)
self.commit() self.commit()
return ok return ok
@ -188,8 +184,12 @@ class SQLSubModel(PamhyrID):
@classmethod @classmethod
def _update_submodel(cls, execute, version, data=None): def _update_submodel(cls, execute, version, data=None):
ok = True
for sc in cls._sub_classes: for sc in cls._sub_classes:
sc._db_update(execute, version, data) ok &= sc._db_update(execute, version, data)
return ok
@classmethod @classmethod
def _db_update(cls, execute, version, data=None): def _db_update(cls, execute, version, data=None):

View File

@ -16,8 +16,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging
from tools import trace, timer from tools import trace, timer
logger = logging.getLogger()
class PamhyrID(object): class PamhyrID(object):
_pamhyr_id_cnt = 0 _pamhyr_id_cnt = 0
@ -47,16 +51,14 @@ class PamhyrID(object):
@classmethod @classmethod
def update_db_add_pamhyr_id(cls, execute, table, def update_db_add_pamhyr_id(cls, execute, table,
autoset=True, data=None):
data={}):
execute( execute(
f"ALTER TABLE {table} " + f"ALTER TABLE {table} " +
f"ADD COLUMN pamhyr_id INTEGER NOT NULL DEFAULT -1" f"ADD COLUMN pamhyr_id INTEGER NOT NULL DEFAULT -1"
) )
if autoset:
rows = execute(f"SELECT id FROM {table}")
id2pid = cls.update_db_add_pamhyr_id_init_id2pid(table, data) id2pid = cls.update_db_add_pamhyr_id_init_id2pid(table, data)
rows = execute(f"SELECT id FROM {table}")
for row in rows: for row in rows:
id = row[0] id = row[0]

View File

@ -118,6 +118,7 @@ class SQL(object):
except Exception as e: except Exception as e:
logger_exception(e) logger_exception(e)
finally: finally:
logger.debug(f" - {value}")
return value return value
def _create(self): def _create(self):