refac: move transaction utils to core
This commit is contained in:
parent
687062fd76
commit
0318c4eb4d
6 changed files with 37 additions and 37 deletions
|
@ -35,3 +35,32 @@ pub use self::mysql::Client as MysqlClient;
|
||||||
pub mod sqlite;
|
pub mod sqlite;
|
||||||
#[cfg(feature = "sqlite")]
|
#[cfg(feature = "sqlite")]
|
||||||
pub use self::sqlite::Client as SqliteClient;
|
pub use self::sqlite::Client as SqliteClient;
|
||||||
|
|
||||||
|
pub fn with_transaction<TrxFnMut, Res>(
|
||||||
|
client: &mut AnyClient,
|
||||||
|
trx_fn: &mut TrxFnMut,
|
||||||
|
) -> MigraResult<Res>
|
||||||
|
where
|
||||||
|
TrxFnMut: FnMut(&mut AnyClient) -> MigraResult<Res>,
|
||||||
|
{
|
||||||
|
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<TrxFnMut, Res>(
|
||||||
|
is_needed: bool,
|
||||||
|
client: &mut AnyClient,
|
||||||
|
trx_fn: &mut TrxFnMut,
|
||||||
|
) -> MigraResult<Res>
|
||||||
|
where
|
||||||
|
TrxFnMut: FnMut(&mut AnyClient) -> MigraResult<Res>,
|
||||||
|
{
|
||||||
|
if is_needed {
|
||||||
|
with_transaction(client, trx_fn)
|
||||||
|
} else {
|
||||||
|
trx_fn(client)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -4,11 +4,11 @@
|
||||||
|
|
||||||
pub mod clients;
|
pub mod clients;
|
||||||
|
|
||||||
|
mod errors;
|
||||||
pub mod fs;
|
pub mod fs;
|
||||||
pub mod managers;
|
pub mod managers;
|
||||||
pub mod migration;
|
pub mod migration;
|
||||||
|
|
||||||
mod errors;
|
pub use clients::{maybe_with_transaction, with_transaction};
|
||||||
pub use errors::{Error, MigraResult as Result, StdResult};
|
pub use errors::{Error, MigraResult as Result, StdResult};
|
||||||
|
|
||||||
pub use migration::Migration;
|
pub use migration::Migration;
|
||||||
|
|
|
@ -20,14 +20,14 @@ pub(crate) fn apply_sql(app: &App, cmd_opts: &ApplyCommandOpt) -> migra::StdResu
|
||||||
.map(std::fs::read_to_string)
|
.map(std::fs::read_to_string)
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
database::maybe_with_transaction(
|
migra::maybe_with_transaction(
|
||||||
cmd_opts.transaction_opts.single_transaction,
|
cmd_opts.transaction_opts.single_transaction,
|
||||||
&mut client,
|
&mut client,
|
||||||
&mut |mut client| {
|
&mut |mut client| {
|
||||||
file_contents
|
file_contents
|
||||||
.iter()
|
.iter()
|
||||||
.try_for_each(|content| {
|
.try_for_each(|content| {
|
||||||
database::maybe_with_transaction(
|
migra::maybe_with_transaction(
|
||||||
!cmd_opts.transaction_opts.single_transaction,
|
!cmd_opts.transaction_opts.single_transaction,
|
||||||
&mut client,
|
&mut client,
|
||||||
&mut |client| client.apply_sql(content),
|
&mut |client| client.apply_sql(content),
|
||||||
|
|
|
@ -32,7 +32,7 @@ pub(crate) fn rollback_applied_migrations(
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
database::maybe_with_transaction(
|
migra::maybe_with_transaction(
|
||||||
opts.transaction_opts.single_transaction,
|
opts.transaction_opts.single_transaction,
|
||||||
&mut client,
|
&mut client,
|
||||||
&mut |mut client| {
|
&mut |mut client| {
|
||||||
|
@ -41,7 +41,7 @@ pub(crate) fn rollback_applied_migrations(
|
||||||
.try_for_each(|(migration_name, content)| {
|
.try_for_each(|(migration_name, content)| {
|
||||||
if all_migrations.contains_name(migration_name) {
|
if all_migrations.contains_name(migration_name) {
|
||||||
println!("downgrade {}...", migration_name);
|
println!("downgrade {}...", migration_name);
|
||||||
database::maybe_with_transaction(
|
migra::maybe_with_transaction(
|
||||||
!opts.transaction_opts.single_transaction,
|
!opts.transaction_opts.single_transaction,
|
||||||
&mut client,
|
&mut client,
|
||||||
&mut |client| client.run_downgrade_migration(migration_name, &content),
|
&mut |client| client.run_downgrade_migration(migration_name, &content),
|
||||||
|
|
|
@ -53,7 +53,7 @@ pub(crate) fn upgrade_pending_migrations(
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
database::maybe_with_transaction(
|
migra::maybe_with_transaction(
|
||||||
opts.transaction_opts.single_transaction,
|
opts.transaction_opts.single_transaction,
|
||||||
&mut client,
|
&mut client,
|
||||||
&mut |mut client| {
|
&mut |mut client| {
|
||||||
|
@ -61,7 +61,7 @@ pub(crate) fn upgrade_pending_migrations(
|
||||||
.iter()
|
.iter()
|
||||||
.try_for_each(|(migration_name, content)| {
|
.try_for_each(|(migration_name, content)| {
|
||||||
println!("upgrade {}...", migration_name);
|
println!("upgrade {}...", migration_name);
|
||||||
database::maybe_with_transaction(
|
migra::maybe_with_transaction(
|
||||||
!opts.transaction_opts.single_transaction,
|
!opts.transaction_opts.single_transaction,
|
||||||
&mut client,
|
&mut client,
|
||||||
&mut |client| client.run_upgrade_migration(migration_name, &content),
|
&mut |client| client.run_upgrade_migration(migration_name, &content),
|
||||||
|
|
|
@ -42,32 +42,3 @@ pub fn create_client_from_config(config: &Config) -> migra::StdResult<AnyClient>
|
||||||
)
|
)
|
||||||
.map_err(From::from)
|
.map_err(From::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_transaction<TrxFnMut, Res>(
|
|
||||||
client: &mut AnyClient,
|
|
||||||
trx_fn: &mut TrxFnMut,
|
|
||||||
) -> migra::Result<Res>
|
|
||||||
where
|
|
||||||
TrxFnMut: FnMut(&mut AnyClient) -> migra::Result<Res>,
|
|
||||||
{
|
|
||||||
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<TrxFnMut, Res>(
|
|
||||||
is_needed: bool,
|
|
||||||
client: &mut AnyClient,
|
|
||||||
trx_fn: &mut TrxFnMut,
|
|
||||||
) -> migra::Result<Res>
|
|
||||||
where
|
|
||||||
TrxFnMut: FnMut(&mut AnyClient) -> migra::Result<Res>,
|
|
||||||
{
|
|
||||||
if is_needed {
|
|
||||||
with_transaction(client, trx_fn)
|
|
||||||
} else {
|
|
||||||
trx_fn(client)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Reference in a new issue