refactor(db)!: finalize pooled WNDB v2 migration
This commit is contained in:
@@ -4,6 +4,7 @@ from ..core.database import (
|
||||
ChangeSet,
|
||||
DatabaseCommand,
|
||||
execute_command,
|
||||
execute_locked_command,
|
||||
g_add_prefix,
|
||||
g_delete_prefix,
|
||||
g_update_prefix,
|
||||
@@ -144,9 +145,12 @@ class Pipe(object):
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
return { 'type': self.type, 'id': self.id, 'node1': self.node1, 'node2': self.node2, 'length': self.length, 'diameter': self.diameter, 'roughness': self.roughness, 'minor_loss': self.minor_loss, 'status': self.status }
|
||||
|
||||
def _set_pipe(name: str, cs: ChangeSet) -> DatabaseCommand:
|
||||
raw_new = get_pipe(name, cs.operations[0]['id'])
|
||||
|
||||
def _set_pipe(
|
||||
name: str,
|
||||
cs: ChangeSet,
|
||||
current: dict[str, Any] | None = None,
|
||||
) -> DatabaseCommand:
|
||||
raw_new = current if current is not None else get_pipe(name, cs.operations[0]['id'])
|
||||
new_dict = cs.operations[0]
|
||||
schema = get_pipe_schema(name)
|
||||
for key, value in schema.items():
|
||||
@@ -154,19 +158,59 @@ def _set_pipe(name: str, cs: ChangeSet) -> DatabaseCommand:
|
||||
raw_new[key] = new_dict[key]
|
||||
new = Pipe(raw_new)
|
||||
|
||||
statement = f"update network.links set start_node_id = {new.f_node1}, end_node_id = {new.f_node2} where id = {new.f_id};"
|
||||
statement += f"\nupdate network.pipes set length = {new.f_length}, diameter = {new.f_diameter}, roughness = {new.f_roughness}, minor_loss = {new.f_minor_loss}, status = {new.f_status} where link_id = {new.f_id};"
|
||||
link_columns = {
|
||||
'node1': ('start_node_id', new.f_node1),
|
||||
'node2': ('end_node_id', new.f_node2),
|
||||
}
|
||||
pipe_columns = {
|
||||
'length': ('length', new.f_length),
|
||||
'diameter': ('diameter', new.f_diameter),
|
||||
'roughness': ('roughness', new.f_roughness),
|
||||
'minor_loss': ('minor_loss', new.f_minor_loss),
|
||||
'status': ('status', new.f_status),
|
||||
}
|
||||
statements = []
|
||||
link_assignments = [
|
||||
f"{column} = {value}"
|
||||
for field, (column, value) in link_columns.items()
|
||||
if field in new_dict
|
||||
]
|
||||
if link_assignments:
|
||||
statements.append(
|
||||
f"update network.links set {', '.join(link_assignments)} where id = {new.f_id};"
|
||||
)
|
||||
pipe_assignments = [
|
||||
f"{column} = {value}"
|
||||
for field, (column, value) in pipe_columns.items()
|
||||
if field in new_dict
|
||||
]
|
||||
if pipe_assignments:
|
||||
statements.append(
|
||||
f"update network.pipes set {', '.join(pipe_assignments)} where link_id = {new.f_id};"
|
||||
)
|
||||
statement = "\n".join(statements)
|
||||
change = g_update_prefix | new.as_dict()
|
||||
|
||||
return DatabaseCommand(statement, [change])
|
||||
|
||||
|
||||
def set_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
if 'id' not in cs.operations[0]:
|
||||
operation = cs.operations[0]
|
||||
if 'id' not in operation:
|
||||
return ChangeSet()
|
||||
if get_pipe(name, cs.operations[0]['id']) == {}:
|
||||
mutable_fields = {
|
||||
'node1', 'node2', 'length', 'diameter', 'roughness', 'minor_loss', 'status'
|
||||
}
|
||||
if not mutable_fields.intersection(operation):
|
||||
return ChangeSet()
|
||||
return execute_command(name, _set_pipe(name, cs))
|
||||
|
||||
def build_command() -> DatabaseCommand | None:
|
||||
current = get_pipe(name, operation['id'])
|
||||
if current == {}:
|
||||
return None
|
||||
return _set_pipe(name, cs, current)
|
||||
|
||||
return execute_locked_command(name, build_command)
|
||||
|
||||
|
||||
def _add_pipe(name: str, cs: ChangeSet) -> DatabaseCommand:
|
||||
|
||||
Reference in New Issue
Block a user