binlog-inspect 0.6.0

Offline inspector for plaintext MySQL binary logs with JSON Lines, text and CSV output
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright (c) 2026 houseme
// SPDX-License-Identifier: Apache-2.0

//! Stable output model, independent of the underlying binlog library.
use serde::{Deserialize, Serialize};

/// Canonical event type; the single source of truth for event naming.
///
/// The JSON output renders the snake_case variant name, which matches the
/// historical string values byte for byte. This enum is `#[non_exhaustive]`
/// (R45): downstream matches require a wildcard arm, so new event types can
/// be added without a semver-major release.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "cli", value(rename_all = "snake_case"))]
#[non_exhaustive]
pub enum EventType {
    /// Layout description for the rest of the binlog file.
    FormatDescription,
    /// Table identifier and its schema.
    TableMap,
    /// Inserted row images (V1 codes 23 and V2 code 30).
    WriteRows,
    /// Updated row image pairs (V1 code 24 and V2 code 31).
    UpdateRows,
    /// Deleted row images (V1 code 25 and V2 code 32).
    DeleteRows,
    /// Partial update row events: only the before images are full row
    /// images; the after image's JSON diffs are applied against the before
    /// values, and unappliable diffs render as the loss-marking
    /// `partial_json` value.
    PartialUpdateRows,
    /// Statement event.
    Query,
    /// Transaction commit identifier.
    Xid,
    /// Global transaction identifier (tagged or untagged).
    Gtid,
    /// Global transaction identifier of an anonymous transaction.
    AnonymousGtid,
    /// GTID set committed before the current binlog file.
    PreviousGtids,
    /// Log rotation.
    Rotate,
    /// Server shutdown marker.
    Stop,
    /// Original statement for row events.
    RowsQuery,
    /// Replication incident report.
    Incident,
    /// Statement-time integer variable assignment.
    Intvar,
    /// Random seed used by statement-based replication.
    Rand,
    /// User-defined variable assignment.
    UserVar,
    /// Bulk load data carried outside the statement stream.
    BeginLoadQuery,
    /// Bulk load execution marker.
    ExecuteLoadQuery,
    /// Replication heartbeat.
    Heartbeat,
    /// Event that may be safely ignored by readers that do not handle it.
    Ignorable,
    /// Two-phase commit transaction context.
    TransactionContext,
    /// Group replication view change.
    ViewChange,
    /// XA transaction prepare.
    XaPrepare,
    /// Compressed transaction payload (WL#3549) enclosing a whole transaction.
    TransactionPayload,
    /// Event type that this version does not name.
    Unknown,
}

impl EventType {
    /// The snake_case name used in JSON output; identical to the serde
    /// rendering of this variant.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::FormatDescription => "format_description",
            Self::TableMap => "table_map",
            Self::WriteRows => "write_rows",
            Self::UpdateRows => "update_rows",
            Self::DeleteRows => "delete_rows",
            Self::PartialUpdateRows => "partial_update_rows",
            Self::Query => "query",
            Self::Xid => "xid",
            Self::Gtid => "gtid",
            Self::AnonymousGtid => "anonymous_gtid",
            Self::PreviousGtids => "previous_gtids",
            Self::Rotate => "rotate",
            Self::Stop => "stop",
            Self::RowsQuery => "rows_query",
            Self::Incident => "incident",
            Self::Intvar => "intvar",
            Self::Rand => "rand",
            Self::UserVar => "user_var",
            Self::BeginLoadQuery => "begin_load_query",
            Self::ExecuteLoadQuery => "execute_load_query",
            Self::Heartbeat => "heartbeat",
            Self::Ignorable => "ignorable",
            Self::TransactionContext => "transaction_context",
            Self::ViewChange => "view_change",
            Self::XaPrepare => "xa_prepare",
            Self::TransactionPayload => "transaction_payload",
            Self::Unknown => "unknown",
        }
    }

    /// Maps a raw binlog event type number to the canonical event type.
    ///
    /// V1 row events (codes 23-25, MySQL 5.5 era) map onto the same variants
    /// as their V2 counterparts (codes 30-32). The pre-GA row events (codes
    /// 20-22) and MariaDB-only codes (160+) have no stable cross-server
    /// meaning and are reported as [`EventType::Unknown`].
    pub fn from_raw(code: u8) -> Self {
        match code {
            2 => Self::Query,
            3 => Self::Stop,
            4 => Self::Rotate,
            5 => Self::Intvar,
            13 => Self::Rand,
            14 => Self::UserVar,
            15 => Self::FormatDescription,
            16 => Self::Xid,
            17 => Self::BeginLoadQuery,
            18 => Self::ExecuteLoadQuery,
            19 => Self::TableMap,
            23 => Self::WriteRows,
            24 => Self::UpdateRows,
            25 => Self::DeleteRows,
            26 => Self::Incident,
            27 | 41 => Self::Heartbeat,
            28 => Self::Ignorable,
            29 => Self::RowsQuery,
            30 => Self::WriteRows,
            31 => Self::UpdateRows,
            32 => Self::DeleteRows,
            33 | 42 => Self::Gtid,
            34 => Self::AnonymousGtid,
            35 => Self::PreviousGtids,
            36 => Self::TransactionContext,
            37 => Self::ViewChange,
            38 => Self::XaPrepare,
            39 => Self::PartialUpdateRows,
            40 => Self::TransactionPayload,
            _ => Self::Unknown,
        }
    }
}

