-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: on Price create, create (or get) Location, and link them (#36)
* On Price create, background task to create Location * Create relationship between Price & Location * Link new or existing location to new price * Prices: return location_id
- Loading branch information
Showing
6 changed files
with
119 additions
and
6 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
alembic/versions/20231116_1559_1c8431a64d3a_add_price_location_relationship.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Add Price.location relationship | ||
Revision ID: 1c8431a64d3a | ||
Revises: b3b951e016d0 | ||
Create Date: 2023-11-16 15:59:03.881443 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "1c8431a64d3a" | ||
down_revision: Union[str, None] = "b3b951e016d0" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column("prices", sa.Column("location_id", sa.Integer(), nullable=True)) | ||
op.create_foreign_key(None, "prices", "locations", ["location_id"], ["id"]) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint(None, "prices", type_="foreignkey") | ||
op.drop_column("prices", "location_id") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from sqlalchemy.orm import Session | ||
|
||
from app import crud | ||
from app.schemas import LocationCreate, PriceBase | ||
|
||
|
||
def create_price_location(db: Session, price: PriceBase): | ||
if price.location_osm_id and price.location_osm_type: | ||
# get or create the corresponding location | ||
location = LocationCreate( | ||
osm_id=price.location_osm_id, osm_type=price.location_osm_type | ||
) | ||
db_location = crud.get_or_create_location(db, location=location) | ||
# link the location to the price | ||
crud.set_price_location(db, price=price, location=db_location) |