Skip to main content

maxwell_cdc/
schema.rs

1//! Schema-change payloads.
2
3use alloc::string::String;
4use alloc::vec::Vec;
5use serde::{Deserialize, Serialize};
6use serde_json::{Map, Value};
7
8/// Metadata common to DDL messages.
9#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
10pub struct DdlMetadata {
11    /// Event timestamp in milliseconds since epoch.
12    pub ts: i64,
13    /// The DDL SQL statement.
14    pub sql: String,
15    /// Binlog position (filename:offset).
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub position: Option<String>,
18    /// Global transaction ID.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub gtid: Option<String>,
21    /// Schema ID.
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub schema_id: Option<i64>,
24}
25
26/// A column definition in a table.
27#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
28pub struct ColumnDefinition {
29    /// Column name.
30    pub name: String,
31    /// Column data type.
32    #[serde(rename = "type")]
33    pub column_type: String,
34    /// Character set for text columns.
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub charset: Option<String>,
37    /// Whether the column is signed (for numeric types).
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub signed: Option<bool>,
40    /// Enum values for ENUM columns.
41    #[serde(rename = "enum-values", skip_serializing_if = "Option::is_none")]
42    pub enum_values: Option<Vec<String>>,
43    /// Fractional-second precision, on the types that carry one (`DATETIME`, `TIMESTAMP`,
44    /// `TIME`). An `i64` because Maxwell writes a Java `Long`, though real values are 0 to 6.
45    #[serde(rename = "column-length", skip_serializing_if = "Option::is_none")]
46    pub column_length: Option<i64>,
47}
48
49/// A table definition in a database.
50#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
51pub struct TableDefinition {
52    /// Database name.
53    pub database: String,
54    /// Table name.
55    pub table: String,
56    /// Table character set.
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub charset: Option<String>,
59    /// Primary key column names.
60    #[serde(rename = "primary-key")]
61    pub primary_key: Vec<String>,
62    /// Column definitions.
63    pub columns: Vec<ColumnDefinition>,
64}
65
66/// A database definition.
67#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
68pub struct DatabaseDefinition {
69    /// Database name.
70    pub database: String,
71    /// Database character set.
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub charset: Option<String>,
74}
75
76/// A table creation message.
77#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
78pub struct TableCreateChange {
79    /// Database name.
80    pub database: String,
81    /// Table name.
82    pub table: String,
83    /// New table definition.
84    #[serde(rename = "def")]
85    pub definition: TableDefinition,
86    /// DDL metadata.
87    #[serde(flatten)]
88    pub metadata: DdlMetadata,
89    /// Fields Maxwell emitted that this crate does not model, kept verbatim. Keys must not
90    /// collide with the named fields above, or serializing writes them twice.
91    #[serde(flatten)]
92    pub extra: Map<String, Value>,
93}
94
95/// A table alteration message.
96#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
97pub struct TableAlterChange {
98    /// Database name.
99    pub database: String,
100    /// Table name.
101    pub table: String,
102    /// Old table definition before alteration.
103    #[serde(rename = "old")]
104    pub old_definition: TableDefinition,
105    /// New table definition after alteration.
106    #[serde(rename = "def")]
107    pub definition: TableDefinition,
108    /// DDL metadata.
109    #[serde(flatten)]
110    pub metadata: DdlMetadata,
111    /// Fields Maxwell emitted that this crate does not model, kept verbatim. Keys must not
112    /// collide with the named fields above, or serializing writes them twice.
113    #[serde(flatten)]
114    pub extra: Map<String, Value>,
115}
116
117/// A table drop message.
118#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
119pub struct TableDropChange {
120    /// Database name.
121    pub database: String,
122    /// Table name.
123    pub table: String,
124    /// DDL metadata.
125    #[serde(flatten)]
126    pub metadata: DdlMetadata,
127    /// Fields Maxwell emitted that this crate does not model, kept verbatim. Keys must not
128    /// collide with the named fields above, or serializing writes them twice.
129    #[serde(flatten)]
130    pub extra: Map<String, Value>,
131}
132
133/// A database creation or alteration message.
134#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
135pub struct DatabaseChange {
136    /// Database definition.
137    #[serde(flatten)]
138    pub definition: DatabaseDefinition,
139    /// DDL metadata.
140    #[serde(flatten)]
141    pub metadata: DdlMetadata,
142    /// Fields Maxwell emitted that this crate does not model, kept verbatim. Keys must not
143    /// collide with the named fields above, or serializing writes them twice.
144    #[serde(flatten)]
145    pub extra: Map<String, Value>,
146}
147
148/// A database drop message.
149#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
150pub struct DatabaseDropChange {
151    /// Database name.
152    pub database: String,
153    /// DDL metadata.
154    #[serde(flatten)]
155    pub metadata: DdlMetadata,
156    /// Fields Maxwell emitted that this crate does not model, kept verbatim. Keys must not
157    /// collide with the named fields above, or serializing writes them twice.
158    #[serde(flatten)]
159    pub extra: Map<String, Value>,
160}