Skip to content

Commit

Permalink
Create Price model with migration
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Nov 15, 2023
1 parent a2e3b51 commit 5169034
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
49 changes: 49 additions & 0 deletions alembic/versions/43571a31006d_create_price_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Create Price table
Revision ID: 43571a31006d
Revises: 01907c7134ee
Create Date: 2023-11-12 18:27:49.656734
"""
from typing import Sequence
from typing import Union

import sqlalchemy as sa

from alembic import op


# revision identifiers, used by Alembic.
revision: str = '43571a31006d'
down_revision: Union[str, None] = '01907c7134ee'
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.create_table(
'prices',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('product_code', sa.String(), nullable=True),
sa.Column('price', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('currency', sa.String(length=3), nullable=True),
sa.Column('location_osm_id', sa.BigInteger(), nullable=True),
sa.Column('location_osm_type', sa.String(length=255), nullable=True),
sa.Column('date', sa.Date(), nullable=True),
sa.Column('created', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.PrimaryKeyConstraint('id'),
)
op.create_index(op.f('ix_prices_id'), 'prices', ['id'], unique=False)
op.create_index(op.f('ix_prices_location_osm_id'), 'prices', ['location_osm_id'], unique=False)
op.create_index(op.f('ix_prices_product_code'), 'prices', ['product_code'], unique=False)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_prices_product_code'), table_name='prices')
op.drop_index(op.f('ix_prices_location_osm_id'), table_name='prices')
op.drop_index(op.f('ix_prices_id'), table_name='prices')
op.drop_table('prices')
# ### end Alembic commands ###
7 changes: 7 additions & 0 deletions app/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from enum import Enum


class PriceLocationOSMType(Enum):
NODE = "NODE"
WAY = "WAY"
RELATION = "RELATION"
29 changes: 29 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
from sqlalchemy import BigInteger
from sqlalchemy import Column
from sqlalchemy import Date
from sqlalchemy import DateTime
from sqlalchemy import Integer
from sqlalchemy import Numeric
from sqlalchemy import String
from sqlalchemy.sql import func
from sqlalchemy_utils import force_auto_coercion
from sqlalchemy_utils.types.choice import ChoiceType
from sqlalchemy_utils.types.currency import CurrencyType

from app.db import Base
from app.enums import PriceLocationOSMType

force_auto_coercion()


class User(Base):
user_id = Column(String, primary_key=True, index=True)
token = Column(String, unique=True, index=True)

last_used = Column(DateTime(timezone=True))

created = Column(DateTime(timezone=True), server_default=func.now())

__tablename__ = "users"


class Price(Base):
id = Column(Integer, primary_key=True, index=True)
product_code = Column(String, index=True)

price = Column(Numeric(precision=10, scale=2))
currency = Column(CurrencyType)

location_osm_id = Column(BigInteger, index=True)
location_osm_type = Column(ChoiceType(PriceLocationOSMType))

date = Column(Date)

created = Column(DateTime(timezone=True), server_default=func.now())

__tablename__ = "prices"

0 comments on commit 5169034

Please sign in to comment.