-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add unique constraints for NMA_WaterLevelsContinuous_Pressure_Daily and NGWMN tables #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jirhiker
merged 1 commit into
staging
from
migration/waterlevels-continuous-pressure-daily
Jan 7, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
alembic/versions/4b7aa74b15ad_add_unique_constraint_pressure_daily.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
113
alembic/versions/5f4e2b0a6b8b_ensure_ngwmn_unique_constraints.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| 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; | ||
| $$; | ||
| """ | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 CONFLICTupserts 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 👍 / 👎.