Skip to content

Commit

Permalink
Modify Config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
kip2 committed Mar 23, 2024
1 parent d52ba02 commit 73df58d
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use std::{env, error::Error, fs};
#[derive(Debug, Parser)]
#[command(author, version, about)]
pub struct Config {
#[arg(help = "Input file path")]
file: String,
#[arg(help = "Target database name")]
db_name: String,
#[arg(short = 'b', long = "backup", help = "Backup execution")]
backup: bool,
}

type MyResult<T> = Result<T, Box<dyn Error>>;
Expand All @@ -31,3 +33,29 @@ pub async fn run(config: Config) -> MyResult<()> {

Ok(())
}

async fn execute_query(db: &Pool<MySql>, queries: Vec<String>) {
// Gererate transaction
let mut tx = db.begin().await.expect("transaction error.");

for query in queries {
// Execute SQL query
let result = sqlx::query(&query).execute(&mut *tx).await;

match result {
Ok(_) => {}
Err(e) => {
println!("Database query failed: {}", e);
println!("Failed query: {:?}", &query);
// rollback
tx.rollback().await.expect("Transaction rollback error.");
return;
}
}
}

// transaction commit
let _ = tx.commit().await.unwrap_or_else(|e| {
println!("{:?}", e);
});
}

0 comments on commit 73df58d

Please sign in to comment.