CREATE TABLE IF NOT EXISTS flyway_schema_history_analytical_db
(
    `installed_rank` Int32,
    `version` Nullable(String),
    `description` String,
    `type` String,
    `script` String,
    `checksum` Nullable(Int32),
    `installed_by` String,
    `installed_on` DateTime DEFAULT now(),
    `execution_time` Int32,
    `success` UInt8
)
ENGINE = ReplacingMergeTree(installed_on)
ORDER BY installed_rank;

CREATE TABLE IF NOT EXISTS ods_chat_session
(
    `id` UInt64,
    `tenant_id` UInt64,
    `dept_id` UInt64,
    `user_id` UInt64,
    `user_account` String,
    `assistant_id` UInt64,
    `assistant_code` Nullable(String),
    `assistant_name` Nullable(String),
    `title` String,
    `last_message_preview` Nullable(String),
    `last_sender_id` Nullable(UInt64),
    `last_sender_name` Nullable(String),
    `message_count` UInt32,
    `access_at` DateTime,
    `last_message_at` Nullable(DateTime),
    `created` DateTime,
    `created_by` UInt64,
    `modified` DateTime,
    `modified_by` UInt64,
    `is_deleted` UInt8
)
ENGINE = ReplacingMergeTree(modified)
PARTITION BY toYYYYMM(access_at)
ORDER BY (id);

CREATE TABLE IF NOT EXISTS ods_chat_log
(
    `id` UInt64,
    `session_id` UInt64,
    `user_id` UInt64,
    `assistant_id` UInt64,
    `sender_id` Nullable(UInt64),
    `sender_name` Nullable(String),
    `sender_role` String,
    `content_type` String,
    `content_text` Nullable(String),
    `content_payload` Nullable(String),
    `created` DateTime,
    `created_by` UInt64,
    `sync_version` UInt64
)
ENGINE = ReplacingMergeTree(sync_version)
PARTITION BY toYYYYMM(created)
ORDER BY (session_id, created, id);