impl std::fmt::Display for EventType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// An event and its byte offset in the input file.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ParsedEvent {
    /// File offset of the event header.
    pub offset: u64,
    /// Unix timestamp in seconds.
    pub timestamp: u32,
    /// Canonical event type.
    pub event_type: EventType,
    /// Raw MySQL event type number.
    pub event_type_code: u8,
    /// Originating server ID.
    pub server_id: u32,
    /// Event size including header.
    pub event_size: u32,
    /// Position of the next event recorded in the header.
    pub log_pos: u32,
    /// Decoded event content.
    pub payload: EventPayload,
}

/// Event-specific content.
///
/// This enum is `#[non_exhaustive]` (R45): downstream matches require a
/// wildcard arm, so new payload kinds can be added without a semver-major
/// release.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[non_exhaustive]
pub enum EventPayload {
    /// Format description.
    FormatDescription {
        /// Server version with NUL padding removed.
        server_version: String,
    },
    /// Table identifier and its schema.
    TableMap {
        /// Table identifier used by following row events.
        table_id: u64,
        /// Database containing the table.
        database: String,
        /// Table name.
        table: String,
        /// Number of columns in the table.
        column_count: u64,
    },
    /// Row images with typed column values.
    Rows {
        /// Table identifier from the preceding table map event.
        table_id: u64,
        /// Database containing the table.
        database: String,
        /// Table name.
        table: String,
        /// One entry per affected row.
        changes: Vec<RowChange>,
    },
    /// Partial update row event (WL#2955).
    ///
    /// The before image is a full row image. The after image is a sequence of
    /// JSON diffs per JSON column, which is applied against the before value:
    /// an applied column renders as [`CellValue::Json`] with the full new
    /// document, and a diff that cannot be applied renders as
    /// [`CellValue::PartialJson`] with the preserved diffs and the reason.
    PartialUpdateRows {
        /// Table identifier from the preceding table map event.
        table_id: u64,
        /// Database containing the table.
        database: String,
        /// Table name.
        table: String,
        /// Full before image per affected row; `None` when the image is absent.
        before: Vec<Option<RowImage>>,
        /// After image per affected row with diffs applied; `None` when the
        /// image is absent.
        after: Vec<Option<RowImage>>,
    },
    /// A row event whose table map was not available.
    Unresolved {
        /// Table identifier that had no table map event.
        table_id: u64,
    },
    /// Statement event.
    Query {
        /// Default database of the statement.
        database: String,
        /// Statement text.
        sql: String,
    },
    /// Transaction commit identifier.
    Xid {
        /// Transaction ID.
        xid: u64,
    },
    /// Global transaction identifier.
    Gtid {
        /// Canonical GTID: `uuid:gno` when untagged, `uuid:tag:gno` when
        /// tagged; `ANONYMOUS` for anonymous transactions.
        gtid: String,
        /// GTID tag (MySQL 8.4+), if present.
        tag: Option<String>,
    },
    /// GTID set committed before the current binlog file, rendered in the
    /// mysqlbinlog textual form (`uuid[:tag]:interval[,...]`, sids separated
    /// by commas).
    PreviousGtids {
        /// Rendered GTID set.
        gtid_set: String,
    },
    /// Log rotation.
    Rotate {
        /// Name of the next binlog file.
        next_file: String,
        /// Start position in the next file.
        position: u64,
    },
    /// Server shutdown marker.
    Stop,
    /// Original statement for row events.
    RowsQuery {
        /// Statement text.
        sql: String,
    },
    /// Replication incident report.
    Incident {
        /// Canonical incident type name, or `UNKNOWN(n)` for unrecognized codes.
        incident_type: String,
        /// Human-readable incident description.
        message: String,
    },
    /// Statement-time integer variable assignment.
    Intvar {
        /// Variable kind: the pinned canonical MySQL constant name
        /// (`INVALID_INT_EVENT`, `LAST_INSERT_ID_EVENT`, `INSERT_ID_EVENT`),
        /// independent of the underlying library's Debug rendering.
        var_type: String,
        /// Assigned value.
        value: u64,
    },
    /// Unhandled event, retained with its type and size.
    Unknown {
        /// Length of the retained event data.
        raw_len: usize,
    },
    /// A compressed transaction payload (code 40, WL#3549) and its decoded
    /// inner events.
    ///
    /// The compression algorithm byte is validated before the underlying
    /// library parses the header, the payload is only ever streamed through a
    /// counting decompression reader bounded by the declared size, and inner
    /// events share the parser's table map state with the outer stream.
    TransactionPayload {
        /// Canonical compression name: `"NONE"` or `"ZSTD"`.
        compression: String,
        /// Declared uncompressed size from the payload header.
        uncompressed_size: u64,
        /// Inner events in stream order; the `offset` of each inner event is
        /// its byte position inside the decompressed payload, while
        /// `timestamp` and `server_id` are inherited from the enclosing event.
        events: Vec<ParsedEvent>,
    },
}

