Archived
1
0
Fork 0

refac: change transaction utils

This commit is contained in:
Dmitriy Pleshevskiy 2021-06-12 00:32:00 +03:00
parent 0318c4eb4d
commit b78651f81d
5 changed files with 31 additions and 31 deletions

View file

@ -19,7 +19,7 @@ where
pub trait Client: ManageMigrations + ManageTransaction {}
pub type AnyClient = Box<dyn Client>;
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<TrxFnMut, Res>(
client: &mut AnyClient,
trx_fn: &mut TrxFnMut,
) -> MigraResult<Res>
pub fn run_in_transaction<TrxFnMut>(client: &mut AnyClient, trx_fn: TrxFnMut) -> MigraResult<()>
where
TrxFnMut: FnMut(&mut AnyClient) -> MigraResult<Res>,
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<TrxFnMut, Res>(
is_needed: bool,
pub fn should_run_in_transaction<TrxFnMut>(
client: &mut AnyClient,
trx_fn: &mut TrxFnMut,
) -> MigraResult<Res>
is_needed: bool,
trx_fn: TrxFnMut,
) -> MigraResult<()>
where
TrxFnMut: FnMut(&mut AnyClient) -> MigraResult<Res>,
TrxFnMut: FnOnce(&mut AnyClient) -> MigraResult<()>,
{
if is_needed {
with_transaction(client, trx_fn)
run_in_transaction(client, trx_fn)
} else {
trx_fn(client)
}

View file

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

View file

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

View file

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

View file

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