Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cascade to tables #61

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions code/alembic/versions/29dab04045ac_add_cascade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""add cascade

Revision ID: 29dab04045ac
Revises: 80277768fe1d
Create Date: 2025-01-22 14:34:44.988247

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '29dab04045ac'
down_revision: Union[str, None] = '80277768fe1d'
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! ###
pass
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
60 changes: 60 additions & 0 deletions code/alembic/versions/5826b05428eb_added_cascade_to_all_fks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""added cascade to all fks

Revision ID: 5826b05428eb
Revises: a344b0c7bf56
Create Date: 2025-01-22 14:46:33.823214

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '5826b05428eb'
down_revision: Union[str, None] = 'a344b0c7bf56'
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.drop_constraint('cities_country_id_fkey', 'cities', type_='foreignkey')
op.create_foreign_key(None, 'cities', 'countries', ['country_id'], ['id'], ondelete='CASCADE')
op.drop_constraint('locations_country_id_fkey', 'locations', type_='foreignkey')
op.drop_constraint('locations_city_id_fkey', 'locations', type_='foreignkey')
op.create_foreign_key(None, 'locations', 'countries', ['country_id'], ['id'], ondelete='CASCADE')
op.create_foreign_key(None, 'locations', 'cities', ['city_id'], ['id'], ondelete='CASCADE')
op.drop_constraint('measurements_location_id_fkey', 'measurements', type_='foreignkey')
op.drop_constraint('measurements_station_id_fkey', 'measurements', type_='foreignkey')
op.create_foreign_key(None, 'measurements', 'stations', ['station_id'], ['id'], ondelete='CASCADE')
op.create_foreign_key(None, 'measurements', 'locations', ['location_id'], ['id'], ondelete='CASCADE')
op.drop_constraint('stationStatus_station_id_fkey', 'stationStatus', type_='foreignkey')
op.create_foreign_key(None, 'stationStatus', 'stations', ['station_id'], ['id'], ondelete='CASCADE')
op.drop_constraint('stations_location_id_fkey', 'stations', type_='foreignkey')
op.create_foreign_key(None, 'stations', 'locations', ['location_id'], ['id'], ondelete='CASCADE')
op.drop_constraint('values_measurement_id_fkey', 'values', type_='foreignkey')
op.create_foreign_key(None, 'values', 'measurements', ['measurement_id'], ['id'], ondelete='CASCADE')
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'values', type_='foreignkey')
op.create_foreign_key('values_measurement_id_fkey', 'values', 'measurements', ['measurement_id'], ['id'])
op.drop_constraint(None, 'stations', type_='foreignkey')
op.create_foreign_key('stations_location_id_fkey', 'stations', 'locations', ['location_id'], ['id'])
op.drop_constraint(None, 'stationStatus', type_='foreignkey')
op.create_foreign_key('stationStatus_station_id_fkey', 'stationStatus', 'stations', ['station_id'], ['id'])
op.drop_constraint(None, 'measurements', type_='foreignkey')
op.drop_constraint(None, 'measurements', type_='foreignkey')
op.create_foreign_key('measurements_station_id_fkey', 'measurements', 'stations', ['station_id'], ['id'])
op.create_foreign_key('measurements_location_id_fkey', 'measurements', 'locations', ['location_id'], ['id'])
op.drop_constraint(None, 'locations', type_='foreignkey')
op.drop_constraint(None, 'locations', type_='foreignkey')
op.create_foreign_key('locations_city_id_fkey', 'locations', 'cities', ['city_id'], ['id'])
op.create_foreign_key('locations_country_id_fkey', 'locations', 'countries', ['country_id'], ['id'])
op.drop_constraint(None, 'cities', type_='foreignkey')
op.create_foreign_key('cities_country_id_fkey', 'cities', 'countries', ['country_id'], ['id'])
# ### end Alembic commands ###
30 changes: 30 additions & 0 deletions code/alembic/versions/a344b0c7bf56_changed_cascade_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""changed cascade method

Revision ID: a344b0c7bf56
Revises: 29dab04045ac
Create Date: 2025-01-22 14:39:45.568462

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'a344b0c7bf56'
down_revision: Union[str, None] = '29dab04045ac'
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! ###
pass
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
30 changes: 30 additions & 0 deletions code/alembic/versions/b6289a85b50a_fixed_cascades.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""fixed cascades

Revision ID: b6289a85b50a
Revises: 5826b05428eb
Create Date: 2025-01-22 14:53:14.426837

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'b6289a85b50a'
down_revision: Union[str, None] = '5826b05428eb'
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! ###
pass
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
60 changes: 33 additions & 27 deletions code/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from sqlalchemy import Column, Integer, String, Float, ForeignKey, DateTime, JSON
from sqlalchemy.orm import relationship
from database import Base

from slugify import slugify


Expand All @@ -12,8 +11,9 @@ class Country(Base):
name = Column(String, unique=True, index=True)
slug = Column(String, unique=True, index=True)
code = Column(String, unique=True, index=True)
# Relationships:
cities = relationship("City", back_populates="country")

