CREATE TABLE IF NOT EXISTS `log_message` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `platform` VARCHAR(32) NOT NULL,
  `app_version` VARCHAR(32) NULL,
  `os_name` VARCHAR(32) NULL,
  `os_version` VARCHAR(32) NULL,
  `device_id` VARCHAR(64) NULL,
  `user_id` BIGINT NULL DEFAULT NULL,
  `session_id` VARCHAR(64) NULL,
  `level` TINYINT UNSIGNED NOT NULL DEFAULT 3,
  `error_code` VARCHAR(64) NULL,
  `message` TEXT NOT NULL,
  `stack` MEDIUMTEXT NULL,
  `context` JSON NULL,
  `client_ip` VARCHAR(45) NULL,
  `user_agent` VARCHAR(255) NULL,
  `locale` VARCHAR(16) NULL,
  `digest` VARCHAR(64) NULL,
  `occurred_at` DATETIME NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_digest` (`digest`),
  KEY `idx_platform_time` (`platform`, `created_at`),
  KEY `idx_user_time` (`user_id`, `created_at`),
  KEY `idx_device_time` (`device_id`, `created_at`),
  KEY `idx_error_code` (`error_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
