|
| 1 | +"""add latest tds pygeoapi materialized view |
| 2 | +
|
| 3 | +Revision ID: i2b3c4d5e6f7 |
| 4 | +Revises: d5e6f7a8b9c0 |
| 5 | +Create Date: 2026-03-02 11:00:00.000000 |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import Sequence, Union |
| 9 | + |
| 10 | +from alembic import op |
| 11 | +from sqlalchemy import inspect, text |
| 12 | + |
| 13 | +# revision identifiers, used by Alembic. |
| 14 | +revision: str = "i2b3c4d5e6f7" |
| 15 | +down_revision: Union[str, Sequence[str], None] = "d5e6f7a8b9c0" |
| 16 | +branch_labels: Union[str, Sequence[str], None] = None |
| 17 | +depends_on: Union[str, Sequence[str], None] = None |
| 18 | + |
| 19 | +LATEST_LOCATION_CTE = """ |
| 20 | +SELECT DISTINCT ON (lta.thing_id) |
| 21 | + lta.thing_id, |
| 22 | + lta.location_id, |
| 23 | + lta.effective_start |
| 24 | +FROM location_thing_association AS lta |
| 25 | +WHERE lta.effective_end IS NULL |
| 26 | +ORDER BY lta.thing_id, lta.effective_start DESC |
| 27 | +""".strip() |
| 28 | + |
| 29 | + |
| 30 | +def _create_latest_tds_view() -> str: |
| 31 | + return f""" |
| 32 | + CREATE VIEW ogc_latest_tds_wells AS |
| 33 | + WITH latest_location AS ( |
| 34 | +{LATEST_LOCATION_CTE} |
| 35 | + ), |
| 36 | + tds_obs AS ( |
| 37 | + SELECT |
| 38 | + csi.thing_id, |
| 39 | + mc.id AS major_chemistry_id, |
| 40 | + COALESCE(mc."AnalysisDate", csi."CollectionDate")::date AS observation_date, |
| 41 | + mc."SampleValue" AS sample_value, |
| 42 | + mc."Units" AS units |
| 43 | + FROM "NMA_MajorChemistry" AS mc |
| 44 | + JOIN "NMA_Chemistry_SampleInfo" AS csi |
| 45 | + ON csi.id = mc.chemistry_sample_info_id |
| 46 | + JOIN thing AS t ON t.id = csi.thing_id |
| 47 | + WHERE |
| 48 | + t.thing_type = 'water well' |
| 49 | + AND mc."SampleValue" IS NOT NULL |
| 50 | + AND ( |
| 51 | + lower(coalesce(mc."Analyte", '')) IN ( |
| 52 | + 'tds', |
| 53 | + 'total dissolved solids' |
| 54 | + ) |
| 55 | + OR lower(coalesce(mc."Symbol", '')) = 'tds' |
| 56 | + ) |
| 57 | + ), |
| 58 | + ranked_tds AS ( |
| 59 | + SELECT |
| 60 | + to2.thing_id, |
| 61 | + to2.major_chemistry_id, |
| 62 | + to2.observation_date, |
| 63 | + to2.sample_value, |
| 64 | + to2.units, |
| 65 | + ROW_NUMBER() OVER ( |
| 66 | + PARTITION BY to2.thing_id |
| 67 | + ORDER BY to2.observation_date DESC NULLS LAST, to2.major_chemistry_id DESC |
| 68 | + ) AS rn |
| 69 | + FROM tds_obs AS to2 |
| 70 | + ) |
| 71 | + SELECT |
| 72 | + t.id AS id, |
| 73 | + t.name, |
| 74 | + t.thing_type, |
| 75 | + rt.major_chemistry_id, |
| 76 | + rt.observation_date AS latest_tds_observation_date, |
| 77 | + rt.sample_value AS latest_tds_value, |
| 78 | + rt.units AS latest_tds_units, |
| 79 | + l.point |
| 80 | + FROM ranked_tds AS rt |
| 81 | + JOIN thing AS t ON t.id = rt.thing_id |
| 82 | + JOIN latest_location AS ll ON ll.thing_id = t.id |
| 83 | + JOIN location AS l ON l.id = ll.location_id |
| 84 | + WHERE rt.rn = 1 |
| 85 | + """ |
| 86 | + |
| 87 | + |
| 88 | +def _create_avg_tds_view() -> str: |
| 89 | + return f""" |
| 90 | + CREATE MATERIALIZED VIEW ogc_avg_tds_wells AS |
| 91 | + WITH latest_location AS ( |
| 92 | +{LATEST_LOCATION_CTE} |
| 93 | + ), |
| 94 | + tds_obs AS ( |
| 95 | + SELECT |
| 96 | + csi.thing_id, |
| 97 | + mc.id AS major_chemistry_id, |
| 98 | + COALESCE(mc."AnalysisDate", csi."CollectionDate")::date AS observation_date, |
| 99 | + mc."SampleValue" AS sample_value, |
| 100 | + mc."Units" AS units |
| 101 | + FROM "NMA_MajorChemistry" AS mc |
| 102 | + JOIN "NMA_Chemistry_SampleInfo" AS csi |
| 103 | + ON csi.id = mc.chemistry_sample_info_id |
| 104 | + JOIN thing AS t ON t.id = csi.thing_id |
| 105 | + WHERE |
| 106 | + t.thing_type = 'water well' |
| 107 | + AND mc."SampleValue" IS NOT NULL |
| 108 | + AND ( |
| 109 | + lower(coalesce(mc."Analyte", '')) IN ( |
| 110 | + 'tds', |
| 111 | + 'total dissolved solids' |
| 112 | + ) |
| 113 | + OR lower(coalesce(mc."Symbol", '')) = 'tds' |
| 114 | + ) |
| 115 | + ) |
| 116 | + SELECT |
| 117 | + t.id AS id, |
| 118 | + t.name, |
| 119 | + t.thing_type, |
| 120 | + COUNT(to2.major_chemistry_id)::integer AS tds_observation_count, |
| 121 | + AVG(to2.sample_value)::double precision AS avg_tds_value, |
| 122 | + MIN(to2.observation_date) AS first_tds_observation_date, |
| 123 | + MAX(to2.observation_date) AS last_tds_observation_date, |
| 124 | + l.point |
| 125 | + FROM tds_obs AS to2 |
| 126 | + JOIN thing AS t ON t.id = to2.thing_id |
| 127 | + JOIN latest_location AS ll ON ll.thing_id = t.id |
| 128 | + JOIN location AS l ON l.id = ll.location_id |
| 129 | + GROUP BY t.id, t.name, t.thing_type, l.point |
| 130 | + """ |
| 131 | + |
| 132 | + |
| 133 | +def _create_avg_tds_view_with_datetime_columns() -> str: |
| 134 | + return f""" |
| 135 | + CREATE MATERIALIZED VIEW ogc_avg_tds_wells AS |
| 136 | + WITH latest_location AS ( |
| 137 | +{LATEST_LOCATION_CTE} |
| 138 | + ), |
| 139 | + tds_obs AS ( |
| 140 | + SELECT |
| 141 | + csi.thing_id, |
| 142 | + mc.id AS major_chemistry_id, |
| 143 | + mc."AnalysisDate" AS analysis_date, |
| 144 | + mc."SampleValue" AS sample_value, |
| 145 | + mc."Units" AS units |
| 146 | + FROM "NMA_MajorChemistry" AS mc |
| 147 | + JOIN "NMA_Chemistry_SampleInfo" AS csi |
| 148 | + ON csi.id = mc.chemistry_sample_info_id |
| 149 | + JOIN thing AS t ON t.id = csi.thing_id |
| 150 | + WHERE |
| 151 | + t.thing_type = 'water well' |
| 152 | + AND mc."SampleValue" IS NOT NULL |
| 153 | + AND ( |
| 154 | + lower(coalesce(mc."Analyte", '')) IN ( |
| 155 | + 'tds', |
| 156 | + 'total dissolved solids' |
| 157 | + ) |
| 158 | + OR lower(coalesce(mc."Symbol", '')) = 'tds' |
| 159 | + ) |
| 160 | + ) |
| 161 | + SELECT |
| 162 | + t.id AS id, |
| 163 | + t.name, |
| 164 | + t.thing_type, |
| 165 | + COUNT(to2.major_chemistry_id)::integer AS tds_observation_count, |
| 166 | + AVG(to2.sample_value)::double precision AS avg_tds_value, |
| 167 | + MIN(to2.analysis_date::date) AS first_tds_observation_date, |
| 168 | + MAX(to2.analysis_date::date) AS last_tds_observation_date, |
| 169 | + l.point |
| 170 | + FROM tds_obs AS to2 |
| 171 | + JOIN thing AS t ON t.id = to2.thing_id |
| 172 | + JOIN latest_location AS ll ON ll.thing_id = t.id |
| 173 | + JOIN location AS l ON l.id = ll.location_id |
| 174 | + GROUP BY t.id, t.name, t.thing_type, l.point |
| 175 | + """ |
| 176 | + |
| 177 | + |
| 178 | +def upgrade() -> None: |
| 179 | + bind = op.get_bind() |
| 180 | + inspector = inspect(bind) |
| 181 | + existing_tables = set(inspector.get_table_names(schema="public")) |
| 182 | + required_tds = {"NMA_MajorChemistry", "NMA_Chemistry_SampleInfo"} |
| 183 | + |
| 184 | + if not required_tds.issubset(existing_tables): |
| 185 | + missing_tds_tables = sorted(t for t in required_tds if t not in existing_tables) |
| 186 | + missing_tds_tables_str = ", ".join(missing_tds_tables) |
| 187 | + raise RuntimeError( |
| 188 | + "Cannot create TDS views. The following required " |
| 189 | + f"tables are missing: {missing_tds_tables_str}" |
| 190 | + ) |
| 191 | + |
| 192 | + op.execute(text("DROP MATERIALIZED VIEW IF EXISTS ogc_avg_tds_wells")) |
| 193 | + op.execute(text("DROP VIEW IF EXISTS ogc_latest_tds_wells")) |
| 194 | + op.execute(text("DROP MATERIALIZED VIEW IF EXISTS ogc_latest_tds_wells")) |
| 195 | + |
| 196 | + op.execute(text(_create_avg_tds_view())) |
| 197 | + op.execute( |
| 198 | + text( |
| 199 | + "COMMENT ON MATERIALIZED VIEW ogc_avg_tds_wells IS " |
| 200 | + "'Average TDS per well from major chemistry results for pygeoapi.'" |
| 201 | + ) |
| 202 | + ) |
| 203 | + op.execute( |
| 204 | + text("CREATE UNIQUE INDEX ux_ogc_avg_tds_wells_id " "ON ogc_avg_tds_wells (id)") |
| 205 | + ) |
| 206 | + |
| 207 | + op.execute(text(_create_latest_tds_view())) |
| 208 | + op.execute( |
| 209 | + text( |
| 210 | + "COMMENT ON VIEW ogc_latest_tds_wells IS " |
| 211 | + "'Latest TDS per well from major chemistry results for pygeoapi.'" |
| 212 | + ) |
| 213 | + ) |
| 214 | + |
| 215 | + |
| 216 | +def downgrade() -> None: |
| 217 | + op.execute(text("DROP MATERIALIZED VIEW IF EXISTS ogc_avg_tds_wells")) |
| 218 | + op.execute(text("DROP VIEW IF EXISTS ogc_latest_tds_wells")) |
| 219 | + op.execute(text("DROP MATERIALIZED VIEW IF EXISTS ogc_latest_tds_wells")) |
| 220 | + op.execute(text(_create_avg_tds_view_with_datetime_columns())) |
| 221 | + op.execute( |
| 222 | + text("CREATE UNIQUE INDEX ux_ogc_avg_tds_wells_id " "ON ogc_avg_tds_wells (id)") |
| 223 | + ) |
0 commit comments