SET @sys_user_theme_exists = (
    SELECT COUNT(1)
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'sys_user'
      AND COLUMN_NAME = 'ui_theme_mode'
);
SET @sys_user_theme_sql = IF(
    @sys_user_theme_exists = 0,
    'ALTER TABLE sys_user ADD COLUMN ui_theme_mode VARCHAR(16) NULL AFTER valid_to',
    'SELECT 1'
);
PREPARE stmt_sys_user_theme FROM @sys_user_theme_sql;
EXECUTE stmt_sys_user_theme;
DEALLOCATE PREPARE stmt_sys_user_theme;

SET @sys_user_density_exists = (
    SELECT COUNT(1)
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'sys_user'
      AND COLUMN_NAME = 'ui_density'
);
SET @sys_user_density_sql = IF(
    @sys_user_density_exists = 0,
    'ALTER TABLE sys_user ADD COLUMN ui_density VARCHAR(16) NULL AFTER ui_theme_mode',
    'SELECT 1'
);
PREPARE stmt_sys_user_density FROM @sys_user_density_sql;
EXECUTE stmt_sys_user_density;
DEALLOCATE PREPARE stmt_sys_user_density;

SET @platform_user_theme_exists = (
    SELECT COUNT(1)
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'platform_user'
      AND COLUMN_NAME = 'ui_theme_mode'
);
SET @platform_user_theme_sql = IF(
    @platform_user_theme_exists = 0,
    'ALTER TABLE platform_user ADD COLUMN ui_theme_mode VARCHAR(16) NULL AFTER valid_to',
    'SELECT 1'
);
PREPARE stmt_platform_user_theme FROM @platform_user_theme_sql;
EXECUTE stmt_platform_user_theme;
DEALLOCATE PREPARE stmt_platform_user_theme;

SET @platform_user_density_exists = (
    SELECT COUNT(1)
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'platform_user'
      AND COLUMN_NAME = 'ui_density'
);
SET @platform_user_density_sql = IF(
    @platform_user_density_exists = 0,
    'ALTER TABLE platform_user ADD COLUMN ui_density VARCHAR(16) NULL AFTER ui_theme_mode',
    'SELECT 1'
);
PREPARE stmt_platform_user_density FROM @platform_user_density_sql;
EXECUTE stmt_platform_user_density;
DEALLOCATE PREPARE stmt_platform_user_density;

UPDATE sys_user
SET ui_theme_mode = IFNULL(NULLIF(ui_theme_mode, ''), 'SYSTEM'),
    ui_density = IFNULL(NULLIF(ui_density, ''), 'COMFORTABLE')
WHERE is_deleted = 0;

UPDATE platform_user
SET ui_theme_mode = IFNULL(NULLIF(ui_theme_mode, ''), 'SYSTEM'),
    ui_density = IFNULL(NULLIF(ui_density, ''), 'COMFORTABLE')
WHERE is_deleted = 0;