# Relationships
cities = relationship("City", back_populates="country", cascade="all, delete-orphan")

def __init__(self, name, code):
self.name = name
Expand All @@ -28,10 +28,11 @@ class City(Base):
name = Column(String, index=True)
slug = Column(String, unique=True, index=True)
tz = Column(String, nullable=True)
# Relationships:
country_id = Column(Integer, ForeignKey('countries.id'))

# Relationships
country_id = Column(Integer, ForeignKey('countries.id', ondelete="CASCADE"))
country = relationship("Country", back_populates="cities")
locations = relationship("Location", back_populates="city")
locations = relationship("Location", back_populates="city", cascade="all, delete-orphan")

lat = Column(Float)
lon = Column(Float)
Expand All @@ -52,13 +53,14 @@ class Location(Base):
lat = Column(Float)
lon = Column(Float)
height = Column(Float)
# Relationships:
city_id = Column(Integer, ForeignKey('cities.id'))

# Relationships
city_id = Column(Integer, ForeignKey('cities.id', ondelete="CASCADE"))
city = relationship("City", back_populates="locations")
country_id = Column(Integer, ForeignKey('countries.id'))
country_id = Column(Integer, ForeignKey('countries.id', ondelete="CASCADE"))
country = relationship("Country")
stations = relationship("Station", back_populates="location")
measurements = relationship("Measurement", back_populates="location")
stations = relationship("Station", back_populates="location", cascade="all, delete-orphan")
measurements = relationship("Measurement", back_populates="location", cascade="all, delete-orphan")


class Station(Base):
Expand All @@ -70,12 +72,13 @@ class Station(Base):
apikey = Column(String)
last_active = Column(DateTime)
source = Column(Integer)
# Relationships:
location_id = Column(Integer, ForeignKey('locations.id'))

# Relationships
location_id = Column(Integer, ForeignKey('locations.id', ondelete="CASCADE"))
location = relationship("Location", back_populates="stations")
measurements = relationship("Measurement", back_populates="station")
hourly_avg = relationship("HourlyDimensionAverages", back_populates="station")
stationStatus = relationship("StationStatus", back_populates="station")
measurements = relationship("Measurement", back_populates="station", cascade="all, delete-orphan")
hourly_avg = relationship("HourlyDimensionAverages", back_populates="station", cascade="all, delete-orphan")
stationStatus = relationship("StationStatus", back_populates="station", cascade="all, delete-orphan")


class Measurement(Base):
Expand All @@ -85,12 +88,13 @@ class Measurement(Base):
time_received = Column(DateTime)
time_measured = Column(DateTime)
sensor_model = Column(Integer)
# Relationships:
location_id = Column(Integer, ForeignKey('locations.id'))

# Relationships
location_id = Column(Integer, ForeignKey('locations.id', ondelete="CASCADE"))
location = relationship("Location", back_populates="measurements")
station_id = Column(Integer, ForeignKey('stations.id'))
station_id = Column(Integer, ForeignKey('stations.id', ondelete="CASCADE"))
station = relationship("Station", back_populates="measurements")
values = relationship("Values", back_populates="measurement")
values = relationship("Values", back_populates="measurement", cascade="all, delete-orphan")


class Values(Base):
Expand All @@ -99,25 +103,27 @@ class Values(Base):
id = Column(Integer, primary_key=True, index=True)
dimension = Column(Integer)
value = Column(Float)
# Relationships:
measurement_id = Column(Integer, ForeignKey('measurements.id'))

# Relationships
measurement_id = Column(Integer, ForeignKey('measurements.id', ondelete="CASCADE"))
measurement = relationship("Measurement", back_populates="values")


class StationStatus(Base):
__tablename__ = "stationStatus"

id = Column(Integer, primary_key=True, index=True)
station_id = Column(Integer, ForeignKey('stations.id'))
station_id = Column(Integer, ForeignKey('stations.id', ondelete="CASCADE"))
station = relationship("Station", back_populates="stationStatus")
timestamp = Column(DateTime)
level = Column(Integer)
message = Column(String)


class HourlyDimensionAverages(Base):
__tablename__ = 'hourly_avg' # This should match your view name in PostgreSQL
__tablename__ = 'hourly_avg'

station_id = Column(Integer, ForeignKey('stations.id'), primary_key=True) # Assuming 'station_id' uniquely identifies the record
station_id = Column(Integer, ForeignKey('stations.id', ondelete="CASCADE"), primary_key=True)
station = relationship("Station", back_populates="hourly_avg")
hour = Column(DateTime, primary_key=True) # Hour as a datetime truncated to hour precision
dimension_avg = Column(JSON) # JSON column to store {dimension_id: avg_value} dictionary
hour = Column(DateTime, primary_key=True) # Hour as a datetime truncated to hour precision
dimension_avg = Column(JSON) # JSON column to store {dimension_id: avg_value} dictionary
Loading