From 5a5948b65303232dfa66f9bc139468993a27d29e Mon Sep 17 00:00:00 2001 From: Paul Bienkowski Date: Wed, 16 Mar 2022 21:01:36 +0100 Subject: [PATCH] Add road_usage table --- .../a9627f63fbed_create_road_usage_table.py | 34 +++++++++++++++++++ api/obs/api/db.py | 21 +++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 api/migrations/versions/a9627f63fbed_create_road_usage_table.py diff --git a/api/migrations/versions/a9627f63fbed_create_road_usage_table.py b/api/migrations/versions/a9627f63fbed_create_road_usage_table.py new file mode 100644 index 0000000..cfbf302 --- /dev/null +++ b/api/migrations/versions/a9627f63fbed_create_road_usage_table.py @@ -0,0 +1,34 @@ +"""create road_usage table + +Revision ID: a9627f63fbed +Revises: +Create Date: 2022-03-16 20:26:17.449569 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy import Column, Integer, String, ForeignKey, Index, DateTime, Boolean +from sqlalchemy.types import BIGINT + +# revision identifiers, used by Alembic. +revision = "a9627f63fbed" +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + "road_usage", + Column("id", Integer, autoincrement=True, primary_key=True, index=True), + Column("track_id", Integer, ForeignKey("track.id", ondelete="CASCADE")), + Column("hex_hash", String, unique=True, index=True), + Column("way_id", BIGINT, index=True), + Column("time", DateTime), + Column("direction_reversed", Boolean), + Index("road_usage_segment", "way_id", "direction_reversed"), + ) + + +def downgrade(): + op.drop_table("road_usage") diff --git a/api/obs/api/db.py b/api/obs/api/db.py index 41c23cf..d4cc5c5 100644 --- a/api/obs/api/db.py +++ b/api/obs/api/db.py @@ -68,7 +68,9 @@ def random_string(length): async def connect_db(url, pool_size=10, max_overflow=20): global engine, sessionmaker - engine = create_async_engine(url, echo=False, pool_size=pool_size, max_overflow=max_overflow) + engine = create_async_engine( + url, echo=False, pool_size=pool_size, max_overflow=max_overflow + ) sessionmaker = SessionMaker(engine, class_=AsyncSession, expire_on_commit=False) yield engine @@ -143,11 +145,28 @@ class Road(Base): } +class RoadUsage(Base): + __tablename__ = "road_usage" + __table_args__ = (Index("road_segment", "way_id", "direction_reversed"),) + + id = Column(Integer, autoincrement=True, primary_key=True, index=True) + track_id = Column(Integer, ForeignKey("track.id", ondelete="CASCADE")) + hex_hash = Column(String, unique=True, index=True) + way_id = Column(BIGINT, index=True) + time = Column(DateTime) + direction_reversed = Column(Boolean) + + def __repr__(self): + return f"" + + NOW = text("NOW()") + class DuplicateTrackFileError(ValueError): pass + class Track(Base): __tablename__ = "track" id = Column(Integer, primary_key=True, autoincrement=True)