Skip to content

Commit 56af195

Browse files
authored
Merge pull request #467 from DataIntegrationGroup/kas-bdms-540-standardize-PKs-FKs
kas-bdms-540-standardize-PKs-FKs
2 parents 88602e4 + 53df9c9 commit 56af195

9 files changed

Lines changed: 450 additions & 146 deletions

admin/views/radionuclides.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ def can_delete(self, request: Request) -> bool:
6363
"chemistry_sample_info_id",
6464
"nma_sample_pt_id",
6565
"nma_sample_point_id",
66-
"thing_id",
6766
"analyte",
6867
"symbol",
6968
"sample_value",
@@ -85,7 +84,6 @@ def can_delete(self, request: Request) -> bool:
8584
"chemistry_sample_info_id",
8685
"nma_sample_pt_id",
8786
"nma_sample_point_id",
88-
"thing_id",
8987
"analyte",
9088
"symbol",
9189
"sample_value",
@@ -127,7 +125,6 @@ def can_delete(self, request: Request) -> bool:
127125
"chemistry_sample_info_id",
128126
"nma_sample_pt_id",
129127
"nma_sample_point_id",
130-
"thing_id",
131128
"analyte",
132129
"symbol",
133130
"sample_value",
@@ -149,7 +146,6 @@ def can_delete(self, request: Request) -> bool:
149146
"chemistry_sample_info_id": "Chemistry Sample Info ID",
150147
"nma_sample_pt_id": "NMA SamplePtID (Legacy)",
151148
"nma_sample_point_id": "NMA SamplePointID (Legacy)",
152-
"thing_id": "Thing ID",
153149
"analyte": "Analyte",
154150
"symbol": "Symbol",
155151
"sample_value": "Sample Value",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""add thing_id to NMA_SurfaceWaterData
2+
3+
Revision ID: c7f8a9b0c1d2
4+
Revises: d9f1e2c3b4a5
5+
Create Date: 2026-02-04 12:03:00.000000
6+
7+
"""
8+
9+
from typing import Sequence, Union
10+
11+
import sqlalchemy as sa
12+
from alembic import op
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = "c7f8a9b0c1d2"
16+
down_revision: Union[str, Sequence[str], None] = "d9f1e2c3b4a5"
17+
branch_labels: Union[str, Sequence[str], None] = None
18+
depends_on: Union[str, Sequence[str], None] = None
19+
20+
21+
def upgrade() -> None:
22+
"""Upgrade schema."""
23+
op.add_column(
24+
"NMA_SurfaceWaterData",
25+
sa.Column("thing_id", sa.Integer(), nullable=True),
26+
)
27+
op.create_foreign_key(
28+
"fk_surface_water_data_thing_id",
29+
"NMA_SurfaceWaterData",
30+
"thing",
31+
["thing_id"],
32+
["id"],
33+
ondelete="CASCADE",
34+
)
35+
# Backfill thing_id based on LocationId -> Thing.nma_pk_location
36+
op.execute("""
37+
UPDATE "NMA_SurfaceWaterData" sw
38+
SET thing_id = t.id
39+
FROM thing t
40+
WHERE t.nma_pk_location IS NOT NULL
41+
AND sw."LocationId" IS NOT NULL
42+
AND t.nma_pk_location = sw."LocationId"::text
43+
""")
44+
# Remove any rows that cannot be linked to a Thing, then enforce NOT NULL
45+
op.execute('DELETE FROM "NMA_SurfaceWaterData" WHERE thing_id IS NULL')
46+
op.alter_column(
47+
"NMA_SurfaceWaterData", "thing_id", existing_type=sa.Integer(), nullable=False
48+
)
49+
50+
51+
def downgrade() -> None:
52+
"""Downgrade schema."""
53+
op.drop_constraint(
54+
"fk_surface_water_data_thing_id",
55+
"NMA_SurfaceWaterData",
56+
type_="foreignkey",
57+
)
58+
op.drop_column("NMA_SurfaceWaterData", "thing_id")
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""Drop thing_id from NMA_Radionuclides
2+
3+
Revision ID: d9f1e2c3b4a5
4+
Revises: 71a4c6b3d2e8
5+
Create Date: 2026-02-04 15:32:00.000000
6+
7+
"""
8+
9+
from typing import Sequence, Union
10+
11+
import sqlalchemy as sa
12+
from alembic import op
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = "d9f1e2c3b4a5"
16+
down_revision: Union[str, Sequence[str], None] = "71a4c6b3d2e8"
17+
branch_labels: Union[str, Sequence[str], None] = None
18+
depends_on: Union[str, Sequence[str], None] = None
19+
20+
21+
def _drop_thing_id_fk_and_indexes(inspector) -> None:
22+
fks = inspector.get_foreign_keys("NMA_Radionuclides")
23+
for fk in fks:
24+
if "thing_id" in (fk.get("constrained_columns") or []):
25+
op.drop_constraint(fk["name"], "NMA_Radionuclides", type_="foreignkey")
26+
27+
indexes = inspector.get_indexes("NMA_Radionuclides")
28+
for idx in indexes:
29+
if "thing_id" in (idx.get("column_names") or []):
30+
op.drop_index(idx["name"], table_name="NMA_Radionuclides")
31+
32+
33+
def upgrade() -> None:
34+
"""Upgrade schema."""
35+
bind = op.get_bind()
36+
inspector = sa.inspect(bind)
37+
columns = [col["name"] for col in inspector.get_columns("NMA_Radionuclides")]
38+
if "thing_id" in columns:
39+
_drop_thing_id_fk_and_indexes(inspector)
40+
op.drop_column("NMA_Radionuclides", "thing_id")
41+
42+
43+
def downgrade() -> None:
44+
"""Downgrade schema."""
45+
bind = op.get_bind()
46+
inspector = sa.inspect(bind)
47+
columns = [col["name"] for col in inspector.get_columns("NMA_Radionuclides")]
48+
if "thing_id" not in columns:
49+
op.add_column(
50+
"NMA_Radionuclides",
51+
sa.Column("thing_id", sa.Integer(), nullable=True),
52+
)
53+
op.create_foreign_key(
54+
"fk_nma_radionuclides_thing_id",
55+
"NMA_Radionuclides",
56+
"thing",
57+
["thing_id"],
58+
["id"],
59+
ondelete="CASCADE",
60+
)

0 commit comments

Comments
 (0)