Skip to content

Commit 3fdcbf9

Browse files
Merge pull request #29 from readysettech/issue-28-dry-run
Add dry-run option to the CLI
2 parents bbecb53 + 84982ea commit 3fdcbf9

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ struct Args {
1919
/// path to the config file
2020
#[arg(long)]
2121
config: String,
22+
/// Dry run mode
23+
#[arg(long)]
24+
dry_run: bool,
2225
}
2326

2427
fn main() {
@@ -61,7 +64,7 @@ fn main() {
6164
}
6265
};
6366

64-
let mut proxysql = ProxySQL::new(&config);
67+
let mut proxysql = ProxySQL::new(&config, args.dry_run);
6568

6669
let running_mode = match config.operation_mode {
6770
Some(mode) => mode,

src/proxysql.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct ProxySQL {
1515
warmup_time_s: u16,
1616
conn: mysql::Conn,
1717
hosts: Vec<Host>,
18+
dry_run: bool,
1819
}
1920

2021
impl ProxySQL {
@@ -27,7 +28,7 @@ impl ProxySQL {
2728
/// # Returns
2829
///
2930
/// A new ProxySQL struct.
30-
pub fn new(config: &config::Config) -> Self {
31+
pub fn new(config: &config::Config, dry_run: bool) -> Self {
3132
let mut conn = Conn::new(
3233
OptsBuilder::new()
3334
.ip_or_hostname(Some(config.proxysql_host.as_str()))
@@ -59,9 +60,20 @@ impl ProxySQL {
5960
readyset_hostgroup: config.readyset_hostgroup,
6061
warmup_time_s: config.warmup_time_s.unwrap_or(0),
6162
hosts,
63+
dry_run,
6264
}
6365
}
6466

67+
/// This function is used to get the dry_run field.
68+
/// This field is used to indicate if the ProxySQL operations should be executed or not.
69+
///
70+
/// # Returns
71+
///
72+
/// A boolean indicating if the ProxySQL operations should be executed or not.
73+
pub fn dry_run(&self) -> bool {
74+
self.dry_run
75+
}
76+
6577
/// This function is used to add a query rule to ProxySQL.
6678
///
6779
/// # Arguments
@@ -199,6 +211,10 @@ impl ProxySQL {
199211
.as_str(),
200212
);
201213
host.change_status(status);
214+
if self.dry_run {
215+
messages::print_info("Dry run, skipping changes to ProxySQL");
216+
continue;
217+
}
202218
let _ = self.conn.query_drop(format!(
203219
"UPDATE mysql_servers SET status = '{}' {}",
204220
host.get_status(),

src/queries.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,17 @@ impl QueryDiscovery {
193193
.as_str(),
194194
);
195195
queries_added_or_change = true;
196-
proxysql.get_online_hosts().iter_mut().for_each(|host| {
197-
host.cache_query(query)
198-
.expect("Failed to create readyset cache");
199-
});
200-
proxysql
201-
.add_as_query_rule(query)
202-
.expect("Failed to add query rule");
196+
if !proxysql.dry_run() {
197+
proxysql.get_online_hosts().iter_mut().for_each(|host| {
198+
host.cache_query(query)
199+
.expect("Failed to create readyset cache");
200+
});
201+
proxysql
202+
.add_as_query_rule(query)
203+
.expect("Failed to add query rule");
204+
} else {
205+
messages::print_info("Dry run, not adding query");
206+
}
203207
current_queries_digest.push(query.get_digest().to_string());
204208
}
205209
Ok(false) => {

0 commit comments

Comments
 (0)