Skip to content
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
161 changes: 161 additions & 0 deletions alembic/versions/2025_09_15_1137-d5f92e6fedf4_add_location_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
"""Add Location tables

Revision ID: d5f92e6fedf4
Revises: e7189dc92a83
Create Date: 2025-09-15 11:37:58.183674

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

Check warning on line 11 in alembic/versions/2025_09_15_1137-d5f92e6fedf4_add_location_tables.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] alembic/versions/2025_09_15_1137-d5f92e6fedf4_add_location_tables.py#L11 <401>

'sqlalchemy as sa' imported but unused
Raw output
./alembic/versions/2025_09_15_1137-d5f92e6fedf4_add_location_tables.py:11:1: F401 'sqlalchemy as sa' imported but unused


# revision identifiers, used by Alembic.
revision: str = 'd5f92e6fedf4'
down_revision: Union[str, None] = 'e7189dc92a83'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

US_STATES_TABLE_NAME = 'us_states'
COUNTIES_TABLE_NAME = 'counties'
LOCALITIES_TABLE_NAME = 'localities'
LOCATIONS_TABLE_NAME = 'locations'
LINK_AGENCIES_LOCATIONS_TABLE_NAME = 'link_agencies_locations'

def upgrade() -> None:

Check warning on line 26 in alembic/versions/2025_09_15_1137-d5f92e6fedf4_add_location_tables.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] alembic/versions/2025_09_15_1137-d5f92e6fedf4_add_location_tables.py#L26 <103>

Missing docstring in public function
Raw output
./alembic/versions/2025_09_15_1137-d5f92e6fedf4_add_location_tables.py:26:1: D103 Missing docstring in public function
_create_location_type()
_create_us_states_table()
_create_counties_table()
_create_localities_table()
_create_locations_table()
_create_link_agencies_locations_table()

def downgrade() -> None:

Check warning on line 34 in alembic/versions/2025_09_15_1137-d5f92e6fedf4_add_location_tables.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] alembic/versions/2025_09_15_1137-d5f92e6fedf4_add_location_tables.py#L34 <103>

Missing docstring in public function
Raw output
./alembic/versions/2025_09_15_1137-d5f92e6fedf4_add_location_tables.py:34:1: D103 Missing docstring in public function
_remove_link_agencies_locations_table()
_remove_locations_table()
_remove_localities_table()
_remove_counties_table()
_remove_us_states_table()
_remove_location_type()

def _create_location_type():
op.execute("""
create type location_type as enum ('National', 'State', 'County', 'Locality')
""")

def _remove_location_type():
op.execute("""
drop type location_type
""")

def _create_us_states_table():
op.execute("""
create table if not exists public.us_states
(
state_iso text not null
constraint unique_state_iso
unique,
state_name text,
id bigint generated always as identity
primary key
)
""")

def _create_counties_table():
op.execute("""
create table if not exists public.counties
(
fips varchar not null
constraint unique_fips
unique,
name text,
lat double precision,
lng double precision,
population bigint,
agencies text,
id bigint generated always as identity
primary key,
state_id integer
references public.us_states,
unique (fips, state_id),
constraint unique_county_name_and_state
unique (name, state_id)
)
""")

def _create_localities_table():
op.execute("""
create table if not exists public.localities
(
id bigint generated always as identity
primary key,
name varchar(255) not null
constraint localities_name_check
check ((name)::text !~~ '%,%'::text),
county_id integer not null
references public.counties,
unique (name, county_id)
)

