From f913952df024af7b1a9a861b92cdd42d5cb5f50d Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sat, 12 Jun 2021 22:48:47 +0300 Subject: [PATCH] refac: move run in transaction to cli utils --- migra/src/clients/mod.rs | 26 -------------------------- migra/src/lib.rs | 1 - migra_cli/src/commands/apply.rs | 5 ++--- migra_cli/src/commands/downgrade.rs | 5 ++--- migra_cli/src/commands/upgrade.rs | 5 ++--- migra_cli/src/database.rs | 26 ++++++++++++++++++++++++++ 6 files changed, 32 insertions(+), 36 deletions(-) diff --git a/migra/src/clients/mod.rs b/migra/src/clients/mod.rs index a9474d2..9291b69 100644 --- a/migra/src/clients/mod.rs +++ b/migra/src/clients/mod.rs @@ -35,29 +35,3 @@ pub use self::mysql::Client as MysqlClient; pub mod sqlite; #[cfg(feature = "sqlite")] pub use self::sqlite::Client as SqliteClient; - -pub fn run_in_transaction(client: &mut AnyClient, trx_fn: TrxFnMut) -> MigraResult<()> -where - TrxFnMut: FnOnce(&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 should_run_in_transaction( - client: &mut AnyClient, - is_needed: bool, - trx_fn: TrxFnMut, -) -> MigraResult<()> -where - TrxFnMut: FnOnce(&mut AnyClient) -> MigraResult<()>, -{ - if is_needed { - run_in_transaction(client, trx_fn) - } else { - trx_fn(client) - } -} diff --git a/migra/src/lib.rs b/migra/src/lib.rs index b7166bd..b3e4c82 100644 --- a/migra/src/lib.rs +++ b/migra/src/lib.rs @@ -9,6 +9,5 @@ pub mod fs; pub mod managers; pub mod migration; -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 7cbe398..07d69f1 100644 --- a/migra_cli/src/commands/apply.rs +++ b/migra_cli/src/commands/apply.rs @@ -1,7 +1,6 @@ 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()?; @@ -21,14 +20,14 @@ pub(crate) fn apply_sql(app: &App, cmd_opts: &ApplyCommandOpt) -> migra::StdResu .map(std::fs::read_to_string) .collect::, _>>()?; - should_run_in_transaction( + database::should_run_in_transaction( &mut client, cmd_opts.transaction_opts.single_transaction, |client| { file_contents .iter() .try_for_each(|content| { - should_run_in_transaction( + database::should_run_in_transaction( client, !cmd_opts.transaction_opts.single_transaction, |client| client.apply_sql(content), diff --git a/migra_cli/src/commands/downgrade.rs b/migra_cli/src/commands/downgrade.rs index c3f930e..7c2eeeb 100644 --- a/migra_cli/src/commands/downgrade.rs +++ b/migra_cli/src/commands/downgrade.rs @@ -1,7 +1,6 @@ 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( @@ -33,7 +32,7 @@ pub(crate) fn rollback_applied_migrations( }) .collect::, _>>()?; - should_run_in_transaction( + database::should_run_in_transaction( &mut client, opts.transaction_opts.single_transaction, |client| { @@ -42,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); - should_run_in_transaction( + database::should_run_in_transaction( client, !opts.transaction_opts.single_transaction, |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 1171ee8..82edc9b 100644 --- a/migra_cli/src/commands/upgrade.rs +++ b/migra_cli/src/commands/upgrade.rs @@ -2,7 +2,6 @@ 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, @@ -54,7 +53,7 @@ pub(crate) fn upgrade_pending_migrations( }) .collect::, _>>()?; - should_run_in_transaction( + database::should_run_in_transaction( &mut client, opts.transaction_opts.single_transaction, |client| { @@ -62,7 +61,7 @@ pub(crate) fn upgrade_pending_migrations( .iter() .try_for_each(|(migration_name, content)| { println!("upgrade {}...", migration_name); - should_run_in_transaction( + database::should_run_in_transaction( client, !opts.transaction_opts.single_transaction, |client| client.run_upgrade_migration(migration_name, &content), diff --git a/migra_cli/src/database.rs b/migra_cli/src/database.rs index 67f3507..6d71e06 100644 --- a/migra_cli/src/database.rs +++ b/migra_cli/src/database.rs @@ -42,3 +42,29 @@ pub fn create_client_from_config(config: &Config) -> migra::StdResult ) .map_err(From::from) } + +pub fn run_in_transaction(client: &mut AnyClient, trx_fn: TrxFnMut) -> migra::Result<()> +where + TrxFnMut: FnOnce(&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 should_run_in_transaction( + client: &mut AnyClient, + is_needed: bool, + trx_fn: TrxFnMut, +) -> migra::Result<()> +where + TrxFnMut: FnOnce(&mut AnyClient) -> migra::Result<()>, +{ + if is_needed { + run_in_transaction(client, trx_fn) + } else { + trx_fn(client) + } +}