-- Migration: 0002_positions_faulty -- Adds the faulty column to positions and ensures the (device_id, ts DESC) index -- needed by the snapshot-on-subscribe query exists. -- -- The faulty column is set post-hoc by operators in Directus when a position is -- flagged as unrealistic. The snapshot-on-subscribe query (task 1.5.5) filters -- WHERE faulty = false to exclude flagged positions from the initial map state. -- The live broadcast path (Redis stream → fan-out) never touches this column -- because faulty flags are applied after the fact. ALTER TABLE positions ADD COLUMN IF NOT EXISTS faulty boolean NOT NULL DEFAULT false; -- Index for the snapshot DISTINCT ON query: -- SELECT DISTINCT ON (device_id) ... ORDER BY device_id, ts DESC -- TimescaleDB scans only the latest chunks for devices with recent activity, -- but the (device_id, ts DESC) index makes per-device latest-position lookups -- efficient regardless of chunk age. CREATE INDEX IF NOT EXISTS positions_device_ts_idx ON positions (device_id, ts DESC);