From da948e0a92defc7d3980bd3062920da637c7a413 Mon Sep 17 00:00:00 2001 From: Margriet Palm Date: Wed, 8 Jan 2025 14:43:49 +0100 Subject: [PATCH] Fix problem with culvert geom migration in 228 --- CHANGES.rst | 2 +- .../migrations/versions/0228_upgrade_db_1D.py | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 1cd719f..a787fe2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,7 +6,7 @@ Changelog of threedi-schema 0.229.1 (unreleased) -------------------- -- Nothing changed yet. +- Fix issue where invalid geometries broke migration 228 for culverts 0.229.0 (2025-01-08) diff --git a/threedi_schema/migrations/versions/0228_upgrade_db_1D.py b/threedi_schema/migrations/versions/0228_upgrade_db_1D.py index 04e71ff..1d93024 100644 --- a/threedi_schema/migrations/versions/0228_upgrade_db_1D.py +++ b/threedi_schema/migrations/versions/0228_upgrade_db_1D.py @@ -351,6 +351,26 @@ def set_geom_for_object(table_name: str, col_name: str = 'the_geom'): 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""" @@ -529,6 +549,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)