-- Publish interval migration: days -> hours

ALTER TABLE sites ADD COLUMN publish_interval_hours INT NOT NULL DEFAULT 24 AFTER api_token;

SET @has_old_interval_days := (
    SELECT COUNT(*)
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'sites'
      AND COLUMN_NAME = 'publish_interval_days'
);

SET @backpro_sql := IF(
    @has_old_interval_days > 0,
    'UPDATE sites SET publish_interval_hours = CASE WHEN publish_interval_days IS NULL OR publish_interval_days < 1 THEN 24 ELSE publish_interval_days * 24 END',
    'SELECT 1'
);

PREPARE backpro_stmt FROM @backpro_sql;
EXECUTE backpro_stmt;
DEALLOCATE PREPARE backpro_stmt;
