From 49740494ea61f057ab0df0bb722c5a536dc1a5c3 Mon Sep 17 00:00:00 2001 From: Max Chis Date: Fri, 26 Sep 2025 18:02:55 -0400 Subject: [PATCH] Add new_agency_suggestion table and update locations expanded view --- ...3e23d3f0_update_locations_expanded_view.py | 84 +++++++++++++++++++ src/api/endpoints/search/routes.py | 2 +- 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 alembic/versions/2025_09_26_1751-d4c63e23d3f0_update_locations_expanded_view.py diff --git a/alembic/versions/2025_09_26_1751-d4c63e23d3f0_update_locations_expanded_view.py b/alembic/versions/2025_09_26_1751-d4c63e23d3f0_update_locations_expanded_view.py new file mode 100644 index 00000000..675fd7b2 --- /dev/null +++ b/alembic/versions/2025_09_26_1751-d4c63e23d3f0_update_locations_expanded_view.py @@ -0,0 +1,84 @@ +"""Update locations expanded view + +Revision ID: d4c63e23d3f0 +Revises: b9317c6836e7 +Create Date: 2025-09-26 17:51:41.214287 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects.postgresql import ENUM + +from src.util.alembic_helpers import id_column, location_id_column, created_at_column + +# revision identifiers, used by Alembic. +revision: str = 'd4c63e23d3f0' +down_revision: Union[str, None] = 'b9317c6836e7' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def _update_locations_expanded_view(): + op.execute( + """ + CREATE OR REPLACE VIEW locations_expanded as + SELECT locations.id, + locations.type, + us_states.state_name, + us_states.state_iso, + counties.name AS county_name, + counties.fips AS county_fips, + localities.name AS locality_name, + localities.id AS locality_id, + us_states.id AS state_id, + counties.id AS county_id, + CASE + WHEN locations.type = 'Locality'::location_type THEN localities.name + WHEN locations.type = 'County'::location_type THEN counties.name::character varying + WHEN locations.type = 'State'::location_type THEN us_states.state_name::character varying + ELSE NULL::character varying + END AS display_name, + CASE + WHEN locations.type = 'Locality'::location_type THEN concat(localities.name, ', ', counties.name, + ', ', + us_states.state_name)::character varying + WHEN locations.type = 'County'::location_type + THEN concat(counties.name, ', ', us_states.state_name)::character varying + WHEN locations.type = 'State'::location_type THEN us_states.state_name::character varying + WHEN locations.type = 'National'::location_type THEN 'United States' + ELSE NULL::character varying + END AS full_display_name + FROM locations + LEFT JOIN us_states + ON locations.state_id = us_states.id + LEFT JOIN counties + ON locations.county_id = counties.id + LEFT JOIN localities + ON locations.locality_id = localities.id + """ + ) + + +def _create_new_agency_suggestion_table(): + op.create_table( + 'new_agency_suggestions', + id_column(), + location_id_column(), + sa.Column('name', sa.String()), + sa.Column('jurisdiction_type', ENUM(name='jurisdiction_type_enum', create_type=False), nullable=True), + sa.Column('agency_type', ENUM(name='agency_type_enum', create_type=False), nullable=True), + created_at_column() + ) + + +def upgrade() -> None: + _update_locations_expanded_view() + _create_new_agency_suggestion_table() + + + + +def downgrade() -> None: + pass diff --git a/src/api/endpoints/search/routes.py b/src/api/endpoints/search/routes.py index d8cd870d..f2abb93c 100644 --- a/src/api/endpoints/search/routes.py +++ b/src/api/endpoints/search/routes.py @@ -43,7 +43,7 @@ async def search_agency( access_info: AccessInfo = Depends(get_access_info), async_core: AsyncCore = Depends(get_async_core), ) -> list[AgencySearchResponse]: - if query is None and location_id is None: + if query is None and location_id is None and jurisdiction_type is None: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="At least one of query or location_id must be provided"