Skip to main content

custom_version/
custom_version.rs

1//! Template only: replace series/version checks after validating the actual wire
2//! format. Registration does not certify compatibility with an untested server.
3use mysql_replication::{
4    Event, Replication, Result,
5    config::AppConfig,
6    engine::runtime::Runtime,
7    protocol::mysql::{
8        events::{EventParser, MysqlEventParser},
9        version::{FormatDescription, ServerVersion, VersionAdapter},
10    },
11    sink::{JsonLinesSink, TransactionSink},
12};
13use std::{io::BufWriter, path::Path, sync::Arc};
14struct CustomMysql;
15impl VersionAdapter for CustomMysql {
16    fn name(&self) -> &str {
17        "custom-mysql"
18    }
19    fn supports(&self, version: &ServerVersion) -> bool {
20        version.major == 9
21            && version.minor == 1
22            && !version.raw.to_ascii_lowercase().contains("mariadb")
23    }
24    fn status_query(&self) -> &str {
25        "SHOW BINARY LOG STATUS"
26    }
27    fn create_event_parser(&self, format: &FormatDescription<'_>) -> Result<Box<dyn EventParser>> {
28        // Reuse only after verifying that field order/endianness are unchanged.
29        // For another event structure, return your own EventParser here. It
30        // should return borrowed TableMapEvent/RowsEvent/Control views, without
31        // rebuilding a canonical byte buffer. See tests/integration/event_adapters.rs.
32        Ok(Box::new(MysqlEventParser::from_format(format)?))
33    }
34    // Override validate_server/validate_format for new capability/layout rules.
35    // Override create_decoder if the column metadata or row value encoding also
36    // changes. classify is the fallback for additional control event codes.
37}
38fn main() -> Result<()> {
39    let Some(path) = std::env::args().nth(1) else {
40        eprintln!("usage: cargo run -p mysql-replication --example custom_version -- CONFIG.toml");
41        return Ok(());
42    };
43    let mut runtime = Runtime::default();
44    runtime.versions.register(Arc::new(CustomMysql))?;
45    let mut replication = Replication::with_runtime(AppConfig::read(Path::new(&path))?, runtime)?;
46    let output = JsonLinesSink::new(BufWriter::new(std::io::stdout()));
47    replication.add_handle(move |event: Event<'_>| {
48        output.commit(event.source_id, event.checkpoint, event.changes)
49    });
50    replication.run()
51}