From 0318c4eb4db51d21dd59a47294c954980032b0ee Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 10 Jun 2021 23:54:13 +0300 Subject: [PATCH] refac: move transaction utils to core --- migra/src/clients/mod.rs | 29 +++++++++++++++++++++++++++++ migra/src/lib.rs | 4 ++-- migra_cli/src/commands/apply.rs | 4 ++-- migra_cli/src/commands/downgrade.rs | 4 ++-- migra_cli/src/commands/upgrade.rs | 4 ++-- migra_cli/src/database.rs | 29 ----------------------------- 6 files changed, 37 insertions(+), 37 deletions(-) diff --git a/migra/src/clients/mod.rs b/migra/src/clients/mod.rs index a306036..38137a6 100644 --- a/migra/src/clients/mod.rs +++ b/migra/src/clients/mod.rs @@ -35,3 +35,32 @@ pub use self::mysql::Client as MysqlClient; 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 +where + TrxFnMut: FnMut(&mut AnyClient) -> MigraResult, +{ + client + .begin_transaction() + .and_then(|_| trx_fn(client)) + .and_then(|res| client.commit_transaction().and(Ok(res))) + .or_else(|err| client.rollback_transaction().and(Err(err))) +} + +pub fn maybe_with_transaction( + is_needed: bool, + client: &mut AnyClient, + trx_fn: &mut TrxFnMut, +) -> MigraResult +where + TrxFnMut: FnMut(&mut AnyClient) -> MigraResult, +{ + if is_needed { + with_transaction(client, trx_fn) + } else { + trx_fn(client) + } +} diff --git a/migra/src/lib.rs b/migra/src/lib.rs index 3e4fe82..94fc636 100644 --- a/migra/src/lib.rs +++ b/migra/src/lib.rs @@ -4,11 +4,11 @@ pub mod clients; +mod errors; pub mod fs; pub mod managers; pub mod migration; -mod errors; +pub use clients::{maybe_with_transaction, with_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 e6ec4c9..13604c5 100644 --- a/migra_cli/src/commands/apply.rs +++ b/migra_cli/src/commands/apply.rs @@ -20,14 +20,14 @@ pub(crate) fn apply_sql(app: &App, cmd_opts: &ApplyCommandOpt) -> migra::StdResu .map(std::fs::read_to_string) .collect::, _>>()?; - database::maybe_with_transaction( + migra::maybe_with_transaction( cmd_opts.transaction_opts.single_transaction, &mut client, &mut |mut client| { file_contents .iter() .try_for_each(|content| { - database::maybe_with_transaction( + migra::maybe_with_transaction( !cmd_opts.transaction_opts.single_transaction, &mut client, &mut |client| client.apply_sql(content), diff --git a/migra_cli/src/commands/downgrade.rs b/migra_cli/src/commands/downgrade.rs index 039d2a5..9e1dbdc 100644 --- a/migra_cli/src/commands/downgrade.rs +++ b/migra_cli/src/commands/downgrade.rs @@ -32,7 +32,7 @@ pub(crate) fn rollback_applied_migrations( }) .collect::, _>>()?; - database::maybe_with_transaction( + migra::maybe_with_transaction( opts.transaction_opts.single_transaction, &mut client, &mut |mut client| { @@ -41,7 +41,7 @@ pub(crate) fn rollback_applied_migrations( .try_for_each(|(migration_name, content)| { if all_migrations.contains_name(migration_name) { println!("downgrade {}...", migration_name); - database::maybe_with_transaction( + migra::maybe_with_transaction( !opts.transaction_opts.single_transaction, &mut client, &mut |client| client.run_downgrade_migration(migration_name, &content), diff --git a/migra_cli/src/commands/upgrade.rs b/migra_cli/src/commands/upgrade.rs index 92a55d8..11b9106 100644 --- a/migra_cli/src/commands/upgrade.rs +++ b/migra_cli/src/commands/upgrade.rs @@ -53,7 +53,7 @@ pub(crate) fn upgrade_pending_migrations( }) .collect::, _>>()?; - database::maybe_with_transaction( + migra::maybe_with_transaction( opts.transaction_opts.single_transaction, &mut client, &mut |mut client| { @@ -61,7 +61,7 @@ pub(crate) fn upgrade_pending_migrations( .iter() .try_for_each(|(migration_name, content)| { println!("upgrade {}...", migration_name); - database::maybe_with_transaction( + migra::maybe_with_transaction( !opts.transaction_opts.single_transaction, &mut client, &mut |client| client.run_upgrade_migration(migration_name, &content), diff --git a/migra_cli/src/database.rs b/migra_cli/src/database.rs index c0bd188..67f3507 100644 --- a/migra_cli/src/database.rs +++ b/migra_cli/src/database.rs @@ -42,32 +42,3 @@ pub fn create_client_from_config(config: &Config) -> migra::StdResult ) .map_err(From::from) } - -pub fn with_transaction( - client: &mut AnyClient, - trx_fn: &mut TrxFnMut, -) -> migra::Result -where - TrxFnMut: FnMut(&mut AnyClient) -> migra::Result, -{ - client - .begin_transaction() - .and_then(|_| trx_fn(client)) - .and_then(|res| client.commit_transaction().and(Ok(res))) - .or_else(|err| client.rollback_transaction().and(Err(err))) -} - -pub fn maybe_with_transaction( - is_needed: bool, - client: &mut AnyClient, - trx_fn: &mut TrxFnMut, -) -> migra::Result -where - TrxFnMut: FnMut(&mut AnyClient) -> migra::Result, -{ - if is_needed { - with_transaction(client, trx_fn) - } else { - trx_fn(client) - } -}