-- ============================================================
-- OSS 配置表专用字段迁移脚本
-- 文件：server/scripts/migrate_oss_configs.sql
-- 说明：可重复执行（幂等），为 oss_configs 补齐阿里云/腾讯云/七牛专用列
-- 用法：mysql -u root -p your_database < scripts/migrate_oss_configs.sql
-- ============================================================

SET NAMES utf8mb4;

-- 阿里云 OSS
SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'oss_access_key_id'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `oss_access_key_id` varchar(255) NOT NULL DEFAULT '''' COMMENT ''阿里云 AccessKeyId（AES加密）'' AFTER `domain`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'oss_access_key_secret'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `oss_access_key_secret` varchar(255) NOT NULL DEFAULT '''' COMMENT ''阿里云 AccessKeySecret（AES加密）'' AFTER `oss_access_key_id`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'oss_endpoint'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `oss_endpoint` varchar(255) NOT NULL DEFAULT '''' COMMENT ''阿里云 Endpoint'' AFTER `oss_access_key_secret`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'oss_bucket'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `oss_bucket` varchar(255) NOT NULL DEFAULT '''' COMMENT ''阿里云 Bucket'' AFTER `oss_endpoint`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'oss_domain'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `oss_domain` varchar(255) NOT NULL DEFAULT '''' COMMENT ''阿里云自定义域名'' AFTER `oss_bucket`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 腾讯云 COS
SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'qcloud_secret_id'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `qcloud_secret_id` varchar(255) NOT NULL DEFAULT '''' COMMENT ''腾讯云 SecretId（AES加密）'' AFTER `oss_domain`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'qcloud_secret_key'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `qcloud_secret_key` varchar(255) NOT NULL DEFAULT '''' COMMENT ''腾讯云 SecretKey（AES加密）'' AFTER `qcloud_secret_id`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'qcloud_region'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `qcloud_region` varchar(255) NOT NULL DEFAULT '''' COMMENT ''腾讯云 Region'' AFTER `qcloud_secret_key`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'qcloud_bucket'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `qcloud_bucket` varchar(255) NOT NULL DEFAULT '''' COMMENT ''腾讯云 Bucket'' AFTER `qcloud_region`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'qcloud_domain'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `qcloud_domain` varchar(255) NOT NULL DEFAULT '''' COMMENT ''腾讯云自定义域名'' AFTER `qcloud_bucket`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 七牛云
SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'qiniu_access_key'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `qiniu_access_key` varchar(255) NOT NULL DEFAULT '''' COMMENT ''七牛 AccessKey（AES加密）'' AFTER `qcloud_domain`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'qiniu_secret_key'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `qiniu_secret_key` varchar(255) NOT NULL DEFAULT '''' COMMENT ''七牛 SecretKey（AES加密）'' AFTER `qiniu_access_key`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'qiniu_bucket'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `qiniu_bucket` varchar(255) NOT NULL DEFAULT '''' COMMENT ''七牛 Bucket'' AFTER `qiniu_secret_key`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'qiniu_region'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `qiniu_region` varchar(255) NOT NULL DEFAULT '''' COMMENT ''七牛 Region'' AFTER `qiniu_bucket`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'oss_configs' AND COLUMN_NAME = 'qiniu_domain'
);
SET @sql := IF(@col_exists = 0,
  'ALTER TABLE `oss_configs` ADD COLUMN `qiniu_domain` varchar(255) NOT NULL DEFAULT '''' COMMENT ''七牛自定义域名'' AFTER `qiniu_region`',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
