SET NAMES utf8mb4;

SET @current_schema = DATABASE();

SET @ddl = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @current_schema
      AND TABLE_NAME = 'user_login_logger'
      AND COLUMN_NAME = 'ip_country_code'
  ),
  'SELECT 1',
  'ALTER TABLE `user_login_logger` ADD COLUMN `ip_country_code` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '''' COMMENT ''登录IP国家码'' AFTER `ip`'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @current_schema
      AND TABLE_NAME = 'user_login_logger'
      AND COLUMN_NAME = 'ip_country_name'
  ),
  'SELECT 1',
  'ALTER TABLE `user_login_logger` ADD COLUMN `ip_country_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '''' COMMENT ''登录IP国家/地区名称'' AFTER `ip_country_code`'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @current_schema
      AND TABLE_NAME = 'user_login_logger'
      AND COLUMN_NAME = 'ip_region'
  ),
  'SELECT 1',
  'ALTER TABLE `user_login_logger` ADD COLUMN `ip_region` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '''' COMMENT ''登录IP地区/省'' AFTER `ip_country_name`'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @current_schema
      AND TABLE_NAME = 'user_login_logger'
      AND COLUMN_NAME = 'req_zone_id'
  ),
  'SELECT 1',
  'ALTER TABLE `user_login_logger` ADD COLUMN `req_zone_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '''' COMMENT ''请求时区'' AFTER `ip_region`'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @current_schema
      AND TABLE_NAME = 'user_login_logger'
      AND COLUMN_NAME = 'login_blocked'
  ),
  'SELECT 1',
  'ALTER TABLE `user_login_logger` ADD COLUMN `login_blocked` tinyint(1) NOT NULL DEFAULT 0 COMMENT ''是否被登录访问校验屏蔽'' AFTER `req_zone_id`'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = @current_schema
      AND TABLE_NAME = 'user_login_logger'
      AND COLUMN_NAME = 'block_reason'
  ),
  'SELECT 1',
  'ALTER TABLE `user_login_logger` ADD COLUMN `block_reason` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '''' COMMENT ''登录访问校验屏蔽原因'' AFTER `login_blocked`'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
