Skip to content

Commit

Permalink
Merge branch 'hotfix_228'
Browse files Browse the repository at this point in the history
  • Loading branch information
margrietpalm committed Jan 10, 2025
2 parents c645544 + 75524db commit 93c706e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
8 changes: 7 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ Changelog of threedi-schema
===================================================



0.229.1 (unreleased)
--------------------

Expand All @@ -22,6 +21,13 @@ Changelog of threedi-schema
- Remove v2 related views from sqlite


0.228.4 (2025-01-10)
--------------------

- Fix incorrectly setting of geometry for pipe, weir and orifice in migration
- Fix issue where invalid geometries broke migration 228 for culverts


0.228.3 (2024-12-10)
--------------------

Expand Down
43 changes: 35 additions & 8 deletions threedi_schema/migrations/versions/0228_upgrade_db_1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,21 +336,46 @@ def migrate_cross_section_definition_to_object(table_name: str):


def set_geom_for_object(table_name: str, col_name: str = 'the_geom'):
# line from connection_node_start_id to connection_node_end_id
# SELECT load_extension('mod_spatialite');
op.execute(sa.text(f"SELECT AddGeometryColumn('{table_name}', '{col_name}', 4326, 'LINESTRING', 'XY', 0);"))
q = f"""
UPDATE
{table_name}
SET the_geom = (
SELECT MakeLine(start_node.the_geom, end_node.the_geom) FROM {table_name} AS object
JOIN v2_connection_nodes AS start_node ON object.connection_node_start_id = start_node.id
JOIN v2_connection_nodes AS end_node ON object.connection_node_end_id = end_node.id
)
{table_name} AS object
SET
{col_name} = (
SELECT
MakeLine(start_node.the_geom, end_node.the_geom)
FROM
v2_connection_nodes AS start_node,
v2_connection_nodes AS end_node
WHERE
object.connection_node_start_id = start_node.id
AND
object.connection_node_end_id = end_node.id
)
"""
op.execute(sa.text(q))


def fix_geom_for_culvert():
new_table_name = f'_temp_228_fix_culvert_{uuid.uuid4().hex}'
old_table_name = 'v2_culvert'
connection = op.get_bind()
columns = connection.execute(sa.text(f"PRAGMA table_info('{old_table_name}')")).fetchall()
# get all column names and types
col_names = [col[1] for col in columns if col[1] not in ['id', 'the_geom']]
col_types = [col[2] for col in columns if col[1] in col_names]
# Create new table (temp), insert data, drop original and rename temp to table_name
new_col_str = ','.join(['id INTEGER PRIMARY KEY NOT NULL'] + [f'{cname} {ctype}' for cname, ctype in
zip(col_names, col_types)])
op.execute(sa.text(f"CREATE TABLE {new_table_name} ({new_col_str});"))
op.execute(sa.text(f"SELECT AddGeometryColumn('{new_table_name}', 'the_geom', 4326, 'LINESTRING', 'XY', 0);"))
# Copy data
op.execute(sa.text(f"INSERT INTO {new_table_name} (id, {','.join(col_names)}, the_geom) "
f"SELECT id, {','.join(col_names)}, the_geom FROM {old_table_name}"))
op.execute(sa.text("DROP TABLE v2_culvert"))
op.execute(sa.text(f"ALTER TABLE {new_table_name} RENAME TO v2_culvert"))


def set_geom_for_v2_pumpstation():
op.execute(sa.text(f"SELECT AddGeometryColumn('v2_pumpstation', 'the_geom', 4326, 'POINT', 'XY', 0);"))
q = f"""
Expand Down Expand Up @@ -529,6 +554,8 @@ def upgrade():
# Set geometry for tables without one
if table_name != 'v2_culvert':
set_geom_for_object(table_name)
else:
fix_geom_for_culvert()
set_geom_for_v2_pumpstation()
for old_table_name, new_table_name in RENAME_TABLES:
modify_table(old_table_name, new_table_name)
Expand Down

0 comments on commit 93c706e

Please sign in to comment.