Skip to content
Merged
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
4 changes: 0 additions & 4 deletions admin/views/radionuclides.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def can_delete(self, request: Request) -> bool:
"chemistry_sample_info_id",
"nma_sample_pt_id",
"nma_sample_point_id",
"thing_id",
"analyte",
"symbol",
"sample_value",
Expand All @@ -85,7 +84,6 @@ def can_delete(self, request: Request) -> bool:
"chemistry_sample_info_id",
"nma_sample_pt_id",
"nma_sample_point_id",
"thing_id",
"analyte",
"symbol",
"sample_value",
Expand Down Expand Up @@ -127,7 +125,6 @@ def can_delete(self, request: Request) -> bool:
"chemistry_sample_info_id",
"nma_sample_pt_id",
"nma_sample_point_id",
"thing_id",
"analyte",
"symbol",
"sample_value",
Expand All @@ -149,7 +146,6 @@ def can_delete(self, request: Request) -> bool:
"chemistry_sample_info_id": "Chemistry Sample Info ID",
"nma_sample_pt_id": "NMA SamplePtID (Legacy)",
"nma_sample_point_id": "NMA SamplePointID (Legacy)",
"thing_id": "Thing ID",
"analyte": "Analyte",
"symbol": "Symbol",
"sample_value": "Sample Value",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""add thing_id to NMA_SurfaceWaterData

Revision ID: c7f8a9b0c1d2
Revises: d9f1e2c3b4a5
Create Date: 2026-02-04 12:03:00.000000

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "c7f8a9b0c1d2"
down_revision: Union[str, Sequence[str], None] = "d9f1e2c3b4a5"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
"""Upgrade schema."""
op.add_column(
"NMA_SurfaceWaterData",
sa.Column("thing_id", sa.Integer(), nullable=True),
)
op.create_foreign_key(
"fk_surface_water_data_thing_id",
"NMA_SurfaceWaterData",
"thing",
["thing_id"],
["id"],
ondelete="CASCADE",
)
# Backfill thing_id based on LocationId -> Thing.nma_pk_location
op.execute("""
UPDATE "NMA_SurfaceWaterData" sw
SET thing_id = t.id
FROM thing t
WHERE t.nma_pk_location IS NOT NULL
AND sw."LocationId" IS NOT NULL
AND t.nma_pk_location = sw."LocationId"::text
""")
# Remove any rows that cannot be linked to a Thing, then enforce NOT NULL
op.execute('DELETE FROM "NMA_SurfaceWaterData" WHERE thing_id IS NULL')
op.alter_column(
"NMA_SurfaceWaterData", "thing_id", existing_type=sa.Integer(), nullable=False
)


def downgrade() -> None:
"""Downgrade schema."""
op.drop_constraint(
"fk_surface_water_data_thing_id",
"NMA_SurfaceWaterData",
type_="foreignkey",
)
op.drop_column("NMA_SurfaceWaterData", "thing_id")
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Drop thing_id from NMA_Radionuclides

Revision ID: d9f1e2c3b4a5
Revises: 71a4c6b3d2e8
Create Date: 2026-02-04 15:32:00.000000

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "d9f1e2c3b4a5"
down_revision: Union[str, Sequence[str], None] = "71a4c6b3d2e8"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def _drop_thing_id_fk_and_indexes(inspector) -> None:
fks = inspector.get_foreign_keys("NMA_Radionuclides")
for fk in fks:
if "thing_id" in (fk.get("constrained_columns") or []):
op.drop_constraint(fk["name"], "NMA_Radionuclides", type_="foreignkey")

indexes = inspector.get_indexes("NMA_Radionuclides")
for idx in indexes:
if "thing_id" in (idx.get("column_names") or []):
op.drop_index(idx["name"], table_name="NMA_Radionuclides")


def upgrade() -> None:
"""Upgrade schema."""
bind = op.get_bind()
inspector = sa.inspect(bind)
columns = [col["name"] for col in inspector.get_columns("NMA_Radionuclides")]
if "thing_id" in columns:
_drop_thing_id_fk_and_indexes(inspector)
op.drop_column("NMA_Radionuclides", "thing_id")


def downgrade() -> None:
"""Downgrade schema."""
bind = op.get_bind()
inspector = sa.inspect(bind)
columns = [col["name"] for col in inspector.get_columns("NMA_Radionuclides")]
if "thing_id" not in columns:
op.add_column(
"NMA_Radionuclides",
sa.Column("thing_id", sa.Integer(), nullable=True),
)
op.create_foreign_key(
"fk_nma_radionuclides_thing_id",
"NMA_Radionuclides",
"thing",
["thing_id"],
["id"],
ondelete="CASCADE",
)
Loading