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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Add unique constraint for NMA_WaterLevelsContinuous_Pressure_Daily

Revision ID: 4b7aa74b15ad
Revises: 8a1de3e3f0b3
Create Date: 2026-02-10 01:00:00.000000
"""

from typing import Sequence, Union

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "4b7aa74b15ad"
down_revision: Union[str, Sequence[str], None] = "8a1de3e3f0b3"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
"""Ensure unique constraint on GlobalID for upserts."""
op.create_unique_constraint(
"uq_nma_pressure_daily_globalid",
"NMA_WaterLevelsContinuous_Pressure_Daily",
["GlobalID"],
)


def downgrade() -> None:
"""Drop the unique constraint."""
op.drop_constraint(
"uq_nma_pressure_daily_globalid",
"NMA_WaterLevelsContinuous_Pressure_Daily",
type_="unique",
)
113 changes: 113 additions & 0 deletions alembic/versions/5f4e2b0a6b8b_ensure_ngwmn_unique_constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""Ensure NGWMN unique constraints for upserts

Revision ID: 5f4e2b0a6b8b
Revises: 4b7aa74b15ad
Create Date: 2026-02-10 01:20:00.000000
"""

from typing import Sequence, Union

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "5f4e2b0a6b8b"
down_revision: Union[str, Sequence[str], None] = "4b7aa74b15ad"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
"""Add unique constraints needed for ON CONFLICT upserts (idempotent)."""
op.execute(
"""
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'uq_nma_view_ngwmn_waterlevels_point_date'
) THEN
ALTER TABLE "NMA_view_NGWMN_WaterLevels"
ADD CONSTRAINT uq_nma_view_ngwmn_waterlevels_point_date UNIQUE ("PointID", "DateMeasured");
END IF;
END;
$$;
"""
)

op.execute(
"""
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'uq_nma_view_ngwmn_wellconstruction_point_casing_screen'
) THEN
ALTER TABLE "NMA_view_NGWMN_WellConstruction"
ADD CONSTRAINT uq_nma_view_ngwmn_wellconstruction_point_casing_screen
UNIQUE ("PointID", "CasingTop", "ScreenTop");
END IF;
END;
$$;
"""
)

op.execute(
"""
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'uq_nma_view_ngwmn_lithology_objectid'
) THEN
ALTER TABLE "NMA_view_NGWMN_Lithology"
ADD CONSTRAINT uq_nma_view_ngwmn_lithology_objectid UNIQUE ("OBJECTID");
END IF;
END;
$$;
"""
)


def downgrade() -> None:
"""Drop the NGWMN unique constraints if they exist."""
op.execute(
"""
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'uq_nma_view_ngwmn_waterlevels_point_date'
) THEN
ALTER TABLE "NMA_view_NGWMN_WaterLevels"
DROP CONSTRAINT uq_nma_view_ngwmn_waterlevels_point_date;
END IF;
Comment on lines +68 to +79

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep NGWMN constraints when downgrading 5f4e2b0a6b8b

The downgrade here drops the NGWMN unique constraints even though they are originally introduced in an earlier migration (8a1de3e3f0b3). If someone downgrades from 5f4e2b0a6b8b to 4b7aa74b15ad, Alembic will run this block and remove constraints that should still exist at that revision, which can break the ON CONFLICT upserts for NGWMN backfills. Since this migration’s upgrade is only a conditional “ensure,” its downgrade should be a no-op (or only revert changes it made) to avoid leaving the schema in an invalid historical state.

Useful? React with 👍 / 👎.

END;
$$;
"""
)

op.execute(
"""
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'uq_nma_view_ngwmn_wellconstruction_point_casing_screen'
) THEN
ALTER TABLE "NMA_view_NGWMN_WellConstruction"
DROP CONSTRAINT uq_nma_view_ngwmn_wellconstruction_point_casing_screen;
END IF;
END;
$$;
"""
)

op.execute(
"""
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'uq_nma_view_ngwmn_lithology_objectid'
) THEN
ALTER TABLE "NMA_view_NGWMN_Lithology"
DROP CONSTRAINT uq_nma_view_ngwmn_lithology_objectid;
END IF;
END;
$$;
"""
)
2 changes: 1 addition & 1 deletion transfers/backfill/staging.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def run(batch_size: int = 1000) -> None:
Execute all backfill steps in a deterministic order.
"""
steps = (
# ("WaterLevelsContinuous_Pressure_Daily", run_pressure_daily),
("WaterLevelsContinuous_Pressure_Daily", run_pressure_daily),
("NGWMN views", run_ngwmn_views),
)

Expand Down
Loading