ALTER TABLE vehicle_alert_rule
  ADD COLUMN threshold_high DOUBLE NOT NULL DEFAULT 0 AFTER threshold_value,
  ADD COLUMN scope_oems_json TEXT NULL AFTER scope_vins_json;

ALTER TABLE vehicle_alert_event
  ADD COLUMN threshold_high DOUBLE NOT NULL DEFAULT 0 AFTER threshold_value;

CREATE TABLE IF NOT EXISTS vehicle_alert_rule_state (
  rule_id VARCHAR(64) NOT NULL,
  vin VARCHAR(64) NOT NULL,
  protocol VARCHAR(32) NOT NULL,
  observed_value DOUBLE NOT NULL,
  last_observed_at DATETIME(3) NOT NULL,
  PRIMARY KEY (rule_id, vin, protocol),
  KEY idx_alert_rule_state_observed (last_observed_at)
);
