-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#18 added alembic migration to add hourly_avg view
- Loading branch information
Nik Sauer
committed
Nov 7, 2024
1 parent
c9b2592
commit 5d1fccc
Showing
3 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
code/alembic/versions/8e1c45de1275_create_hourly_avg_view.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 @@ | ||
"""create hourly_avg view | ||
Revision ID: 8e1c45de1275 | ||
Revises: 5516d2dd3e78 | ||
Create Date: 2024-11-07 14:21:42.941381 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '8e1c45de1275' | ||
down_revision: Union[str, None] = '5516d2dd3e78' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.execute("""create view hourly_avg as | ||
select station_id, dimension, date_trunc('hour', time_measured) as hour, avg(value) | ||
from measurements as m join values as v on v.measurement_id = m.id | ||
join stations as s on s.id = m.station_id | ||
group by station_id, dimension, hour | ||
order by station_id, dimension, hour;""") | ||
|
||
|
||
|
||
def downgrade() -> None: | ||
op.execute("drop view if exists hourly_avg") |
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 |
---|---|---|
|
@@ -26,4 +26,4 @@ def calculate_hourly_average(station_id: int, db: Session): | |
) | ||
db.add(hourly_avg) | ||
|
||
db.commit() | ||
db.commit() |