Archived
1
0
Fork 0

refac: move run in transaction to cli utils

This commit is contained in:
Dmitriy Pleshevskiy 2021-06-12 22:48:47 +03:00
parent 7c29bcb113
commit f913952df0
6 changed files with 32 additions and 36 deletions

View file

@ -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<TrxFnMut>(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<TrxFnMut>(
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)
}
}

View file

@ -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;

View file

@ -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::<Result<Vec<_>, _>>()?;
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),

View file

@ -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::<Result<Vec<_>, _>>()?;
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),

View file

@ -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::<Result<Vec<_>, _>>()?;
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),

View file

@ -42,3 +42,29 @@ pub fn create_client_from_config(config: &Config) -> migra::StdResult<AnyClient>
)
.map_err(From::from)
}
pub fn run_in_transaction<TrxFnMut>(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<TrxFnMut>(
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)
}
}