--liquibase formatted sql

--changeset fpsms:printer_connectivity_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_reachable'
--comment: Last TCP connectivity probe result for printer monitoring
ALTER TABLE `printer`
  ADD COLUMN `last_reachable` tinyint(1) DEFAULT NULL,
  ADD COLUMN `last_check_at` datetime DEFAULT NULL,
  ADD COLUMN `last_check_error` varchar(255) DEFAULT NULL,
  ADD COLUMN `last_check_latency_ms` int DEFAULT NULL;

--changeset fpsms:printer_connectivity_event
--preconditions onFail:MARK_RAN
--precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'printer_connectivity_event'
--comment: Printer offline / connectivity event log
CREATE TABLE `printer_connectivity_event` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `printer_id` int NOT NULL,
  `printer_code` varchar(500) DEFAULT NULL,
  `printer_name` varchar(500) DEFAULT NULL,
  `printer_type` varchar(255) DEFAULT NULL,
  `brand` varchar(255) DEFAULT NULL,
  `ip` varchar(30) DEFAULT NULL,
  `port` int DEFAULT NULL,
  `reachable` tinyint(1) NOT NULL DEFAULT 0,
  `error_message` varchar(255) DEFAULT NULL,
  `latency_ms` int DEFAULT NULL,
  `recorded_at` datetime NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_pce_printer_recorded` (`printer_id`, `recorded_at`),
  KEY `idx_pce_recorded_at` (`recorded_at`),
  KEY `idx_pce_reachable_recorded` (`reachable`, `recorded_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
