-- sys_license_infos 增加 request_code 字段
-- 用途：按当前机器请求码筛选并激活对应许可证
-- 适用数据库：MySQL

SET @current_db := DATABASE();

SET @column_exists := (
  SELECT COUNT(1)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = @current_db
    AND TABLE_NAME = 'sys_license_infos'
    AND COLUMN_NAME = 'request_code'
);

SET @add_column_sql := IF(
  @column_exists = 0,
  'ALTER TABLE sys_license_infos ADD COLUMN request_code VARCHAR(2048) NULL COMMENT ''许可证请求码'' AFTER license_time',
  'SELECT ''column request_code already exists'' '
);
PREPARE stmt_add_column FROM @add_column_sql;
EXECUTE stmt_add_column;
DEALLOCATE PREPARE stmt_add_column;

SET @index_exists := (
  SELECT COUNT(1)
  FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = @current_db
    AND TABLE_NAME = 'sys_license_infos'
    AND INDEX_NAME = 'idx_sys_license_infos_request_code_create_time'
);

SET @add_index_sql := IF(
  @index_exists = 0,
  'ALTER TABLE sys_license_infos ADD INDEX idx_sys_license_infos_request_code_create_time (request_code(191), create_time)',
  'SELECT ''index idx_sys_license_infos_request_code_create_time already exists'' '
);
PREPARE stmt_add_index FROM @add_index_sql;
EXECUTE stmt_add_index;
DEALLOCATE PREPARE stmt_add_index;

SHOW COLUMNS FROM sys_license_infos LIKE 'request_code';
SHOW INDEX FROM sys_license_infos WHERE Key_name = 'idx_sys_license_infos_request_code_create_time';
