CREATE TABLE ias_operation_logs (
    id BIGSERIAL PRIMARY KEY,
    level VARCHAR(32) NOT NULL DEFAULT 'info',
    module VARCHAR(128) NOT NULL DEFAULT '',
    message TEXT NOT NULL DEFAULT '',
    metadata JSONB NOT NULL DEFAULT '{}',
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_ias_operation_logs_created
    ON ias_operation_logs(created_at DESC);

CREATE INDEX idx_ias_operation_logs_level
    ON ias_operation_logs(level);

CREATE INDEX idx_ias_operation_logs_module
    ON ias_operation_logs(module);

INSERT INTO ias_upgrade_logs (
    version,
    released_at,
    title,
    description,
    changes
) VALUES (
    '0.2.25',
    '2026-07-08T00:00:00+08:00',
    '操作日志入库与查询',
    '新增操作日志表 ias_operation_logs，支持按级别、模块和关键词筛选日志，并新增日志查询页面。',
    '[
        "新增 ias_operation_logs 数据库表，支持记录操作日志（级别、模块、消息、元数据和时间戳）。",
        "新增 GET /v1/logs 日志查询接口，支持按 level、module、q 筛选和分页。",
        "新增 GET /v1/logs/{id} 单条日志查询接口。",
        "新增 React 日志查询页面，支持按级别、模块和关键词筛选，分页浏览。",
        "导航栏新增「日志」入口。"
    ]'::jsonb
) ON CONFLICT (version) DO UPDATE SET
    released_at = EXCLUDED.released_at,
    title = EXCLUDED.title,
    description = EXCLUDED.description,
    changes = EXCLUDED.changes;