Wrap all operations in database transactions

This commit is contained in:
Oystein Kristoffer Tveit 2024-08-07 16:54:26 +02:00
parent cb83942cc8
commit d1c42dac8b
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
3 changed files with 35 additions and 16 deletions

View File

@ -4,7 +4,7 @@ use dialoguer::Editor;
use indoc::indoc; use indoc::indoc;
use itertools::Itertools; use itertools::Itertools;
use prettytable::{Cell, Row, Table}; use prettytable::{Cell, Row, Table};
use sqlx::MySqlConnection; use sqlx::{Connection, MySqlConnection};
use crate::core::{ use crate::core::{
self, self,
@ -148,13 +148,19 @@ pub async fn handle_command(
command: DatabaseCommand, command: DatabaseCommand,
mut conn: MySqlConnection, mut conn: MySqlConnection,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let result = match command { let result = conn
DatabaseCommand::CreateDb(args) => create_databases(args, &mut conn).await, .transaction(|mut txn| {
DatabaseCommand::DropDb(args) => drop_databases(args, &mut conn).await, Box::pin(async move {
DatabaseCommand::ListDb(args) => list_databases(args, &mut conn).await, match command {
DatabaseCommand::ShowDbPerm(args) => show_databases(args, &mut conn).await, DatabaseCommand::CreateDb(args) => create_databases(args, &mut txn).await,
DatabaseCommand::EditDbPerm(args) => edit_permissions(args, &mut conn).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; close_database_connection(conn).await;

View File

@ -3,7 +3,7 @@ use std::vec;
use anyhow::Context; use anyhow::Context;
use clap::Parser; use clap::Parser;
use dialoguer::{Confirm, Password}; use dialoguer::{Confirm, Password};
use sqlx::MySqlConnection; use sqlx::{Connection, MySqlConnection};
use crate::core::{common::close_database_connection, user_operations::validate_user_name}; 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<()> { pub async fn handle_command(command: UserCommand, mut conn: MySqlConnection) -> anyhow::Result<()> {
let result = match command { let result = conn
UserCommand::CreateUser(args) => create_users(args, &mut conn).await, .transaction(|mut txn| {
UserCommand::DropUser(args) => drop_users(args, &mut conn).await, Box::pin(async move {
UserCommand::PasswdUser(args) => change_password_for_user(args, &mut conn).await, match command {
UserCommand::ShowUser(args) => show_users(args, &mut conn).await, 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; close_database_connection(conn).await;

View File

@ -67,8 +67,15 @@ async fn main() -> anyhow::Result<()> {
let config = core::config::get_config(args.config_overrides)?; let config = core::config::get_config(args.config_overrides)?;
let connection = core::config::mysql_connection_from_config(config).await?; 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::Db(command) => cli::database_command::handle_command(command, connection).await,
Command::User(user_args) => cli::user_command::handle_command(user_args, 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
} }