/// Before and after images for a single row.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct RowChange {
    /// Previous image, if present.
    pub before: Option<RowImage>,
    /// New image, if present.
    pub after: Option<RowImage>,
}

/// A row image in table-column order, including columns omitted by minimal row images.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct RowImage {
    /// Cells are in zero-based table-column order.
    pub columns: Vec<ColumnValue>,
}

/// One table column in a row image.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ColumnValue {
    /// Zero-based position in the table.
    pub index: usize,
    /// Column name when present in binlog metadata; otherwise a generated name.
    pub name: Option<String>,
    /// MySQL column type, if this column is present in the row image.
    pub mysql_type: Option<String>,
    /// Signedness metadata for numeric columns; unknown if the binlog omitted it.
    pub signedness: Option<Signedness>,
    /// Value, including explicit absent and SQL NULL states.
    pub value: CellValue,
}

/// Loss-aware MySQL value representation.
///
/// When deserializing, [`CellValue::Bytes`] expects the lowercase hexadecimal
/// encoding produced by serialization; no character decoding is performed.
/// This enum is `#[non_exhaustive]` (R45): downstream matches require a
/// wildcard arm, so new value kinds can be added without a semver-major
/// release.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
#[non_exhaustive]
pub enum CellValue {
    /// Column omitted from this image.
    Absent,
    /// SQL NULL.
    Null,
    /// Bytes encoded as lowercase hexadecimal; no character encoding is assumed.
    Bytes(String),
    /// Signed integer.
    Int(i64),
    /// Unsigned integer.
    UInt(u64),
    /// Single-precision value, rendered as a string to preserve non-finite values.
    Float(String),
    /// Double-precision value, rendered as a string to preserve non-finite values.
    Double(String),
    /// Date or datetime components, including zero dates.
    Date {
        /// Gregorian year.
        year: u16,
        /// Month of the year (1-12).
        month: u8,
        /// Day of the month (1-31).
        day: u8,
        /// Hour of the day (0-23).
        hour: u8,
        /// Minute of the hour (0-59).
        minute: u8,
        /// Second of the minute (0-59).
        second: u8,
        /// Microsecond component.
        micros: u32,
    },
    /// Signed MySQL TIME components, including values greater than 24 hours.
    Time {
        /// Whether the value is negative.
        negative: bool,
        /// Whole days beyond the hour component.
        days: u32,
        /// Remaining hours (0-23).
        hours: u8,
        /// Remaining minutes (0-59).
        minutes: u8,
        /// Remaining seconds (0-59).
        seconds: u8,
        /// Microsecond component.
        micros: u32,
    },
    /// Decoded full JSON value.
    Json(serde_json::Value),
    /// JSON column whose after image is a sequence of binlog JSON diffs that
    /// could not be applied to a before value; the raw diffs are preserved.
    ///
    /// A successful diff application renders as [`CellValue::Json`]; this
    /// variant records the loss and why it happened.
    PartialJson {
        /// The recorded diffs in stream order.
        diffs: Vec<JsonDiffRecord>,
        /// Why the diffs could not be applied to the before value.
        reason: String,
    },
}

