-- ============================================
-- 内容压缩（gzip）配置字段迁移
-- 数据库: nginxserver @ 10.1.1.122:3306
-- 日期: 2026-06-14
-- 执行方式: mysql -h 10.1.1.122 -u nginxserver -p nginxserver < gzip_migration.sql
-- ============================================

USE nginxserver;

-- 安全添加列（检查是否已存在，避免重复执行报错）
DROP PROCEDURE IF EXISTS add_gzip_columns;

DELIMITER $$
CREATE PROCEDURE add_gzip_columns()
BEGIN
    -- gzip_enabled
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.COLUMNS
        WHERE TABLE_SCHEMA = 'nginxserver'
          AND TABLE_NAME = 'forwarding_rules'
          AND COLUMN_NAME = 'gzip_enabled'
    ) THEN
        ALTER TABLE forwarding_rules
            ADD COLUMN gzip_enabled TINYINT(1) DEFAULT 0
            COMMENT 'gzip压缩开关: 0-关闭, 1-开启';
    END IF;

    -- gzip_types
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.COLUMNS
        WHERE TABLE_SCHEMA = 'nginxserver'
          AND TABLE_NAME = 'forwarding_rules'
          AND COLUMN_NAME = 'gzip_types'
    ) THEN
        ALTER TABLE forwarding_rules
            ADD COLUMN gzip_types VARCHAR(500)
            DEFAULT 'text/plain text/css text/xml text/javascript application/x-javascript application/json application/xml application/xml+rss application/vnd.ms-fontobject application/x-font-ttf application/x-font-opentype application/x-font-truetype'
            COMMENT 'gzip压缩MIME类型(空格分隔)';
    END IF;

    -- gzip_comp_level
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.COLUMNS
        WHERE TABLE_SCHEMA = 'nginxserver'
          AND TABLE_NAME = 'forwarding_rules'
          AND COLUMN_NAME = 'gzip_comp_level'
    ) THEN
        ALTER TABLE forwarding_rules
            ADD COLUMN gzip_comp_level TINYINT DEFAULT 6
            COMMENT 'gzip压缩级别 1-9';
    END IF;

    -- gzip_min_length
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.COLUMNS
        WHERE TABLE_SCHEMA = 'nginxserver'
          AND TABLE_NAME = 'forwarding_rules'
          AND COLUMN_NAME = 'gzip_min_length'
    ) THEN
        ALTER TABLE forwarding_rules
            ADD COLUMN gzip_min_length VARCHAR(10) DEFAULT '1000'
            COMMENT 'gzip最小压缩长度(可带k/m单位)';
    END IF;
END$$
DELIMITER ;

-- 执行存储过程
CALL add_gzip_columns();

-- 清理
DROP PROCEDURE IF EXISTS add_gzip_columns;

-- 验证结果
SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_DEFAULT, COLUMN_COMMENT
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'nginxserver'
  AND TABLE_NAME = 'forwarding_rules'
  AND COLUMN_NAME LIKE 'gzip%';
