Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions refinery_core/src/drivers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use std::convert::Infallible;
impl Transaction for Config {
type Error = Infallible;

fn execute(&mut self, _queries: &[&str]) -> Result<usize, Self::Error> {
fn execute<'a, T: Iterator<Item = &'a str>>(
&mut self,
_queries: T,
) -> Result<usize, Self::Error> {
Ok(0)
}
}
Expand All @@ -33,7 +36,10 @@ impl Query<Vec<Migration>> for Config {
impl AsyncTransaction for Config {
type Error = Infallible;

async fn execute(&mut self, _queries: &[&str]) -> Result<usize, Self::Error> {
async fn execute<'a, T: Iterator<Item = &'a str> + Send>(
&mut self,
_queries: T,
) -> Result<usize, Self::Error> {
Ok(0)
}
}
Expand Down
14 changes: 10 additions & 4 deletions refinery_core/src/drivers/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ fn query_applied_migrations(
impl Transaction for Conn {
type Error = MError;

fn execute(&mut self, queries: &[&str]) -> Result<usize, Self::Error> {
fn execute<'a, T: Iterator<Item = &'a str>>(
&mut self,
queries: T,
) -> Result<usize, Self::Error> {
let mut transaction = self.start_transaction(get_tx_opts())?;
let mut count = 0;
for query in queries.iter() {
for query in queries {
transaction.query_iter(query)?;
count += 1;
}
Expand All @@ -58,11 +61,14 @@ impl Transaction for Conn {
impl Transaction for PooledConn {
type Error = MError;

fn execute(&mut self, queries: &[&str]) -> Result<usize, Self::Error> {
fn execute<'a, T: Iterator<Item = &'a str>>(
&mut self,
queries: T,
) -> Result<usize, Self::Error> {
let mut transaction = self.start_transaction(get_tx_opts())?;
let mut count = 0;

for query in queries.iter() {
for query in queries {
transaction.query_iter(query)?;
count += 1;
}
Expand Down
7 changes: 5 additions & 2 deletions refinery_core/src/drivers/mysql_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ async fn query_applied_migrations<'a>(
impl AsyncTransaction for Pool {
type Error = MError;

async fn execute(&mut self, queries: &[&str]) -> Result<usize, Self::Error> {
async fn execute<'a, T: Iterator<Item = &'a str> + Send>(
&mut self,
queries: T,
) -> Result<usize, Self::Error> {
let mut conn = self.get_conn().await?;
let mut options = TxOpts::new();
options.with_isolation_level(Some(IsolationLevel::ReadCommitted));

let mut transaction = conn.start_transaction(options).await?;
let mut count = 0;
for query in queries {
transaction.query_drop(*query).await?;
transaction.query_drop(query).await?;
count += 1;
}
transaction.commit().await?;
Expand Down
7 changes: 5 additions & 2 deletions refinery_core/src/drivers/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ fn query_applied_migrations(
impl Transaction for PgClient {
type Error = PgError;

fn execute(&mut self, queries: &[&str]) -> Result<usize, Self::Error> {
fn execute<'a, T: Iterator<Item = &'a str>>(
&mut self,
queries: T,
) -> Result<usize, Self::Error> {
let mut transaction = PgClient::transaction(self)?;
let mut count = 0;
for query in queries.iter() {
for query in queries {
PgTransaction::batch_execute(&mut transaction, query)?;
count += 1;
}
Expand Down
7 changes: 5 additions & 2 deletions refinery_core/src/drivers/rusqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ fn query_applied_migrations(

impl Transaction for RqlConnection {
type Error = RqlError;
fn execute(&mut self, queries: &[&str]) -> Result<usize, Self::Error> {
fn execute<'a, T: Iterator<Item = &'a str>>(
&mut self,
queries: T,
) -> Result<usize, Self::Error> {
let transaction = self.transaction()?;
let mut count = 0;
for query in queries.iter() {
for query in queries {
transaction.execute_batch(query)?;
count += 1;
}
Expand Down
7 changes: 5 additions & 2 deletions refinery_core/src/drivers/tiberius.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ where
{
type Error = Error;

async fn execute(&mut self, queries: &[&str]) -> Result<usize, Self::Error> {
async fn execute<'a, T: Iterator<Item = &'a str> + Send>(
&mut self,
queries: T,
) -> Result<usize, Self::Error> {
// Tiberius doesn't support transactions, see https://github.com/prisma/tiberius/issues/28
self.simple_query("BEGIN TRAN T1;").await?;
let mut count = 0;
for query in queries {
// Drop the returning `QueryStream<'a>` to avoid compiler complaning regarding lifetimes
if let Err(err) = self.simple_query(*query).await.map(drop) {
if let Err(err) = self.simple_query(query).await.map(drop) {
if let Err(err) = self.simple_query("ROLLBACK TRAN T1").await {
log::error!("could not ROLLBACK transaction, {}", err);
}
Expand Down
5 changes: 4 additions & 1 deletion refinery_core/src/drivers/tokio_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ async fn query_applied_migrations(
impl AsyncTransaction for Client {
type Error = PgError;

async fn execute(&mut self, queries: &[&str]) -> Result<usize, Self::Error> {
async fn execute<'a, T: Iterator<Item = &'a str> + Send>(
&mut self,
queries: T,
) -> Result<usize, Self::Error> {
let transaction = self.transaction().await?;
let mut count = 0;
for query in queries {
Expand Down
32 changes: 19 additions & 13 deletions refinery_core/src/traits/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use std::string::ToString;
pub trait AsyncTransaction {
type Error: std::error::Error + Send + Sync + 'static;

async fn execute(&mut self, query: &[&str]) -> Result<usize, Self::Error>;
async fn execute<'a, T: Iterator<Item = &'a str> + Send>(
&mut self,
queries: T,
) -> Result<usize, Self::Error>;
}

#[async_trait]
Expand Down Expand Up @@ -43,10 +46,13 @@ async fn migrate<T: AsyncTransaction>(
migration.set_applied();
let update_query = insert_migration_query(&migration, migration_table_name);
transaction
.execute(&[
migration.sql().as_ref().expect("sql must be Some!"),
&update_query,
])
.execute(
[
migration.sql().as_ref().expect("sql must be Some!"),
update_query.as_str(),
]
.into_iter(),
)
.await
.migration_err(
&format!("error applying migration {migration}"),
Expand Down Expand Up @@ -109,10 +115,8 @@ async fn migrate_grouped<T: AsyncTransaction>(
);
}

let refs: Vec<&str> = grouped_migrations.iter().map(AsRef::as_ref).collect();

transaction
.execute(refs.as_ref())
.execute(grouped_migrations.iter().map(AsRef::as_ref))
.await
.migration_err("error applying migrations", None)?;

Expand Down Expand Up @@ -142,7 +146,7 @@ where
migration_table_name: &str,
) -> Result<Option<Migration>, Error> {
let mut migrations = self
.query(Self::get_last_applied_migration_query(migration_table_name).as_str())
.query(Self::get_last_applied_migration_query(migration_table_name).as_ref())
.await
.migration_err("error getting last applied migration", None)?;

Expand All @@ -154,7 +158,7 @@ where
migration_table_name: &str,
) -> Result<Vec<Migration>, Error> {
let migrations = self
.query(Self::get_applied_migrations_query(migration_table_name).as_str())
.query(Self::get_applied_migrations_query(migration_table_name).as_ref())
.await
.migration_err("error getting applied migrations", None)?;

Expand All @@ -170,9 +174,11 @@ where
target: Target,
migration_table_name: &str,
) -> Result<Report, Error> {
self.execute(&[&Self::assert_migrations_table_query(migration_table_name)])
.await
.migration_err("error asserting migrations table", None)?;
self.execute(
[Self::assert_migrations_table_query(migration_table_name).as_ref()].into_iter(),
)
.await
.migration_err("error asserting migrations table", None)?;

let applied_migrations = self
.get_applied_migrations(migration_table_name)
Expand Down
19 changes: 12 additions & 7 deletions refinery_core/src/traits/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use crate::{Error, Migration, Report, Target};
pub trait Transaction {
type Error: std::error::Error + Send + Sync + 'static;

fn execute(&mut self, queries: &[&str]) -> Result<usize, Self::Error>;
fn execute<'a, T: Iterator<Item = &'a str>>(
&mut self,
queries: T,
) -> Result<usize, Self::Error>;
}

pub trait Query<T>: Transaction {
Expand Down Expand Up @@ -66,7 +69,7 @@ pub fn migrate<T: Transaction>(
}
};

let refs: Vec<&str> = migration_batch.iter().map(AsRef::as_ref).collect();
let refs = migration_batch.iter().map(AsRef::as_ref);

if batched {
let migrations_display = applied_migrations
Expand All @@ -76,10 +79,10 @@ pub fn migrate<T: Transaction>(
.join("\n");
log::info!("going to apply batch migrations in single transaction:\n{migrations_display}");
transaction
.execute(refs.as_ref())
.execute(refs)
.migration_err("error applying migrations", None)?;
} else {
for (i, update) in refs.iter().enumerate() {
for (i, update) in refs.enumerate() {
// first iteration is pair so we know the following even in the iteration index
// marks the previous (pair) migration as completed.
let applying_migration = i % 2 == 0;
Expand All @@ -91,7 +94,7 @@ pub fn migrate<T: Transaction>(
log::debug!("applied migration: {current_migration} writing state to db.");
}
transaction
.execute(&[update])
.execute([update].into_iter())
.migration_err("error applying update", Some(&applied_migrations[0..i / 2]))?;
}
}
Expand Down Expand Up @@ -119,8 +122,10 @@ where
fn assert_migrations_table(&mut self, migration_table_name: &str) -> Result<usize, Error> {
// Needed cause some database vendors like Mssql have a non sql standard way of checking the migrations table,
// though on this case it's just to be consistent with the async trait `AsyncMigrate`
self.execute(&[Self::assert_migrations_table_query(migration_table_name).as_str()])
.migration_err("error asserting migrations table", None)
self.execute(
[Self::assert_migrations_table_query(migration_table_name).as_ref()].into_iter(),
)
.migration_err("error asserting migrations table", None)
}

fn get_last_applied_migration(
Expand Down
Loading