Wrap all operations in database transactions
This commit is contained in:
parent
cb83942cc8
commit
d1c42dac8b
@ -4,7 +4,7 @@ use dialoguer::Editor;
|
||||
use indoc::indoc;
|
||||
use itertools::Itertools;
|
||||
use prettytable::{Cell, Row, Table};
|
||||
use sqlx::MySqlConnection;
|
||||
use sqlx::{Connection, MySqlConnection};
|
||||
|
||||
use crate::core::{
|
||||
self,
|
||||
@ -148,13 +148,19 @@ pub async fn handle_command(
|
||||
command: DatabaseCommand,
|
||||
mut conn: MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
let result = match command {
|
||||
DatabaseCommand::CreateDb(args) => create_databases(args, &mut conn).await,
|
||||
DatabaseCommand::DropDb(args) => drop_databases(args, &mut conn).await,
|
||||
DatabaseCommand::ListDb(args) => list_databases(args, &mut conn).await,
|
||||
DatabaseCommand::ShowDbPerm(args) => show_databases(args, &mut conn).await,
|
||||
DatabaseCommand::EditDbPerm(args) => edit_permissions(args, &mut conn).await,
|
||||
};
|
||||
let result = conn
|
||||
.transaction(|mut txn| {
|
||||
Box::pin(async move {
|
||||
match command {
|
||||
DatabaseCommand::CreateDb(args) => create_databases(args, &mut txn).await,
|
||||
DatabaseCommand::DropDb(args) => drop_databases(args, &mut txn).await,
|
||||
DatabaseCommand::ListDb(args) => list_databases(args, &mut txn).await,
|
||||
DatabaseCommand::ShowDbPerm(args) => show_databases(args, &mut txn).await,
|
||||
DatabaseCommand::EditDbPerm(args) => edit_permissions(args, &mut txn).await,
|
||||
}
|
||||
})
|
||||
})
|
||||
.await;
|
||||
|
||||
close_database_connection(conn).await;
|
||||
|
||||
|
@ -3,7 +3,7 @@ use std::vec;
|
||||
use anyhow::Context;
|
||||
use clap::Parser;
|
||||
use dialoguer::{Confirm, Password};
|
||||
use sqlx::MySqlConnection;
|
||||
use sqlx::{Connection, MySqlConnection};
|
||||
|
||||
use crate::core::{common::close_database_connection, user_operations::validate_user_name};
|
||||
|
||||
@ -65,12 +65,18 @@ pub struct UserShowArgs {
|
||||
}
|
||||
|
||||
pub async fn handle_command(command: UserCommand, mut conn: MySqlConnection) -> anyhow::Result<()> {
|
||||
let result = match command {
|
||||
UserCommand::CreateUser(args) => create_users(args, &mut conn).await,
|
||||
UserCommand::DropUser(args) => drop_users(args, &mut conn).await,
|
||||
UserCommand::PasswdUser(args) => change_password_for_user(args, &mut conn).await,
|
||||
UserCommand::ShowUser(args) => show_users(args, &mut conn).await,
|
||||
};
|
||||
let result = conn
|
||||
.transaction(|mut txn| {
|
||||
Box::pin(async move {
|
||||
match command {
|
||||
UserCommand::CreateUser(args) => create_users(args, &mut txn).await,
|
||||
UserCommand::DropUser(args) => drop_users(args, &mut txn).await,
|
||||
UserCommand::PasswdUser(args) => change_password_for_user(args, &mut txn).await,
|
||||
UserCommand::ShowUser(args) => show_users(args, &mut txn).await,
|
||||
}
|
||||
})
|
||||
})
|
||||
.await;
|
||||
|
||||
close_database_connection(conn).await;
|
||||
|
||||
|
@ -67,8 +67,15 @@ async fn main() -> anyhow::Result<()> {
|
||||
let config = core::config::get_config(args.config_overrides)?;
|
||||
let connection = core::config::mysql_connection_from_config(config).await?;
|
||||
|
||||
match args.command {
|
||||
let result = match args.command {
|
||||
Command::Db(command) => cli::database_command::handle_command(command, connection).await,
|
||||
Command::User(user_args) => cli::user_command::handle_command(user_args, connection).await,
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(_) => println!("Changes committed to database"),
|
||||
Err(_) => println!("Changes reverted due to error"),
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user