Skip to content

Commit

Permalink
Add dry-run option to the CLI
Browse files Browse the repository at this point in the history
This commit adds a new mode of operation to the CLI, the dry-run mode.
In this mode, the CLI will not execute any changes, but will instead
print them to the console.

Closes #28
  • Loading branch information
altmannmarcelo committed Nov 12, 2024
1 parent bbecb53 commit 84982ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 17 additions & 1 deletion src/proxysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct ProxySQL {
warmup_time_s: u16,
conn: mysql::Conn,
hosts: Vec<Host>,
dry_run: bool,
}

impl ProxySQL {
Expand All @@ -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()))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
18 changes: 11 additions & 7 deletions src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 84982ea

Please sign in to comment.