-- Migration script for gateway_offline_tracking table
-- Add alert_sent column if it doesn't exist and remove online_alert_sent

-- Modify table to add alert_sent column
ALTER TABLE gateway_offline_tracking 
ADD COLUMN IF NOT EXISTS alert_sent INT DEFAULT 0;

-- Remove online_alert_sent column if it exists
ALTER TABLE gateway_offline_tracking 
DROP COLUMN IF EXISTS online_alert_sent;

-- Update existing rows to set default alert status
UPDATE gateway_offline_tracking 
SET alert_sent = 0 
WHERE alert_sent IS NULL;

-- Ensure the table structure matches our current requirements
ALTER TABLE gateway_offline_tracking 
MODIFY COLUMN gateway_id VARCHAR(255) PRIMARY KEY,
MODIFY COLUMN first_offline_time DATETIME,
MODIFY COLUMN last_check_time DATETIME,
MODIFY COLUMN alert_sent INT DEFAULT 0;
