generated from bcgov/quickstart-openshift
-
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.
feat: datamodel updates and migration to add data
- Loading branch information
1 parent
e2af408
commit 797d5d2
Showing
17 changed files
with
1,008 additions
and
120 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
"""migration message | ||
Revision ID: V4 | ||
Revises: V3 | ||
Create Date: 2024-01-30 17:09:02.177727 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
import sqlmodel | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "V5" | ||
down_revision: Union[str, None] = "V4" | ||
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! ### | ||
# connection = op.get_bind() | ||
# op.add_column( | ||
# "basins", sa.Column("basin_id", sa.Integer(), nullable=False), schema="py_api" | ||
# ) | ||
# op.drop_column("basins", "id", schema="py_api") | ||
|
||
op.create_table( | ||
"alert_levels", | ||
sa.Column("alert_level_id", sa.Integer(), nullable=False), | ||
sa.Column("alert_level", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.PrimaryKeyConstraint("alert_level_id"), | ||
schema="py_api", | ||
) | ||
op.create_table( | ||
"alerts", | ||
sa.Column( | ||
"alert_description", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column( | ||
"alert_hydro_conditions", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column( | ||
"alert_meteorological_conditions", | ||
sqlmodel.sql.sqltypes.AutoString(), | ||
nullable=False, | ||
), | ||
sa.Column("alert_created", sa.DateTime(), nullable=False), | ||
sa.Column("alert_updated", sa.DateTime(), nullable=False), | ||
sa.Column("author_name", sqlmodel.sql.sqltypes.AutoString(), nullable=True), | ||
sa.Column("alert_status", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column("alert_id", sa.Integer(), nullable=False), | ||
sa.PrimaryKeyConstraint("alert_id"), | ||
schema="py_api", | ||
) | ||
op.create_table( | ||
"cap_event", | ||
sa.Column("alert_id", sa.Integer(), nullable=False), | ||
sa.Column("alert_level_id", sa.Integer(), nullable=False), | ||
sa.Column( | ||
"cap_event_status", sqlmodel.sql.sqltypes.AutoString(), nullable=False | ||
), | ||
sa.Column("cap_event_created_date", sa.DateTime(), nullable=False), | ||
sa.Column("cap_event_updated_date", sa.DateTime(), nullable=False), | ||
sa.Column("cap_event_id", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["alert_id"], | ||
["py_api.alerts.alert_id"], | ||
), | ||
sa.ForeignKeyConstraint( | ||
["alert_level_id"], | ||
["py_api.alert_levels.alert_level_id"], | ||
), | ||
sa.PrimaryKeyConstraint("cap_event_id"), | ||
schema="py_api", | ||
) | ||
op.create_table( | ||
"cap_event_areas", | ||
sa.Column("cap_event_area_id", sa.Integer(), nullable=False), | ||
sa.Column("basin_id", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["basin_id"], | ||
["py_api.basins.basin_id"], | ||
), | ||
sa.PrimaryKeyConstraint("cap_event_area_id"), | ||
) | ||
op.create_table( | ||
"alert_areas", | ||
sa.Column("alert_area_id", sa.Integer(), nullable=False), | ||
sa.Column("alert_id", sa.Integer(), nullable=False), | ||
sa.Column("basin_id", sa.Integer(), nullable=False), | ||
sa.Column("alert_level_id", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["alert_id"], | ||
["py_api.alerts.alert_id"], | ||
), | ||
sa.ForeignKeyConstraint( | ||
["alert_level_id"], | ||
["py_api.alert_levels.alert_level_id"], | ||
), | ||
sa.ForeignKeyConstraint( | ||
["basin_id"], | ||
["py_api.basins.basin_id"], | ||
), | ||
sa.PrimaryKeyConstraint("alert_area_id"), | ||
schema="py_api", | ||
) | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
# op.add_column( | ||
# "basins", | ||
# sa.Column( | ||
# "id", | ||
# sa.INTEGER(), | ||
# server_default=sa.text("nextval('py_api.basins_id_seq'::regclass)"), | ||
# autoincrement=True, | ||
# nullable=False, | ||
# ), | ||
# schema="py_api", | ||
# ) | ||
# op.drop_column("basins", "basin_id", schema="py_api") | ||
op.drop_table("alert_areas", schema="py_api") | ||
op.drop_table("cap_event_areas") | ||
op.drop_table("cap_event", schema="py_api") | ||
op.drop_table("alerts", schema="py_api") | ||
op.drop_table("alert_levels", schema="py_api") | ||
|
||
# connection = op.get_bind() | ||
|
||
# ### end Alembic commands ### |
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,63 @@ | ||
"""add alert level fixtures | ||
Revision ID: V5 | ||
Revises: V4 | ||
Create Date: 2024-01-29 11:38:30.304621 | ||
""" | ||
import logging | ||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
import sqlmodel | ||
from alembic import op | ||
from src.v1.models import model | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "V6" | ||
down_revision: Union[str, None] = "V5" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
alert_levels = [ | ||
model.Alert_Levels(alert_level="High Streamflow Advisory"), | ||
model.Alert_Levels(alert_level="Flood Watch"), | ||
model.Alert_Levels(alert_level="Flood Warning"), | ||
] | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
bind = op.get_bind() | ||
session = sqlmodel.Session(bind=bind) | ||
|
||
for alert_level in alert_levels: | ||
LOGGER.debug(f"adding alert_level: {alert_level}") | ||
session.add(alert_level) | ||
|
||
session.commit() | ||
session.close() | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
bind = op.get_bind() | ||
session = sqlmodel.Session(bind=bind) | ||
|
||
for alert_level in alert_levels: | ||
stmt = sqlmodel.select(model.Alert_Levels).where( # noqa: F405 | ||
model.Alert_Levels.alert_level == alert_level.alert_level | ||
) | ||
results = session.exec(stmt) | ||
print("results: ", results.all()) | ||
if results.all(): | ||
alert_level_result = results.one() | ||
session.delete(alert_level_result) | ||
|
||
session.commit() | ||
session.close() | ||
|
||
# ### end Alembic commands ### |
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,95 @@ | ||
"""add mockup data | ||
Revision ID: V6 | ||
Revises: V5 | ||
Create Date: 2024-01-29 12:28:16.951414 | ||
""" | ||
import datetime | ||
import logging | ||
from typing import List, Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
import sqlmodel | ||
from alembic import op | ||
from src.v1.models import model | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "V7" | ||
down_revision: Union[str, None] = "V6" | ||
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! ### | ||
# rename id to alert_id | ||
bind = op.get_bind() | ||
session = sqlmodel.Session(bind=bind) | ||
|
||
basin_1_select = sqlmodel.select(model.Basins).where( | ||
model.Basins.basin_name == "Haida Gwaii" | ||
) | ||
basin_1 = session.exec(basin_1_select).one() | ||
LOGGER.debug(f"basin_1: {basin_1}") | ||
|
||
alert_level_select = sqlmodel.select(model.Alert_Levels).where( | ||
model.Alert_Levels.alert_level == "High Streamflow Advisory" | ||
) | ||
# alert_level = session.exec(alert_level_select).one() | ||
# LOGGER.debug(f"alert_level: {alert_level}") | ||
|
||
alert_data_1 = model.Alerts( | ||
alert_description="Alert Description 1", | ||
alert_hydro_conditions="Alert Hydro Conditions 1", | ||
alert_meteorological_conditions="Alert Meteorological Conditions 1", | ||
author_name="Rocky B.", | ||
alert_status="active", | ||
basins=[basin_1], | ||
# alert_levels=[alert_level], | ||
# basin_id=basin_1.basin_id, | ||
# alert_level_id=alert_level.alert_level_id, | ||
) | ||
# session.add(alert_data_1) | ||
# session.commit() | ||
session.add(alert_data_1) | ||
|
||
LOGGER.debug(f"alert_data_1 added: {alert_data_1}") | ||
|
||
# area_alert_data_1 = model.Alert_Areas( | ||
# alert_area="Alert Area 1", | ||
# alert_level_id=alert_level.alert_level_id, | ||
# basin_id=basin_1.basin_id, | ||
# alert_id=alert_data_1, | ||
# ) | ||
# session.add(area_alert_data_1) | ||
session.commit() | ||
|
||
# basin_data_1 = model.Basins(basin_name='Haida Gwaii') | ||
|
||
# area_alert_data_1 = model.Alert_Areas( | ||
# alert_area="Alert Area 1", | ||
# alert_level_id=1, | ||
# basin_id=81, | ||
# alert_id=alert_data_1, | ||
# ) | ||
|
||
# session.add(alert_data_1) | ||
# session.commit() | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
bind = op.get_bind() | ||
session = sqlmodel.Session(bind=bind) | ||
|
||
sql = "DELETE FROM py_api.alerts WHERE alert_description is not null;" | ||
op.execute(sql) | ||
sql = "DELETE FROM py_api.alert_areas WHERE alert_area_id is not null;" | ||
op.execute(sql) | ||
|
||
pass | ||
# ### end Alembic commands ### |
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
backend/alembic/versions/V4_rename_basin_and_sub_basin_pks.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 @@ | ||
"""rename basin and sub basin pks | ||
Revision ID: V7 | ||
Revises: V6 | ||
Create Date: 2024-01-31 12:01:14.390799 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
import sqlmodel | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "V4" | ||
down_revision: Union[str, None] = "V3" | ||
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.execute("ALTER TABLE py_api.basins RENAME COLUMN id TO basin_id;") | ||
op.execute("ALTER TABLE py_api.subbasins RENAME COLUMN id TO subbasin_id;") | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.execute("ALTER TABLE py_api.basins RENAME COLUMN basin_id TO id;") | ||
op.execute("ALTER TABLE py_api.subbasins RENAME COLUMN subbasin_id TO id;") | ||
# ### end Alembic commands ### |
Oops, something went wrong.