Skip to content
Open
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
43 changes: 43 additions & 0 deletions alembic/versions/g4a5b6c7d8e9_change_minor_trace_volume_to_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""change NMA_MinorTraceChemistry volume from Float to Integer

Revision ID: g4a5b6c7d8e9
Revises: f3b4c5d6e7f8
Create Date: 2026-01-14 12:00:00.000000

This migration changes the volume column in NMA_MinorTraceChemistry from Float to Integer
to match the source database schema (NM_Aquifer_Dev_DB).
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "g4a5b6c7d8e9"
down_revision: Union[str, Sequence[str], None] = "f3b4c5d6e7f8"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
"""Change volume column from Float to Integer."""
op.alter_column(
"NMA_MinorTraceChemistry",
"volume",
existing_type=sa.Float(),
type_=sa.Integer(),
existing_nullable=True,
postgresql_using="volume::integer",
)


def downgrade() -> None:
"""Revert volume column from Integer back to Float."""
op.alter_column(
"NMA_MinorTraceChemistry",
"volume",
existing_type=sa.Integer(),
type_=sa.Float(),
existing_nullable=True,
)
2 changes: 1 addition & 1 deletion db/nma_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ class NMAMinorTraceChemistry(Base):
notes: Mapped[Optional[str]] = mapped_column(Text)
analyses_agency: Mapped[Optional[str]] = mapped_column(String(100))
uncertainty: Mapped[Optional[float]] = mapped_column(Float)
volume: Mapped[Optional[float]] = mapped_column(Float)
volume: Mapped[Optional[int]] = mapped_column(Integer)
volume_unit: Mapped[Optional[str]] = mapped_column(String(20))

# --- Relationships ---
Expand Down
4 changes: 2 additions & 2 deletions tests/test_nma_chemistry_lineage.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_nma_minor_trace_chemistry_save_all_columns(shared_well):
notes="Test measurement",
analyses_agency="NMBGMR",
uncertainty=0.002,
volume=500.0,
volume=500,
volume_unit="mL",
)
session.add(mtc)
Expand All @@ -174,7 +174,7 @@ def test_nma_minor_trace_chemistry_save_all_columns(shared_well):
assert mtc.notes == "Test measurement"
assert mtc.analyses_agency == "NMBGMR"
assert mtc.uncertainty == 0.002
assert mtc.volume == 500.0
assert mtc.volume == 500
assert mtc.volume_unit == "mL"

session.delete(sample_info)
Expand Down
9 changes: 8 additions & 1 deletion transfers/minor_trace_chemistry_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def _row_to_dict(self, row) -> Optional[dict[str, Any]]:
"notes": self._safe_str(row, "Notes"),
"analyses_agency": self._safe_str(row, "AnalysesAgency"),
"uncertainty": self._safe_float(row, "Uncertainty"),
"volume": self._safe_float(row, "Volume"),
"volume": self._safe_int(row, "Volume"),
"volume_unit": self._safe_str(row, "VolumeUnit"),
}

Expand Down Expand Up @@ -225,6 +225,13 @@ def _safe_float(self, row, attr: str) -> Optional[float]:
return None
return float(val)

def _safe_int(self, row, attr: str) -> Optional[int]:
"""Safely get an int value, returning None for NaN."""
val = getattr(row, attr, None)
if val is None or pd.isna(val):
return None
return int(val)

def _parse_date(self, row, attr: str) -> Optional[date]:
"""Parse a date value from the row."""
val = getattr(row, attr, None)
Expand Down
Loading