--liquibase formatted sql

--changeset fpsms:printer_label_odometer_cache_columns
--preconditions onFail:MARK_RAN
--precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'printer' AND column_name = 'last_label_odometer'
--comment: Cache latest Zebra label odometer reading for label-printer-monitor
ALTER TABLE `printer`
  ADD COLUMN `last_label_odometer` bigint DEFAULT NULL,
  ADD COLUMN `last_label_odometer_at` datetime DEFAULT NULL;

--changeset fpsms:printer_label_odometer_log
--preconditions onFail:MARK_RAN
--precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'printer_label_odometer_log'
--comment: Per-check label odometer snapshot for Zebra label printers
CREATE TABLE `printer_label_odometer_log` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `printer_id` int NOT NULL,
  `odometer_total` bigint DEFAULT NULL,
  `delta_since_previous` bigint DEFAULT NULL,
  `reachable` tinyint(1) NOT NULL DEFAULT 0,
  `latency_ms` int DEFAULT NULL,
  `error_message` varchar(255) DEFAULT NULL,
  `host_status_snippet` varchar(500) DEFAULT NULL,
  `recorded_at` datetime NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_plol_printer_recorded` (`printer_id`, `recorded_at`),
  KEY `idx_plol_recorded_at` (`recorded_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
