Skip to content

Commit

Permalink
Fix problem with culvert geom migration in 228
Browse files Browse the repository at this point in the history
  • Loading branch information
margrietpalm committed Jan 8, 2025
1 parent c645544 commit da948e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions threedi_schema/migrations/versions/0228_upgrade_db_1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit da948e0

Please sign in to comment.