Skip to content

Commit

Permalink
none of this works
Browse files Browse the repository at this point in the history
  • Loading branch information
Erutis committed Aug 20, 2024
1 parent c51c2b4 commit a65b843
Show file tree
Hide file tree
Showing 11 changed files with 812 additions and 945 deletions.
2 changes: 1 addition & 1 deletion alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from alembic import context

from app.tables import Feed, FeedItem, Trajectory, metadata
from app.tables import FeedItem, Trajectory, metadata

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand Down

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions app/alembic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
alembic~=1.13.1
geoalchemy2~=0.14.4
psycopg2-binary~=2.9.9
sqlalchemy~=2.0.24
80 changes: 0 additions & 80 deletions app/app.py

This file was deleted.

54 changes: 1 addition & 53 deletions app/db_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,20 @@
# Created: 2/21/24

# Standard libraries
from datetime import datetime, timezone

import enum
import os
import time
import traceback


# External libraries
from geoalchemy2 import Geometry
from sqlalchemy import (
create_engine,
Column,
Integer,
schema,
text,
TIMESTAMP,
UUID,
)

from sqlalchemy.orm import declarative_base
from sqlalchemy.orm.state import InstanceState

# Internal libraries

Base = declarative_base()


class Trajectory(Base):
__tablename__ = "trajectory"
__table_args__ = {"schema": "gps"}
id = Column(Integer, primary_key=True)
create_time = Column(TIMESTAMP, default=datetime.now(timezone.utc))
updated_time = Column(TIMESTAMP, default=datetime.now(timezone.utc))
geom = Column(Geometry("GEOMETRYZM"))
feed_item_id = Column(UUID)

def to_dict(self):
d = {}

for field, value in self.__dict__.items():
if any(
(
isinstance(value, InstanceState),
isinstance(value, list),
)
):
continue
if isinstance(value, (int, float, bool, str, type(None))):
d[field] = value
elif isinstance(value, enum.Enum):
d[field] = value.name
else:
d[field] = str(value)

return d


def setup_pg():
"""Create GIS engine, connect, and create tables."""
Expand All @@ -76,16 +33,7 @@ def setup_pg():
conn.execute(schema.CreateSchema("gps"))
conn.commit()

# Create Trajectory table in gps schema
# with engine.connect() as conn:
# Feed.__table__.create(engine)
# FeedItem.__table__.create(engine)
# Trajectory.__table__.create(engine)
# Trajectory.feed = relationship("Feed", back_populates="trajectory")
# Trajectory.feed_item = relationship("FeedItem", back_populates="trajectory")

# conn.commit()
print("Committed!")
print("Schema created!")

return None

Expand Down
4 changes: 2 additions & 2 deletions app/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from sqlalchemy.orm import sessionmaker

# Internal libraries
from db_setup import Trajectory, engine_go_vroom
from db_setup import engine_go_vroom
from tables import Trajectory


# Pre-determined areas for random data generation
Expand All @@ -37,7 +38,6 @@ def main():
Trajectory(
geom=f"SRID=4326;{geom_type}({linestring})",
feed_item_id=uuid.uuid4(),
feed_id=uuid.uuid4(),
)
]

Expand Down
7 changes: 0 additions & 7 deletions app/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ def to_dict(self):
return d


class Feed(GISBase):
__tablename__ = "feed"
__table_args__ = {"schema": "gps"}
name = mapped_column(String)


class FeedItem(GISBase):
__tablename__ = "feed_item"
__table_args__ = {"schema": "gps"}
Expand All @@ -74,4 +68,3 @@ class Trajectory(GISBase):
__table_args__ = {"schema": "gps"}
geom = mapped_column(Geometry("GEOMETRYZM"))
feed_item_id = mapped_column(ForeignKey(FeedItem.id))
feed_id = mapped_column(ForeignKey(Feed.id))
14 changes: 14 additions & 0 deletions docker/alembic.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM python:3.11-slim-bullseye

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY ../ .


# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r app/alembic.txt

# # Run db_setup.py when the container launches
# CMD ["alembic", "init", "alembic"]
32 changes: 20 additions & 12 deletions docker/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@ services:
db-setup: # create extensions & schemas
container_name: db-setup
environment: *common-variables
depends_on:
db:
condition: service_started
restart: true
build:
context: ..
dockerfile: docker/Dockerfile
dockerfile: docker/db_setup.Dockerfile

alembic:
container_name: alembic-setup
image: python:3.11-slim-bullseye
environment: *common-variables
depends_on:
- db
command: >
/bin/sh -c "
alembic init alembic &&
alembic revision --autogenerate -m 'Initial migration' &&
alembic upgrade head"
# alembic:
# container_name: alembic-setup
# image: python:3.11-slim-bullseye
# environment: *common-variables
# depends_on:
# db-setup:
# condition: service_completed_successfully
# restart: true
# build:
# context: ..
# dockerfile: docker/alembic.Dockerfile
# command: >
# /bin/sh -c "
# alembic revision --autogenerate -m 'Initial migration' &&
# alembic upgrade head"
6 changes: 3 additions & 3 deletions docker/Dockerfile → docker/db_setup.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ FROM python:3.11-slim-bullseye
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY ../app .
COPY ../ .


# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir -r app/requirements.txt

# Run db_setup.py when the container launches
CMD ["python", "db_setup.py"]
CMD ["python", "app/db_setup.py"]
6 changes: 6 additions & 0 deletions gis.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export POSTGRES_USER=nyc
export POSTGRES_PASSWORD=gis
export POSTGRES_DB=nyc
export POSTGRES_HOST=db
export POSTGRES_DRIVERNAME=postgresql
export PORT=5432

0 comments on commit a65b843

Please sign in to comment.