    -- 修改oss_configs表结构，将access_key和secret_key改为VARCHAR(255)并设置默认值
    ALTER TABLE `oss_configs` 
    MODIFY COLUMN `access_key` VARCHAR(255) NOT NULL DEFAULT '',
    MODIFY COLUMN `secret_key` VARCHAR(255) NOT NULL DEFAULT '';

    -- 添加阿里云OSS专用字段（使用存储过程安全添加，如果已存在则跳过）
    SET @dbname = DATABASE();
    SET @tablename = 'oss_configs';
    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'oss_access_key_id') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `oss_access_key_id` VARCHAR(255) NOT NULL DEFAULT '' AFTER `secret_key`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'oss_access_key_secret') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `oss_access_key_secret` VARCHAR(255) NOT NULL DEFAULT '' AFTER `oss_access_key_id`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'oss_endpoint') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `oss_endpoint` VARCHAR(255) NOT NULL DEFAULT '' AFTER `oss_access_key_secret`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'oss_bucket') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `oss_bucket` VARCHAR(255) NOT NULL DEFAULT '' AFTER `oss_endpoint`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'oss_domain') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `oss_domain` VARCHAR(255) NOT NULL DEFAULT '' AFTER `oss_bucket`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    -- 添加腾讯云COS专用字段
    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'qcloud_secret_id') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `qcloud_secret_id` VARCHAR(255) NOT NULL DEFAULT '' AFTER `oss_domain`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'qcloud_secret_key') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `qcloud_secret_key` VARCHAR(255) NOT NULL DEFAULT '' AFTER `qcloud_secret_id`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'qcloud_region') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `qcloud_region` VARCHAR(255) NOT NULL DEFAULT '' AFTER `qcloud_secret_key`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'qcloud_bucket') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `qcloud_bucket` VARCHAR(255) NOT NULL DEFAULT '' AFTER `qcloud_region`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'qcloud_domain') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `qcloud_domain` VARCHAR(255) NOT NULL DEFAULT '' AFTER `qcloud_bucket`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    -- 添加七牛云专用字段
    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'qiniu_access_key') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `qiniu_access_key` VARCHAR(255) NOT NULL DEFAULT '' AFTER `qcloud_domain`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'qiniu_secret_key') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `qiniu_secret_key` VARCHAR(255) NOT NULL DEFAULT '' AFTER `qiniu_access_key`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'qiniu_bucket') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `qiniu_bucket` VARCHAR(255) NOT NULL DEFAULT '' AFTER `qiniu_secret_key`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'qiniu_region') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `qiniu_region` VARCHAR(255) NOT NULL DEFAULT '' AFTER `qiniu_bucket`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    SET @preparedStatement = (SELECT IF(
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = @dbname AND TABLE_NAME = @tablename AND COLUMN_NAME = 'qiniu_domain') > 0,
    'SELECT 1',
    'ALTER TABLE `oss_configs` ADD COLUMN `qiniu_domain` VARCHAR(255) NOT NULL DEFAULT '' AFTER `qiniu_region`'
    ));
    PREPARE alterIfNotExists FROM @preparedStatement;
    EXECUTE alterIfNotExists;
    DEALLOCATE PREPARE alterIfNotExists;

    -- 插入默认OSS配置（如果不存在）
    INSERT INTO `oss_configs` (`name`, `storage_type`, `access_key`, `secret_key`, `bucket`, `region`, `domain`, `is_active`, `deleted_at`, `created_at`, `updated_at`)
    SELECT '本地存储', 'local', '', '', '', '', '', 1, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
    WHERE NOT EXISTS (SELECT 1 FROM `oss_configs` WHERE `name` = '本地存储' AND `storage_type` = 'local' AND `deleted_at` = 0);

    INSERT INTO `oss_configs` (`name`, `storage_type`, `access_key`, `secret_key`, `bucket`, `region`, `domain`, `is_active`, `deleted_at`, `created_at`, `updated_at`)
    SELECT '阿里云OSS', 'aliyun', '', '', '', '', '', 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
    WHERE NOT EXISTS (SELECT 1 FROM `oss_configs` WHERE `name` = '阿里云OSS' AND `storage_type` = 'aliyun' AND `deleted_at` = 0);

    INSERT INTO `oss_configs` (`name`, `storage_type`, `access_key`, `secret_key`, `bucket`, `region`, `domain`, `is_active`, `deleted_at`, `created_at`, `updated_at`)
    SELECT '腾讯云COS', 'qcloud', '', '', '', '', '', 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
    WHERE NOT EXISTS (SELECT 1 FROM `oss_configs` WHERE `name` = '腾讯云COS' AND `storage_type` = 'qcloud' AND `deleted_at` = 0);

    INSERT INTO `oss_configs` (`name`, `storage_type`, `access_key`, `secret_key`, `bucket`, `region`, `domain`, `is_active`, `deleted_at`, `created_at`, `updated_at`)
    SELECT '七牛云', 'qiniu', '', '', '', '', '', 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
    WHERE NOT EXISTS (SELECT 1 FROM `oss_configs` WHERE `name` = '七牛云' AND `storage_type` = 'qiniu' AND `deleted_at` = 0);