""")

def _create_locations_table():
op.execute("""
create table if not exists public.locations
(
id bigint generated always as identity
primary key,
type location_type not null,
state_id bigint
references public.us_states
on delete cascade,
county_id bigint
references public.counties
on delete cascade,
locality_id bigint
references public.localities
on delete cascade,
lat double precision,
lng double precision,
unique (id, type, state_id, county_id, locality_id),
constraint locations_check
check (((type = 'National'::location_type) AND (state_id IS NULL) AND (county_id IS NULL) AND
(locality_id IS NULL)) OR
((type = 'State'::location_type) AND (county_id IS NULL) AND (locality_id IS NULL)) OR
((type = 'County'::location_type) AND (county_id IS NOT NULL) AND (locality_id IS NULL)) OR
((type = 'Locality'::location_type) AND (county_id IS NOT NULL) AND (locality_id IS NOT NULL)))
)
""")

def _create_link_agencies_locations_table():
op.execute("""
create table if not exists public.link_agencies_locations
(
id serial
primary key,
agency_id integer not null
references public.agencies
on delete cascade,
location_id integer not null
references public.locations
on delete cascade,
constraint unique_agency_location
unique (agency_id, location_id)
)
""")

def _remove_link_agencies_locations_table():
op.drop_table(LINK_AGENCIES_LOCATIONS_TABLE_NAME)

def _remove_locations_table():
op.drop_table(LOCATIONS_TABLE_NAME)

def _remove_localities_table():
op.drop_table(LOCALITIES_TABLE_NAME)

def _remove_counties_table():
op.drop_table(COUNTIES_TABLE_NAME)

def _remove_us_states_table():
op.drop_table(US_STATES_TABLE_NAME)
23 changes: 22 additions & 1 deletion src/db/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,25 @@
CURRENT_TIME_SERVER_DEFAULT = func.now()

def url_id_primary_key_constraint() -> PrimaryKeyConstraint:
return PrimaryKeyConstraint('url_id')
return PrimaryKeyConstraint('url_id')

def county_column(nullable: bool = False) -> Column[int]:

Check warning on line 45 in src/db/models/helpers.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/helpers.py#L45 <103>

Missing docstring in public function
Raw output
./src/db/models/helpers.py:45:1: D103 Missing docstring in public function
return Column(
Integer(),
ForeignKey('counties.id', ondelete='CASCADE'),
nullable=nullable
)

def locality_column(nullable: bool = False) -> Column[int]:

Check warning on line 52 in src/db/models/helpers.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/helpers.py#L52 <103>

Missing docstring in public function
Raw output
./src/db/models/helpers.py:52:1: D103 Missing docstring in public function
return Column(
Integer(),
ForeignKey('localities.id', ondelete='CASCADE'),
nullable=nullable
)

def us_state_column(nullable: bool = False) -> Column[int]:

Check warning on line 59 in src/db/models/helpers.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/helpers.py#L59 <103>

Missing docstring in public function
Raw output
./src/db/models/helpers.py:59:1: D103 Missing docstring in public function
return Column(
Integer(),
ForeignKey('us_states.id', ondelete='CASCADE'),
nullable=nullable
)

Check warning on line 64 in src/db/models/helpers.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/helpers.py#L64 <292>

no newline at end of file
Raw output
./src/db/models/helpers.py:64:6: W292 no newline at end of file
Empty file.
10 changes: 10 additions & 0 deletions src/db/models/impl/link/agency_location/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from src.db.models.mixins import AgencyDependentMixin, LocationDependentMixin

Check warning on line 1 in src/db/models/impl/link/agency_location/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/link/agency_location/sqlalchemy.py#L1 <100>

Missing docstring in public module
Raw output
./src/db/models/impl/link/agency_location/sqlalchemy.py:1:1: D100 Missing docstring in public module
from src.db.models.templates_.with_id import WithIDBase


class LinkAgencyLocation(

Check warning on line 5 in src/db/models/impl/link/agency_location/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/link/agency_location/sqlalchemy.py#L5 <101>

Missing docstring in public class
Raw output
./src/db/models/impl/link/agency_location/sqlalchemy.py:5:1: D101 Missing docstring in public class
WithIDBase,
AgencyDependentMixin,
LocationDependentMixin,
):
__tablename__ = "link_agencies_locations"

Check warning on line 10 in src/db/models/impl/link/agency_location/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/link/agency_location/sqlalchemy.py#L10 <292>

no newline at end of file
Raw output
./src/db/models/impl/link/agency_location/sqlalchemy.py:10:46: W292 no newline at end of file
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions src/db/models/impl/location/county/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from sqlalchemy import String, Column, Float, Integer

Check warning on line 1 in src/db/models/impl/location/county/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/county/sqlalchemy.py#L1 <100>

Missing docstring in public module
Raw output
./src/db/models/impl/location/county/sqlalchemy.py:1:1: D100 Missing docstring in public module
from sqlalchemy.orm import Mapped

from src.db.models.helpers import us_state_column
from src.db.models.templates_.with_id import WithIDBase


class County(

Check warning on line 8 in src/db/models/impl/location/county/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/county/sqlalchemy.py#L8 <101>

Missing docstring in public class
Raw output
./src/db/models/impl/location/county/sqlalchemy.py:8:1: D101 Missing docstring in public class
WithIDBase,
):
__tablename__ = "counties"

name: Mapped[str]
state_id = us_state_column()
fips: Mapped[str] = Column(String(5), nullable=True)
lat: Mapped[float] = Column(Float, nullable=True)
lng: Mapped[float] = Column(Float, nullable=True)
population: Mapped[int] = Column(Integer, nullable=True)

Check warning on line 18 in src/db/models/impl/location/county/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/county/sqlalchemy.py#L18 <292>

no newline at end of file
Raw output
./src/db/models/impl/location/county/sqlalchemy.py:18:61: W292 no newline at end of file
Empty file.
14 changes: 14 additions & 0 deletions src/db/models/impl/location/locality/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from sqlalchemy import String, Column

Check warning on line 1 in src/db/models/impl/location/locality/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/locality/sqlalchemy.py#L1 <100>

Missing docstring in public module
Raw output
./src/db/models/impl/location/locality/sqlalchemy.py:1:1: D100 Missing docstring in public module

from src.db.models.helpers import county_column
from src.db.models.templates_.with_id import WithIDBase


class Locality(

Check warning on line 7 in src/db/models/impl/location/locality/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/locality/sqlalchemy.py#L7 <101>

Missing docstring in public class
Raw output
./src/db/models/impl/location/locality/sqlalchemy.py:7:1: D101 Missing docstring in public class
WithIDBase,
):

__tablename__ = "localities"

name = Column(String(255), nullable=False)
county_id = county_column(nullable=False)
Empty file.
8 changes: 8 additions & 0 deletions src/db/models/impl/location/location/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from enum import Enum

Check warning on line 1 in src/db/models/impl/location/location/enums.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/location/enums.py#L1 <100>

Missing docstring in public module
Raw output
./src/db/models/impl/location/location/enums.py:1:1: D100 Missing docstring in public module


class LocationType(Enum):

Check warning on line 4 in src/db/models/impl/location/location/enums.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/location/enums.py#L4 <101>

Missing docstring in public class
Raw output
./src/db/models/impl/location/location/enums.py:4:1: D101 Missing docstring in public class
NATIONAL = "National"
STATE = "State"
COUNTY = "County"
LOCALITY = "Locality"

Check warning on line 8 in src/db/models/impl/location/location/enums.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/location/enums.py#L8 <292>

no newline at end of file
Raw output
./src/db/models/impl/location/location/enums.py:8:26: W292 no newline at end of file
19 changes: 19 additions & 0 deletions src/db/models/impl/location/location/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from sqlalchemy import Float, Column

Check warning on line 1 in src/db/models/impl/location/location/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/location/sqlalchemy.py#L1 <100>

Missing docstring in public module
Raw output
./src/db/models/impl/location/location/sqlalchemy.py:1:1: D100 Missing docstring in public module

from src.db.models.helpers import us_state_column, county_column, locality_column, enum_column
from src.db.models.impl.location.location.enums import LocationType
from src.db.models.templates_.with_id import WithIDBase


class Location(

Check warning on line 8 in src/db/models/impl/location/location/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/location/sqlalchemy.py#L8 <101>

Missing docstring in public class
Raw output
./src/db/models/impl/location/location/sqlalchemy.py:8:1: D101 Missing docstring in public class
WithIDBase
):

__tablename__ = "locations"

state_id = us_state_column(nullable=True)
county_id = county_column(nullable=True)
locality_id = locality_column(nullable=True)
type = enum_column(LocationType, name="location_type", nullable=False)
lat = Column(Float(), nullable=True)
lng = Column(Float(), nullable=True)
Empty file.
12 changes: 12 additions & 0 deletions src/db/models/impl/location/us_state/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from sqlalchemy.orm import Mapped

Check warning on line 1 in src/db/models/impl/location/us_state/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/us_state/sqlalchemy.py#L1 <100>

Missing docstring in public module
Raw output
./src/db/models/impl/location/us_state/sqlalchemy.py:1:1: D100 Missing docstring in public module

from src.db.models.templates_.with_id import WithIDBase


class USState(

Check warning on line 6 in src/db/models/impl/location/us_state/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/us_state/sqlalchemy.py#L6 <101>

Missing docstring in public class
Raw output
./src/db/models/impl/location/us_state/sqlalchemy.py:6:1: D101 Missing docstring in public class
WithIDBase,
):
__tablename__ = "us_states"

state_name: Mapped[str]
state_iso: Mapped[str]

Check warning on line 12 in src/db/models/impl/location/us_state/sqlalchemy.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/impl/location/us_state/sqlalchemy.py#L12 <292>

no newline at end of file
Raw output
./src/db/models/impl/location/us_state/sqlalchemy.py:12:27: W292 no newline at end of file
9 changes: 9 additions & 0 deletions src/db/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
nullable=False
)

class LocationDependentMixin:

Check warning on line 41 in src/db/models/mixins.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] src/db/models/mixins.py#L41 <101>

Missing docstring in public class
Raw output
./src/db/models/mixins.py:41:1: D101 Missing docstring in public class
location_id = Column(
Integer,
ForeignKey(
'locations.id',
ondelete="CASCADE",
),
nullable=False
)

class AgencyDependentMixin:
agency_id = Column(
Expand Down