SET @schema_name := DATABASE();

SET @lang_column_exists := (
    SELECT COUNT(1)
    FROM information_schema.columns
    WHERE table_schema = @schema_name
      AND table_name = 'event'
      AND column_name = 'lang'
);

SET @add_lang_column_sql := IF(
    @lang_column_exists = 0,
    'ALTER TABLE `event` ADD COLUMN lang VARCHAR(10) NULL COMMENT ''이벤트 노출 언어'' AFTER title',
    'SELECT ''event.lang already exists'' AS message'
);

PREPARE add_lang_column_stmt FROM @add_lang_column_sql;
EXECUTE add_lang_column_stmt;
DEALLOCATE PREPARE add_lang_column_stmt;

UPDATE `event`
SET lang = 'KO'
WHERE lang IS NULL;

SET @lang_column_nullable := (
    SELECT IS_NULLABLE
    FROM information_schema.columns
    WHERE table_schema = @schema_name
      AND table_name = 'event'
      AND column_name = 'lang'
);

SET @alter_lang_column_sql := IF(
    @lang_column_nullable = 'YES',
    'ALTER TABLE `event` MODIFY COLUMN lang VARCHAR(10) NOT NULL DEFAULT ''KO'' COMMENT ''이벤트 노출 언어 (KO 기본, EN/JA 등록 가능)''',
    'SELECT ''event.lang already normalized'' AS message'
);

PREPARE alter_lang_column_stmt FROM @alter_lang_column_sql;
EXECUTE alter_lang_column_stmt;
DEALLOCATE PREPARE alter_lang_column_stmt;
