Add POI and POIDistance data models with migration
Introduces PointOfInterest (per-user named locations with lat/lng) and POIDistance (travel time/distance per listing+POI+mode triple) SQLModel entities, plus an Alembic migration to create both tables with indexes and a composite unique constraint.
This commit is contained in:
parent
9ff381106b
commit
5783d8fae9
3 changed files with 106 additions and 0 deletions
70
alembic/versions/c5d6e7f8a9b0_add_poi_tables.py
Normal file
70
alembic/versions/c5d6e7f8a9b0_add_poi_tables.py
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
"""add poi tables
|
||||
|
||||
Revision ID: c5d6e7f8a9b0
|
||||
Revises: b4c7d8e9f0a1
|
||||
Create Date: 2026-02-08 12:00:00.000000
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
import sqlmodel
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'c5d6e7f8a9b0'
|
||||
down_revision: Union[str, None] = 'b4c7d8e9f0a1'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Create pointofinterest and poidistance tables."""
|
||||
op.create_table('pointofinterest',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('address', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('latitude', sa.Float(), nullable=False),
|
||||
sa.Column('longitude', sa.Float(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True,
|
||||
server_default=sa.func.now()),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['user.id']),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
)
|
||||
op.create_index(op.f('ix_pointofinterest_user_id'),
|
||||
'pointofinterest', ['user_id'], unique=False)
|
||||
|
||||
op.create_table('poidistance',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('listing_id', sa.Integer(), nullable=False),
|
||||
sa.Column('listing_type', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('poi_id', sa.Integer(), nullable=False),
|
||||
sa.Column('travel_mode', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('duration_seconds', sa.Integer(), nullable=False),
|
||||
sa.Column('distance_meters', sa.Integer(), nullable=False),
|
||||
sa.Column('computed_at', sa.DateTime(), nullable=True,
|
||||
server_default=sa.func.now()),
|
||||
sa.ForeignKeyConstraint(['poi_id'], ['pointofinterest.id']),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('listing_id', 'listing_type', 'poi_id', 'travel_mode',
|
||||
name='uq_poi_distance_listing_poi_mode'),
|
||||
)
|
||||
op.create_index(op.f('ix_poidistance_listing_id'),
|
||||
'poidistance', ['listing_id'], unique=False)
|
||||
op.create_index(op.f('ix_poidistance_listing_type'),
|
||||
'poidistance', ['listing_type'], unique=False)
|
||||
op.create_index(op.f('ix_poidistance_poi_id'),
|
||||
'poidistance', ['poi_id'], unique=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Drop poidistance and pointofinterest tables."""
|
||||
op.drop_index(op.f('ix_poidistance_poi_id'), table_name='poidistance')
|
||||
op.drop_index(op.f('ix_poidistance_listing_type'), table_name='poidistance')
|
||||
op.drop_index(op.f('ix_poidistance_listing_id'), table_name='poidistance')
|
||||
op.drop_table('poidistance')
|
||||
|
||||
op.drop_index(op.f('ix_pointofinterest_user_id'), table_name='pointofinterest')
|
||||
op.drop_table('pointofinterest')
|
||||
13
models/poi.py
Normal file
13
models/poi.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from datetime import datetime
|
||||
|
||||
from sqlmodel import SQLModel, Field
|
||||
|
||||
|
||||
class PointOfInterest(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
user_id: int = Field(foreign_key="user.id", index=True)
|
||||
name: str = Field(nullable=False)
|
||||
address: str = Field(nullable=False)
|
||||
latitude: float = Field(nullable=False)
|
||||
longitude: float = Field(nullable=False)
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
23
models/poi_distance.py
Normal file
23
models/poi_distance.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
from datetime import datetime
|
||||
|
||||
from sqlmodel import SQLModel, Field, UniqueConstraint
|
||||
|
||||
from models.listing import ListingType
|
||||
|
||||
|
||||
class POIDistance(SQLModel, table=True):
|
||||
__table_args__ = (
|
||||
UniqueConstraint(
|
||||
"listing_id", "listing_type", "poi_id", "travel_mode",
|
||||
name="uq_poi_distance_listing_poi_mode",
|
||||
),
|
||||
)
|
||||
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
listing_id: int = Field(index=True, nullable=False)
|
||||
listing_type: ListingType = Field(index=True, nullable=False)
|
||||
poi_id: int = Field(foreign_key="pointofinterest.id", index=True, nullable=False)
|
||||
travel_mode: str = Field(nullable=False) # WALK, BICYCLE, TRANSIT
|
||||
duration_seconds: int = Field(nullable=False)
|
||||
distance_meters: int = Field(nullable=False)
|
||||
computed_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
Loading…
Add table
Add a link
Reference in a new issue