Skip to content

Commit 33541ac

Browse files
authored
Merge pull request #381 from DataIntegrationGroup/BDMS-433-Migrate-MajorChemistry
BDMS-433 NMA_MajorChemistry table and transfer logic for major chemistry data
2 parents 9f87ede + d86a6b1 commit 33541ac

7 files changed

Lines changed: 702 additions & 1 deletion

File tree

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ TRANSFER_ASSETS=False
2929
TRANSFER_SURFACE_WATER_DATA=True
3030
TRANSFER_HYDRAULICS_DATA=True
3131
TRANSFER_CHEMISTRY_SAMPLEINFO=True
32+
TRANSFER_MAJOR_CHEMISTRY=True
3233
TRANSFER_RADIONUCLIDES=True
3334
TRANSFER_NGWMN_VIEWS=True
3435
TRANSFER_WATERLEVELS_PRESSURE_DAILY=True
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""Create legacy NMA_MajorChemistry table.
2+
3+
Revision ID: a7b8c9d0e1f2
4+
Revises: f3b4c5d6e7f8
5+
Create Date: 2026-03-01 02:00:00.000000
6+
"""
7+
8+
from typing import Sequence, Union
9+
10+
import sqlalchemy as sa
11+
from alembic import op
12+
from sqlalchemy import inspect
13+
from sqlalchemy.dialects import postgresql
14+
15+
# revision identifiers, used by Alembic.
16+
revision: str = "a7b8c9d0e1f2"
17+
down_revision: Union[str, Sequence[str], None] = "f1a2b3c4d5e6"
18+
branch_labels: Union[str, Sequence[str], None] = None
19+
depends_on: Union[str, Sequence[str], None] = None
20+
21+
22+
def upgrade() -> None:
23+
"""Create the legacy major chemistry table."""
24+
bind = op.get_bind()
25+
inspector = inspect(bind)
26+
if not inspector.has_table("NMA_MajorChemistry"):
27+
op.create_table(
28+
"NMA_MajorChemistry",
29+
sa.Column(
30+
"SamplePtID",
31+
postgresql.UUID(as_uuid=True),
32+
sa.ForeignKey(
33+
"NMA_Chemistry_SampleInfo.SamplePtID", ondelete="CASCADE"
34+
),
35+
nullable=False,
36+
),
37+
sa.Column("SamplePointID", sa.String(length=10), nullable=True),
38+
sa.Column("Analyte", sa.String(length=50), nullable=True),
39+
sa.Column("Symbol", sa.String(length=50), nullable=True),
40+
sa.Column(
41+
"SampleValue", sa.Float(), nullable=True, server_default=sa.text("0")
42+
),
43+
sa.Column("Units", sa.String(length=50), nullable=True),
44+
sa.Column("Uncertainty", sa.Float(), nullable=True),
45+
sa.Column("AnalysisMethod", sa.String(length=255), nullable=True),
46+
sa.Column("AnalysisDate", sa.DateTime(), nullable=True),
47+
sa.Column("Notes", sa.String(length=255), nullable=True),
48+
sa.Column(
49+
"Volume", sa.Integer(), nullable=True, server_default=sa.text("0")
50+
),
51+
sa.Column("VolumeUnit", sa.String(length=50), nullable=True),
52+
sa.Column("OBJECTID", sa.Integer(), nullable=True, unique=True),
53+
sa.Column(
54+
"GlobalID",
55+
postgresql.UUID(as_uuid=True),
56+
nullable=False,
57+
primary_key=True,
58+
),
59+
sa.Column("AnalysesAgency", sa.String(length=50), nullable=True),
60+
sa.Column("WCLab_ID", sa.String(length=25), nullable=True),
61+
)
62+
op.create_index(
63+
"MajorChemistry$AnalysesAgency",
64+
"NMA_MajorChemistry",
65+
["AnalysesAgency"],
66+
)
67+
op.create_index(
68+
"MajorChemistry$Analyte",
69+
"NMA_MajorChemistry",
70+
["Analyte"],
71+
)
72+
op.create_index(
73+
"MajorChemistry$Chemistry SampleInfoMajorChemistry",
74+
"NMA_MajorChemistry",
75+
["SamplePtID"],
76+
)
77+
op.create_index(
78+
"MajorChemistry$SamplePointID",
79+
"NMA_MajorChemistry",
80+
["SamplePointID"],
81+
)
82+
op.create_index(
83+
"MajorChemistry$SamplePointIDAnalyte",
84+
"NMA_MajorChemistry",
85+
["SamplePointID", "Analyte"],
86+
)
87+
op.create_index(
88+
"MajorChemistry$SamplePtID",
89+
"NMA_MajorChemistry",
90+
["SamplePtID"],
91+
)
92+
op.create_index(
93+
"MajorChemistry$WCLab_ID",
94+
"NMA_MajorChemistry",
95+
["WCLab_ID"],
96+
)
97+
98+
99+
def downgrade() -> None:
100+
"""Drop the legacy major chemistry table."""
101+
bind = op.get_bind()
102+
inspector = inspect(bind)
103+
if inspector.has_table("NMA_MajorChemistry"):
104+
op.drop_table("NMA_MajorChemistry")

db/nma_legacy.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ class ChemistrySampleInfo(Base):
278278
passive_deletes=True,
279279
)
280280

281+
major_chemistries: Mapped[List["NMAMajorChemistry"]] = relationship(
282+
"NMAMajorChemistry",
283+
back_populates="chemistry_sample_info",
284+
cascade="all, delete-orphan",
285+
passive_deletes=True,
286+
)
287+
281288
@validates("thing_id")
282289
def validate_thing_id(self, key, value):
283290
"""Prevent orphan ChemistrySampleInfo - must have a parent Thing."""
@@ -455,4 +462,52 @@ def validate_sample_pt_id(self, key, value):
455462
return value
456463

457464

465+
class NMAMajorChemistry(Base):
466+
"""
467+
Legacy MajorChemistry table from NM_Aquifer_Dev_DB.
468+
"""
469+
470+
__tablename__ = "NMA_MajorChemistry"
471+
472+
global_id: Mapped[uuid.UUID] = mapped_column(
473+
"GlobalID", UUID(as_uuid=True), primary_key=True
474+
)
475+
sample_pt_id: Mapped[uuid.UUID] = mapped_column(
476+
"SamplePtID",
477+
UUID(as_uuid=True),
478+
ForeignKey("NMA_Chemistry_SampleInfo.SamplePtID", ondelete="CASCADE"),
479+
nullable=False,
480+
)
481+
sample_point_id: Mapped[Optional[str]] = mapped_column("SamplePointID", String(10))
482+
analyte: Mapped[Optional[str]] = mapped_column("Analyte", String(50))
483+
symbol: Mapped[Optional[str]] = mapped_column("Symbol", String(50))
484+
sample_value: Mapped[Optional[float]] = mapped_column(
485+
"SampleValue", Float, server_default=text("0")
486+
)
487+
units: Mapped[Optional[str]] = mapped_column("Units", String(50))
488+
uncertainty: Mapped[Optional[float]] = mapped_column("Uncertainty", Float)
489+
analysis_method: Mapped[Optional[str]] = mapped_column(
490+
"AnalysisMethod", String(255)
491+
)
492+
analysis_date: Mapped[Optional[datetime]] = mapped_column("AnalysisDate", DateTime)
493+
notes: Mapped[Optional[str]] = mapped_column("Notes", String(255))
494+
volume: Mapped[Optional[int]] = mapped_column(
495+
"Volume", Integer, server_default=text("0")
496+
)
497+
volume_unit: Mapped[Optional[str]] = mapped_column("VolumeUnit", String(50))
498+
object_id: Mapped[Optional[int]] = mapped_column("OBJECTID", Integer, unique=True)
499+
analyses_agency: Mapped[Optional[str]] = mapped_column("AnalysesAgency", String(50))
500+
wclab_id: Mapped[Optional[str]] = mapped_column("WCLab_ID", String(25))
501+
502+
chemistry_sample_info: Mapped["ChemistrySampleInfo"] = relationship(
503+
"ChemistrySampleInfo", back_populates="major_chemistries"
504+
)
505+
506+
@validates("sample_pt_id")
507+
def validate_sample_pt_id(self, key, value):
508+
if value is None:
509+
raise ValueError("NMAMajorChemistry requires a SamplePtID")
510+
return value
511+
512+
458513
# ============= EOF =============================================

0 commit comments

Comments
 (0)