diff --git a/src/main.rs b/src/main.rs index 9177ef6..6564382 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,9 @@ struct Args { /// path to the config file #[arg(long)] config: String, + /// Dry run mode + #[arg(long)] + dry_run: bool, } fn main() { @@ -61,7 +64,7 @@ fn main() { } }; - let mut proxysql = ProxySQL::new(&config); + let mut proxysql = ProxySQL::new(&config, args.dry_run); let running_mode = match config.operation_mode { Some(mode) => mode, diff --git a/src/proxysql.rs b/src/proxysql.rs index 484507e..51b9ca3 100644 --- a/src/proxysql.rs +++ b/src/proxysql.rs @@ -15,6 +15,7 @@ pub struct ProxySQL { warmup_time_s: u16, conn: mysql::Conn, hosts: Vec, + dry_run: bool, } impl ProxySQL { @@ -27,7 +28,7 @@ impl ProxySQL { /// # Returns /// /// A new ProxySQL struct. - pub fn new(config: &config::Config) -> Self { + pub fn new(config: &config::Config, dry_run: bool) -> Self { let mut conn = Conn::new( OptsBuilder::new() .ip_or_hostname(Some(config.proxysql_host.as_str())) @@ -59,9 +60,20 @@ impl ProxySQL { readyset_hostgroup: config.readyset_hostgroup, warmup_time_s: config.warmup_time_s.unwrap_or(0), hosts, + dry_run, } } + /// This function is used to get the dry_run field. + /// This field is used to indicate if the ProxySQL operations should be executed or not. + /// + /// # Returns + /// + /// A boolean indicating if the ProxySQL operations should be executed or not. + pub fn dry_run(&self) -> bool { + self.dry_run + } + /// This function is used to add a query rule to ProxySQL. /// /// # Arguments @@ -199,6 +211,10 @@ impl ProxySQL { .as_str(), ); host.change_status(status); + if self.dry_run { + messages::print_info("Dry run, skipping changes to ProxySQL"); + continue; + } let _ = self.conn.query_drop(format!( "UPDATE mysql_servers SET status = '{}' {}", host.get_status(), diff --git a/src/queries.rs b/src/queries.rs index 6af18c5..adef61f 100644 --- a/src/queries.rs +++ b/src/queries.rs @@ -193,13 +193,17 @@ impl QueryDiscovery { .as_str(), ); queries_added_or_change = true; - proxysql.get_online_hosts().iter_mut().for_each(|host| { - host.cache_query(query) - .expect("Failed to create readyset cache"); - }); - proxysql - .add_as_query_rule(query) - .expect("Failed to add query rule"); + if !proxysql.dry_run() { + proxysql.get_online_hosts().iter_mut().for_each(|host| { + host.cache_query(query) + .expect("Failed to create readyset cache"); + }); + proxysql + .add_as_query_rule(query) + .expect("Failed to add query rule"); + } else { + messages::print_info("Dry run, not adding query"); + } current_queries_digest.push(query.get_digest().to_string()); } Ok(false) => {