From f54ac00e7e8bbfc32d368ddd434eb5b840b55f3d 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 | 6 +++++ .../migrations/versions/0228_upgrade_db_1D.py | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 10802ce..12c01a9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,12 @@ Changelog of threedi-schema +0.228.4 (unreleased) +-------------------- + +- Fix issue where invalid geometries broke migration 228 for culverts + + 0.228.3 (2024-12-10) -------------------- diff --git a/threedi_schema/migrations/versions/0228_upgrade_db_1D.py b/threedi_schema/migrations/versions/0228_upgrade_db_1D.py index d34b56f..bb3f18d 100644 --- a/threedi_schema/migrations/versions/0228_upgrade_db_1D.py +++ b/threedi_schema/migrations/versions/0228_upgrade_db_1D.py @@ -291,6 +291,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""" @@ -473,6 +493,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)