From da3503223faaf6521bec3685a0db0b378488b135 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Mon, 30 Mar 2026 13:27:58 -0500 Subject: [PATCH] Fix missing precipitation columns and backfill weather data Add idempotent migration to ensure precip_1h_in and wx_codes columns exist, then reset weather_queued flag and clear stale observations so the cron re-fetches with precipitation data included. --- ...330182625_ensure_precipitation_columns.exs | 32 +++++++++++++++++++ ...urface_observations_with_precipitation.exs | 17 ++++++++++ 2 files changed, 49 insertions(+) create mode 100644 priv/repo/migrations/20260330182625_ensure_precipitation_columns.exs create mode 100644 priv/repo/migrations/20260330182652_backfill_surface_observations_with_precipitation.exs diff --git a/priv/repo/migrations/20260330182625_ensure_precipitation_columns.exs b/priv/repo/migrations/20260330182625_ensure_precipitation_columns.exs new file mode 100644 index 00000000..ffcc70ad --- /dev/null +++ b/priv/repo/migrations/20260330182625_ensure_precipitation_columns.exs @@ -0,0 +1,32 @@ +defmodule Microwaveprop.Repo.Migrations.EnsurePrecipitationColumns do + use Ecto.Migration + + def up do + execute """ + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'surface_observations' AND column_name = 'precip_1h_in' + ) THEN + ALTER TABLE surface_observations ADD COLUMN precip_1h_in double precision; + END IF; + + IF NOT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'surface_observations' AND column_name = 'wx_codes' + ) THEN + ALTER TABLE surface_observations ADD COLUMN wx_codes character varying(255); + END IF; + END + $$; + """ + end + + def down do + alter table(:surface_observations) do + remove :precip_1h_in + remove :wx_codes + end + end +end diff --git a/priv/repo/migrations/20260330182652_backfill_surface_observations_with_precipitation.exs b/priv/repo/migrations/20260330182652_backfill_surface_observations_with_precipitation.exs new file mode 100644 index 00000000..b8a6d779 --- /dev/null +++ b/priv/repo/migrations/20260330182652_backfill_surface_observations_with_precipitation.exs @@ -0,0 +1,17 @@ +defmodule Microwaveprop.Repo.Migrations.BackfillSurfaceObservationsWithPrecipitation do + use Ecto.Migration + + def up do + # Delete surface observations that were fetched without precipitation data + # so the weather cron re-fetches them with the new p01i/wxcodes fields + execute "DELETE FROM surface_observations WHERE precip_1h_in IS NULL AND wx_codes IS NULL" + + # Reset the weather_queued flag so QSOs are re-enqueued for weather fetching + execute "UPDATE qsos SET weather_queued = false" + end + + def down do + # Cannot restore deleted observations; no-op + :ok + end +end