/// One recorded JSON diff of a partial JSON update (WL#2955).
///
/// Binlog JSON diffs describe a logical change to a JSON document; the `path`
/// uses the MySQL JSON path text form (for example `$.a[2]`).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct JsonDiffRecord {
    /// MySQL JSON path of the change.
    pub path: String,
    /// Operation name: `"replace"`, `"insert"`, or `"remove"`.
    pub operation: String,
    /// New value carried by replace and insert operations; remove carries none.
    pub value: Option<serde_json::Value>,
}

/// Signedness carried by the table map, or unknown when metadata was omitted.
///
/// This enum is `#[non_exhaustive]` (R45): downstream matches require a
/// wildcard arm, so new states can be added without a semver-major release.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Signedness {
    /// A signed numeric column.
    Signed,
    /// An unsigned numeric column.
    Unsigned,
    /// Decoded using the library's signed default; schema is required to interpret it.
    Unknown,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn as_str_matches_serde_snake_case() {
        let codes = [
            0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
            24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 100,
            160, 255,
        ];
        for code in codes {
            let event_type = EventType::from_raw(code);
            let json = serde_json::to_string(&event_type).unwrap();
            assert_eq!(
                json.trim_matches('"'),
                event_type.as_str(),
                "serde and as_str disagree for code {code}"
            );
        }
    }

    #[test]
    fn raw_code_table_matches_contract() {
        let expected = [
            (2u8, "query"),
            (3, "stop"),
            (4, "rotate"),
            (5, "intvar"),
            (13, "rand"),
            (14, "user_var"),
            (15, "format_description"),
            (16, "xid"),
            (17, "begin_load_query"),
            (18, "execute_load_query"),
            (19, "table_map"),
            (23, "write_rows"),
            (24, "update_rows"),
            (25, "delete_rows"),
            (26, "incident"),
            (27, "heartbeat"),
            (28, "ignorable"),
            (29, "rows_query"),
            (30, "write_rows"),
            (31, "update_rows"),
            (32, "delete_rows"),
            (33, "gtid"),
            (34, "anonymous_gtid"),
            (35, "previous_gtids"),
            (36, "transaction_context"),
            (37, "view_change"),
            (38, "xa_prepare"),
            (39, "partial_update_rows"),
            (40, "transaction_payload"),
            (41, "heartbeat"),
            (42, "gtid"),
        ];
        for (code, name) in expected {
            assert_eq!(EventType::from_raw(code).as_str(), name, "code {code}");
        }
        for code in [0u8, 1, 6, 7, 8, 9, 10, 11, 12, 20, 21, 22, 43, 160, 255] {
            assert_eq!(EventType::from_raw(code), EventType::Unknown, "code {code}");
        }
    }
}