Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with culvert geom migration in 228 #166

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
--------------------

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 @@ -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"""
Expand Down Expand Up @@ -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)
Expand Down
Loading