mysql_replication/config/
mod.rs1use crate::{Error, Result};
2use serde::Deserialize;
3use std::{
4 collections::HashSet,
5 path::{Path, PathBuf},
6};
7
8#[derive(Debug, Deserialize)]
10#[serde(deny_unknown_fields)]
11pub struct Config {
12 pub source: SourceConfig,
13 pub state_file: PathBuf,
14 #[serde(default = "transaction_budget")]
15 pub max_transaction_bytes: usize,
16 pub tables: Vec<WhitelistTable>,
17}
18#[derive(Debug, Deserialize)]
19#[serde(deny_unknown_fields)]
20pub struct SourceConfig {
21 pub url_env: String,
22 pub server_id: u32,
23 pub tls_ca: Option<PathBuf>,
24 #[serde(default = "mysql_plugin")]
25 pub plugin: String,
26 pub adapter: Option<String>,
27}
28#[derive(Debug, Deserialize)]
29#[serde(deny_unknown_fields)]
30pub struct WhitelistTable {
31 pub schema: String,
32 pub table: String,
33}
34fn mysql_plugin() -> String {
35 "mysql".into()
36}
37fn transaction_budget() -> usize {
38 64 * 1024 * 1024
39}
40
41impl Config {
42 pub fn validate(&self) -> Result<()> {
43 let value = self;
44 if value.source.server_id == 0
45 || value.max_transaction_bytes == 0
46 || value.tables.is_empty()
47 {
48 return Err(Error::Schema(
49 "server_id, transaction budget and whitelist must be nonempty/nonzero".into(),
50 ));
51 }
52 let mut tables = HashSet::new();
53 for table in &value.tables {
54 if table.schema.is_empty()
55 || table.table.is_empty()
56 || !tables.insert((&table.schema, &table.table))
57 {
58 return Err(Error::Schema("empty/duplicate whitelist entry".into()));
59 }
60 }
61 if value.source.plugin.is_empty()
62 || value.source.url_env.is_empty()
63 || value.source.adapter.as_ref().is_some_and(String::is_empty)
64 {
65 return Err(Error::Schema("empty plugin, url_env or adapter".into()));
66 }
67 Ok(())
68 }
69 fn resolve(&mut self, path: &Path) -> Result<()> {
70 self.validate()?;
71 let value = self;
72 let base = path.parent().unwrap_or_else(|| Path::new("."));
73 if value.state_file.is_relative() {
74 value.state_file = base.join(&value.state_file);
75 }
76 if let Some(ca) = &mut value.source.tls_ca
77 && ca.is_relative()
78 {
79 *ca = base.join(&*ca);
80 }
81 Ok(())
82 }
83}
84
85#[derive(Debug, Deserialize)]
87#[serde(deny_unknown_fields)]
88pub struct SourceEntry {
89 pub id: String,
90 #[serde(flatten)]
91 pub config: Config,
92}
93#[derive(Debug, Deserialize)]
94#[serde(deny_unknown_fields)]
95pub struct AppConfig {
96 pub sources: Vec<SourceEntry>,
97}
98impl AppConfig {
99 pub fn read(path: &Path) -> Result<Self> {
100 let text = std::fs::read_to_string(path)?;
101 let mut app: Self = toml::from_str(&text)?;
102 for entry in &mut app.sources {
103 entry.config.resolve(path)?;
104 }
105 app.validate()?;
106 Ok(app)
107 }
108 pub fn validate(&self) -> Result<()> {
109 if self.sources.is_empty() {
110 return Err(Error::Schema("no sources configured".into()));
111 }
112 let mut ids = HashSet::new();
113 let mut states = HashSet::new();
114 for entry in &self.sources {
115 entry.config.validate()?;
116 if entry.id.is_empty() || !ids.insert(&entry.id) {
117 return Err(Error::Schema("empty/duplicate source id".into()));
118 }
119 let path = &entry.config.state_file;
121 let key = if path.exists() {
122 std::fs::canonicalize(path)?
123 } else {
124 let parent = path
125 .parent()
126 .filter(|p| !p.as_os_str().is_empty())
127 .unwrap_or_else(|| Path::new("."));
128 std::fs::canonicalize(parent)?.join(
129 path.file_name()
130 .ok_or_else(|| Error::Schema("state_file requires filename".into()))?,
131 )
132 };
133 if !states.insert(key) {
134 return Err(Error::Schema(
135 "sources must use different state files".into(),
136 ));
137 }
138 }
139 Ok(())
140 }
141}