From b78651f81dd4a35ab64d35e8a641a0bdddde4b0a Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sat, 12 Jun 2021 00:32:00 +0300 Subject: [PATCH] refac: change transaction utils --- migra/src/clients/mod.rs | 21 +++++++++------------ migra/src/lib.rs | 2 +- migra_cli/src/commands/apply.rs | 13 +++++++------ migra_cli/src/commands/downgrade.rs | 13 +++++++------ migra_cli/src/commands/upgrade.rs | 13 +++++++------ 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/migra/src/clients/mod.rs b/migra/src/clients/mod.rs index 38137a6..a9474d2 100644 --- a/migra/src/clients/mod.rs +++ b/migra/src/clients/mod.rs @@ -19,7 +19,7 @@ where pub trait Client: ManageMigrations + ManageTransaction {} -pub type AnyClient = Box; +pub type AnyClient = Box<(dyn Client + 'static)>; #[cfg(feature = "postgres")] pub mod postgres; @@ -36,12 +36,9 @@ pub mod sqlite; #[cfg(feature = "sqlite")] pub use self::sqlite::Client as SqliteClient; -pub fn with_transaction( - client: &mut AnyClient, - trx_fn: &mut TrxFnMut, -) -> MigraResult +pub fn run_in_transaction(client: &mut AnyClient, trx_fn: TrxFnMut) -> MigraResult<()> where - TrxFnMut: FnMut(&mut AnyClient) -> MigraResult, + TrxFnMut: FnOnce(&mut AnyClient) -> MigraResult<()>, { client .begin_transaction() @@ -50,16 +47,16 @@ where .or_else(|err| client.rollback_transaction().and(Err(err))) } -pub fn maybe_with_transaction( - is_needed: bool, +pub fn should_run_in_transaction( client: &mut AnyClient, - trx_fn: &mut TrxFnMut, -) -> MigraResult + is_needed: bool, + trx_fn: TrxFnMut, +) -> MigraResult<()> where - TrxFnMut: FnMut(&mut AnyClient) -> MigraResult, + TrxFnMut: FnOnce(&mut AnyClient) -> MigraResult<()>, { if is_needed { - with_transaction(client, trx_fn) + run_in_transaction(client, trx_fn) } else { trx_fn(client) } diff --git a/migra/src/lib.rs b/migra/src/lib.rs index 94fc636..b7166bd 100644 --- a/migra/src/lib.rs +++ b/migra/src/lib.rs @@ -9,6 +9,6 @@ pub mod fs; pub mod managers; pub mod migration; -pub use clients::{maybe_with_transaction, with_transaction}; +pub use clients::{run_in_transaction, should_run_in_transaction}; pub use errors::{Error, MigraResult as Result, StdResult}; pub use migration::Migration; diff --git a/migra_cli/src/commands/apply.rs b/migra_cli/src/commands/apply.rs index 13604c5..7cbe398 100644 --- a/migra_cli/src/commands/apply.rs +++ b/migra_cli/src/commands/apply.rs @@ -1,6 +1,7 @@ use crate::app::App; use crate::database; use crate::opts::ApplyCommandOpt; +use migra::should_run_in_transaction; pub(crate) fn apply_sql(app: &App, cmd_opts: &ApplyCommandOpt) -> migra::StdResult<()> { let config = app.config()?; @@ -20,17 +21,17 @@ pub(crate) fn apply_sql(app: &App, cmd_opts: &ApplyCommandOpt) -> migra::StdResu .map(std::fs::read_to_string) .collect::, _>>()?; - migra::maybe_with_transaction( - cmd_opts.transaction_opts.single_transaction, + should_run_in_transaction( &mut client, - &mut |mut client| { + cmd_opts.transaction_opts.single_transaction, + |client| { file_contents .iter() .try_for_each(|content| { - migra::maybe_with_transaction( + should_run_in_transaction( + client, !cmd_opts.transaction_opts.single_transaction, - &mut client, - &mut |client| client.apply_sql(content), + |client| client.apply_sql(content), ) }) .map_err(From::from) diff --git a/migra_cli/src/commands/downgrade.rs b/migra_cli/src/commands/downgrade.rs index 9e1dbdc..c3f930e 100644 --- a/migra_cli/src/commands/downgrade.rs +++ b/migra_cli/src/commands/downgrade.rs @@ -1,6 +1,7 @@ use crate::app::App; use crate::database; use crate::opts::DowngradeCommandOpt; +use migra::should_run_in_transaction; use std::cmp; pub(crate) fn rollback_applied_migrations( @@ -32,19 +33,19 @@ pub(crate) fn rollback_applied_migrations( }) .collect::, _>>()?; - migra::maybe_with_transaction( - opts.transaction_opts.single_transaction, + should_run_in_transaction( &mut client, - &mut |mut client| { + opts.transaction_opts.single_transaction, + |client| { migrations_with_content .iter() .try_for_each(|(migration_name, content)| { if all_migrations.contains_name(migration_name) { println!("downgrade {}...", migration_name); - migra::maybe_with_transaction( + should_run_in_transaction( + client, !opts.transaction_opts.single_transaction, - &mut client, - &mut |client| client.run_downgrade_migration(migration_name, &content), + |client| client.run_downgrade_migration(migration_name, &content), ) } else { Ok(()) diff --git a/migra_cli/src/commands/upgrade.rs b/migra_cli/src/commands/upgrade.rs index 11b9106..6775131 100644 --- a/migra_cli/src/commands/upgrade.rs +++ b/migra_cli/src/commands/upgrade.rs @@ -2,6 +2,7 @@ use crate::app::App; use crate::database; use crate::opts::UpgradeCommandOpt; use migra::migration; +use migra::should_run_in_transaction; pub(crate) fn upgrade_pending_migrations( app: &App, @@ -53,18 +54,18 @@ pub(crate) fn upgrade_pending_migrations( }) .collect::, _>>()?; - migra::maybe_with_transaction( - opts.transaction_opts.single_transaction, + should_run_in_transaction( &mut client, - &mut |mut client| { + opts.transaction_opts.single_transaction, + |client| { migrations_with_content .iter() .try_for_each(|(migration_name, content)| { println!("upgrade {}...", migration_name); - migra::maybe_with_transaction( + should_run_in_transaction( + client, !opts.transaction_opts.single_transaction, - &mut client, - &mut |client| client.run_upgrade_migration(migration_name, &content), + |client| client.run_upgrade_migration(migration_name, &content), ) }) .map_err(From::from)