Skip to content

Commit

Permalink
Delete eleveation stats dependent rows
Browse files Browse the repository at this point in the history
  • Loading branch information
conbrad committed Nov 8, 2023
1 parent a7476f8 commit edfc18d
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions api/alembic/versions/8e85e2b291a9_remove_zone_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import sqlalchemy as sa
import geoalchemy2
from sqlalchemy.orm.session import Session
from sqlalchemy import select

# revision identifiers, used by Alembic.
revision = '8e85e2b291a9'
Expand All @@ -34,6 +33,16 @@
sa.Column('run_parameters', sa.Integer),
sa.Column('area', sa.Float))

advisory_elevation_stats = sa.Table('advisory_elevation_stats', sa.MetaData(),
sa.Column('id', sa.Integer),
sa.Column('advisory_shape_id', sa.Integer),
sa.Column('threshold', sa.Integer),
sa.Column('run_parameters', sa.Integer),
sa.Column('minimum', sa.Float),
sa.Column('quartile_25', sa.Float),
sa.Column('median', sa.Float),
sa.Column('quartile_75', sa.Float),
sa.Column('maximum', sa.Float))

def upgrade():
# ### commands auto generated by Alembic ###
Expand All @@ -43,11 +52,12 @@ def upgrade():
result = session.execute(statement).fetchone()
fire_zone_id = result.id

# delete dependent records from high_hfi_area table first
zone_type_subquery = select([shape_type_table.c.id]).where(shape_type_table.c.name == 'fire_zone')
zone_subquery = select([shape_table.c.id]).where(shape_table.c.shape_type.in_(zone_type_subquery))
session.execute(high_hfi_table.delete().where(high_hfi_table.c.advisory_shape_id.in_(zone_subquery)))
zone_id_statement = shape_table.select().where(shape_table.c.shape_type == fire_zone_id)
zone_shape_result = session.execute(zone_id_statement).all()
zone_shape_ids = [row.id for row in zone_shape_result]

session.execute(high_hfi_table.delete().where(high_hfi_table.c.advisory_shape_id.in_(zone_shape_ids)))
session.execute(advisory_elevation_stats.delete().where(advisory_elevation_stats.c.advisory_shape_id.in_(zone_shape_ids)))
session.execute(shape_table.delete().where(shape_table.c.shape_type == fire_zone_id))
# ### end Alembic commands ###

Expand Down

0 comments on commit edfc18d

Please sign in to comment.