towerops/priv/repo/migrations/20260317224403_backfill_default_ping_checks.exs
Graham McIntire c8a092196f add auto ICMP ping check for all devices (#68)
## Summary

- Every device now automatically gets an ICMP ping check (`source_type: "auto_discovery"`) when created via `Monitoring.ensure_default_ping_check/1`
- When a device IP changes, the ping check host config is synced automatically
- Backfill migration inserts ICMP Ping checks for all existing devices that don't have one
- Fixes checkbox alignment in the Add Service Check modal (verify SSL / follow redirects)

## Test plan

- [x] 4 new tests for `ensure_default_ping_check/1` (create, idempotent, IP update, no-op)
- [x] 3 new tests for device lifecycle (auto-create on device create, sync on IP change, no-op when IP unchanged)
- [x] 13 existing tests updated to account for auto-created ping check
- [x] Full test suite passes (`mix precommit`)
- [ ] Verify backfill migration on staging
- [ ] Create device in UI, confirm ping check appears in service checks list
- [ ] Verify checkbox alignment in Add Service Check modal

Reviewed-on: graham/towerops-web#68
2026-03-17 17:55:23 -05:00

43 lines
1 KiB
Elixir

defmodule Towerops.Repo.Migrations.BackfillDefaultPingChecks do
use Ecto.Migration
def up do
execute """
INSERT INTO checks (id, organization_id, device_id, name, check_type, config, enabled,
interval_seconds, timeout_ms, source_type, current_state,
current_state_type, current_check_attempt, inserted_at, updated_at)
SELECT
gen_random_uuid(),
d.organization_id,
d.id,
'ICMP Ping',
'ping',
jsonb_build_object('host', d.ip_address, 'count', 3),
true,
60,
5000,
'auto_discovery',
3,
'soft',
1,
NOW(),
NOW()
FROM devices d
WHERE NOT EXISTS (
SELECT 1 FROM checks c
WHERE c.device_id = d.id
AND c.check_type = 'ping'
AND c.source_type = 'auto_discovery'
)
"""
end
def down do
execute """
DELETE FROM checks
WHERE check_type = 'ping'
AND source_type = 'auto_discovery'
AND name = 'ICMP Ping'
"""
end
end