CREATE TABLE IF NOT EXISTS trace_contexts
(
    trace_id    CHAR(32) PRIMARY KEY,
    parent_id   CHAR(32) NULL,
    alias       VARCHAR(255) NOT NULL,
    type        VARCHAR(64) NULL,
    event_time  DATETIME(6) NOT NULL,
    attrs_json  JSON NOT NULL,
    INDEX idx_trace_contexts_parent_id (parent_id),
    INDEX idx_trace_contexts_event_time (event_time)
);

CREATE TABLE IF NOT EXISTS trace_messages
(
    id          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    trace_id    CHAR(32) NOT NULL,
    event_time  DATETIME(6) NOT NULL,
    step        VARCHAR(128) NOT NULL DEFAULT '',
    status      VARCHAR(64) NOT NULL DEFAULT '',
    level       VARCHAR(16) NOT NULL DEFAULT 'INFO',
    message     TEXT NOT NULL,
    attrs_json  JSON NOT NULL,
    INDEX idx_trace_messages_trace_id (trace_id),
    INDEX idx_trace_messages_event_time (event_time),
    INDEX idx_trace_messages_step (step),
    INDEX idx_trace_messages_status (status),
    CONSTRAINT fk_trace_messages_context
        FOREIGN KEY (trace_id) REFERENCES trace_contexts(trace_id)
        ON DELETE CASCADE
);
