Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs committed Dec 16, 2019
1 parent abfed19 commit 846e9a9
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 119 deletions.
6 changes: 4 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ jobs:
steps:
- checkout
- run: cargo build -p refinery_cli
- run: cd refinery && cargo test -p refinery --features "ttokio-postgres" --test tokio-postgres -- --test-threads 1
- run: cd refinery && cargo test -p refinery --features "tttokio-postgres" --test tokio_postgres -- --test-threads 1
test-tokio-postgres-nightly:
docker:
- image: << pipeline.parameters.nightly >>
- image: postgres:9.6.13-alpine
steps:
- checkout
- run: cargo build -p refinery_cli
- run: cd refinery && cargo test -p refinery --features "ttokio-postgres" --test tokio-postgres -- --test-threads 1
- run: cd refinery && cargo test -p refinery --features "tttokio-postgres" --test tokio_postgres -- --test-threads 1
test-mysql-previous:
docker:
- image: << pipeline.parameters.previous >>
Expand Down Expand Up @@ -176,6 +176,8 @@ workflows:
- test-postgres-previous
- test-postgres-stable
- test-postgres-nightly
- test-tokio-postgres-stable
- test-tokio-postgres-nightly
- test-mysql-previous
- test-mysql-stable
- test-mysql-nightly
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ fn main() {
for more examples refer to the [`examples`](examples)

## Implementation details
refinery works by creating a table that keeps all the applied migrations versions and it's metadata.
When you [run](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.run) the migrations `Runner`,
refinery compares the applied migrations with the ones to be applied, verifying [divergent](https://docs.rs/refinery/0.1.10/refinery/struct.Runner.html#method.set_abort_divergent) and [missing](https://docs.rs/refinery/0.1.10/refinery/struct.Runner.html#method.set_abort_missing) and executing unapplied migrations.\
By default refinery runs each migration on a single transaction, alternatively you can also configure Refinery to wrap the entire execution of all migrations on a single transaction by setting [set_grouped](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.set_grouped) to true.

refinery works by creating a table that keeps all the applied migrations versions and it's metadata. When you [run](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.run) the migrations `Runner`, Refinery compares the applied migrations with the the ones to be applied, checking for [divergent](https://docs.rs/refinery/0.1.10/refinery/struct.Runner.html#method.set_abort_divergent) and [missing](https://docs.rs/refinery/0.1.10/refinery/struct.Runner.html#method.set_abort_missing) and executing unapplied migrations.\
By default refinery runs each migration on a single transaction, alternatively you can also configure refinery to wrap the entire execution of all migrations on a single transaction by setting [set_grouped](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.set_grouped) to true.
### Rollback

refinery's design is based on [flyway](https://flywaydb.org/) and so, shares it's [perspective](https://flywaydb.org/documentation/command/undo#important-notes) on undo/rollback migrations. To undo/rollback a migration you have to generate a new one and write specificaly what you want to undo.
Expand All @@ -61,7 +58,9 @@ refinery aims to support stable Rust, the previous Rust version, and nightly

## Async

while refinery plans to offer async migrations, for now the best way to run migrations on an async context is to run them inside something like tokio's [`spawn_blocking`](https://docs.rs/tokio/0.2.4/tokio/task/fn.spawn_blocking.html) or, if with Rusqlite, tokio's [`block_in_place`](https://docs.rs/tokio/0.2.0/tokio/task/fn.block_in_place.html)
For the momment only [tokio-postgres](https://crates.io/crates/tokio-postgres) is supported on master branch. To migrate async you have to call `Runner`'s [run_async](https://github.com/rust-db/refinery/blob/master/refinery_migrations/src/lib.rs#L216).
More drivers are going to be supported soon and a new version release with them.\
For drivers that are not supported yet, best way to run migrations on an async context is to run them inside something like tokio's [`spawn_blocking`](https://docs.rs/tokio/0.2.4/tokio/task/fn.spawn_blocking.html), or if with Rusqlite, tokio's [`block_in_place`](https://docs.rs/tokio/0.2.0/tokio/task/fn.block_in_place.html).

## Contributing

Expand Down
9 changes: 5 additions & 4 deletions refinery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@ edition = "2018"
default = []
rusqlite = ["refinery-migrations/rusqlite", "barrel/sqlite3"]
postgres = ["refinery-migrations/postgres", "barrel/pg"]
tokio-postgres = ["refinery-migrations/tokio-postgres"]
tokio-postgres = ["refinery-migrations/async", "refinery-migrations/tokio-postgres"]
mysql = ["refinery-migrations/mysql", "barrel/mysql"]

#testing features
trusqlite = ["mysql", "postgres", "rusqlite", "refinery-migrations/mysql", "refinery-migrations/postgres", "refinery-migrations/rusqlite", "mod_migrations/sqlite"]
tpostgres = ["mysql", "postgres", "rusqlite", "refinery-migrations/mysql", "refinery-migrations/postgres", "refinery-migrations/rusqlite", "mod_migrations/pg"]
ttokio-postgres = ["refinery-migrations/tokio-postgres", "mod_migrations/pg"]
tttokio-postgres = ["tokio", "ttokio_postgres", "refinery-migrations/async", "refinery-migrations/tokio-postgres", "mod_migrations/pg"]

tmysql = ["mysql", "postgres", "rusqlite", "refinery-migrations/mysql", "refinery-migrations/postgres", "refinery-migrations/rusqlite", "mod_migrations/mysql"]

[dependencies]
refinery-migrations= { version = "0.1.0", path = "../refinery_migrations" }
refinery-macros= { version = "0.1.0", path = "../refinery_macros" }
barrel = "0.5.3"
# hack because there's no optional dev-dependencies
tokio = { version = "0.2", features = ["full"], optional = true }
ttokio_postgres = {package = "tokio-postgres", version = "0.5.0-alpha.2", optional = true }

[dev-dependencies]
ttrusqlite = {package = "rusqlite", version = "0.18.0"}
ttpostgres = {package = "postgres", version = "0.15"}
ttokio_postgres = {package = "tokio-postgres", version = "0.5.0-alpha.2" }
tokio = { version = "0.2", features = ["full"] }
futures = "0.3.1"
ttmysql = {package = "mysql", version = "16.0"}

Expand Down
16 changes: 9 additions & 7 deletions refinery/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Refinery Logo](assets/logo_wide.svg)
![refinery Logo](assets/logo_wide.svg)

Powerful SQL migration toolkit for Rust.

Expand Down Expand Up @@ -45,20 +45,22 @@ fn main() {
for more examples refer to the [`examples`](examples)

## Implementation details
Refinery works by creating a table that keeps all the applied migrations versions and it's metadata. When you [run](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.run) the migrations `Runner`, Refinery compares the applied migrations withe the ones to be applied, checking for [divergent](https://docs.rs/refinery/0.1.10/refinery/struct.Runner.html#method.set_abort_divergent) and [missing](https://docs.rs/refinery/0.1.10/refinery/struct.Runner.html#method.set_abort_missing) and executing unapplied migrations

refinery works by creating a table that keeps all the applied migrations versions and it's metadata. When you [run](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.run) the migrations `Runner`, Refinery compares the applied migrations with the the ones to be applied, checking for [divergent](https://docs.rs/refinery/0.1.10/refinery/struct.Runner.html#method.set_abort_divergent) and [missing](https://docs.rs/refinery/0.1.10/refinery/struct.Runner.html#method.set_abort_missing) and executing unapplied migrations.\
By default refinery runs each migration on a single transaction, alternatively you can also configure refinery to wrap the entire execution of all migrations on a single transaction by setting [set_grouped](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.set_grouped) to true.
### Rollback

Refinery's design is based on [flyway](https://flywaydb.org/) and so, shares it's [perspective](https://flywaydb.org/documentation/command/undo#important-notes) on undo/rollback migrations. To undo/rollback a migration you have to generate a new one and write specificaly what you want to undo.
refinery's design is based on [flyway](https://flywaydb.org/) and so, shares it's [perspective](https://flywaydb.org/documentation/command/undo#important-notes) on undo/rollback migrations. To undo/rollback a migration you have to generate a new one and write specificaly what you want to undo.

## Compatibility

Refinery aims to support stable Rust, the previous Rust version, and nightly
refinery aims to support stable Rust, the previous Rust version, and nightly


## Async

while Refinery plans to offer async migrations, for now the best way to run migrations on an async context is to run them inside something like tokio's [`spawn_blocking`](https://docs.rs/tokio/0.2.4/tokio/task/fn.spawn_blocking.html)
For the momment only [tokio-postgres](https://crates.io/crates/tokio-postgres) is supported on master branch. To migrate async you have to call `Runner`'s [run_async](https://github.com/rust-db/refinery/blob/master/refinery_migrations/src/lib.rs#L216).
More drivers are going to be supported soon and a new version release with them.\
For drivers that are not supported yet, best way to run migrations on an async context is to run them inside something like tokio's [`spawn_blocking`](https://docs.rs/tokio/0.2.4/tokio/task/fn.spawn_blocking.html), or if with Rusqlite, tokio's [`block_in_place`](https://docs.rs/tokio/0.2.0/tokio/task/fn.block_in_place.html).

## Contributing

Expand All @@ -72,5 +74,5 @@ This project is licensed under the [MIT license](LICENSE).
### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in Refinery by you, shall be licensed as MIT, without any additional
for inclusion in refinery by you, shall be licensed as MIT, without any additional
terms or conditions.
89 changes: 36 additions & 53 deletions refinery/tests/tokio_postgres.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
mod tokio_postgres {
use assert_cmd::prelude::*;
use chrono::{DateTime, Local};
// use predicates::str::contains;
use refinery::Error;
use refinery::Migration;
use refinery_migrations::AsyncMigrate;
// use refinery::{migrate_from_config, Config, ConfigDbType, Error, Migrate as _, Migration};
// use std::process::Command;
use tokio::runtime::Runtime;
use ttokio_postgres::NoTls;

mod embedded {
Expand All @@ -25,63 +20,26 @@ mod tokio_postgres {
embed_migrations!("refinery/tests/sql_migrations_missing");
}

fn get_migrations() -> Vec<Migration> {
let migration1 = Migration::from_filename(
"V1__initial.sql",
include_str!("./sql_migrations/V1__initial.sql"),
)
.unwrap();

let migration2 = Migration::from_filename(
"V2__add_cars_table",
include_str!("./sql_migrations/V2__add_cars_table.sql"),
)
.unwrap();

let migration3 = Migration::from_filename(
"V3__add_brand_to_cars_table",
include_str!("./sql_migrations/V3__add_brand_to_cars_table.sql"),
fn clean_database() {
let conn = ttpostgres::Connection::connect(
"postgres://postgres@localhost:5432/template1",
ttpostgres::TlsMode::None,
)
.unwrap();

let migration4 = Migration::from_filename(
"V4__add_year_field_to_cars",
&"ALTER TABLE cars ADD year INTEGER;",
conn.execute(
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='postgres'",
&[],
)
.unwrap();

vec![migration1, migration2, migration3, migration4]
}

async fn clean_database() {
let (client, connection) =
ttokio_postgres::connect("postgres://postgres@localhost:5432/template1", NoTls)
.await
.unwrap();

tokio::spawn(async move {
connection.await.unwrap();
});

client
.execute(
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='postgres'",
&[],
)
.await
.unwrap();
client.execute("DROP DATABASE postgres", &[]).await.unwrap();
client
.execute("CREATE DATABASE POSTGRES", &[])
.await
.unwrap();
conn.execute("DROP DATABASE postgres", &[]).unwrap();
conn.execute("CREATE DATABASE POSTGRES", &[]).unwrap();
}

struct TearDown;
impl Drop for TearDown {
fn drop(&mut self) {
let mut rt = Runtime::new().unwrap();
rt.block_on(clean_database());
clean_database();
}
}

Expand Down Expand Up @@ -288,7 +246,7 @@ mod tokio_postgres {
}

#[tokio::test]
async fn embedded_updates_to_last_working() {
async fn embedded_updates_to_last_working_if_not_grouped() {
TearDown {};
let (mut client, connection) =
ttokio_postgres::connect("postgres://postgres@localhost:5432/postgres", NoTls)
Expand All @@ -313,6 +271,31 @@ mod tokio_postgres {
}
}

#[tokio::test]
async fn embedded_doesnt_update_to_last_working_if_grouped() {
TearDown {};
let (mut client, connection) =
ttokio_postgres::connect("postgres://postgres@localhost:5432/postgres", NoTls)
.await
.unwrap();

tokio::spawn(async move {
connection.await.unwrap();
});

let result = broken::migrations::runner()
.set_grouped(true)
.run_async(&mut client).await;

assert!(result.is_err());

let query = client
.query("SELECT version FROM refinery_schema_history", &[])
.await
.unwrap();

assert!(query.is_empty());
}
#[tokio::test]
async fn mod_creates_migration_table() {
TearDown {};
Expand Down
1 change: 0 additions & 1 deletion refinery_cli/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ mod cli {
.assert()
.failure();
}

}
3 changes: 3 additions & 0 deletions refinery_migrations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ documentation = "https://docs.rs/refinery/"
repository = "https://github.com/rust-db/refinery"
edition = "2018"

[features]
async = []

[dependencies]
lazy_static = "1.3"
regex = "1.1"
Expand Down
25 changes: 10 additions & 15 deletions refinery_migrations/src/async_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ use chrono::Local;
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(&mut self, query: &[&str]) -> Result<usize, Self::Error>;
}

#[async_trait]
pub trait AsyncQuery<T>: AsyncTransaction {
async fn query(&mut self, query: &str) -> Result<Option<T>, Self::Error>;
}

#[async_trait]
pub trait AsyncExecuteMultiple: AsyncTransaction {
async fn execute_multiple(&mut self, queries: &[&str]) -> Result<usize, Self::Error>;
}

async fn migrate<T: AsyncExecuteMultiple>(
async fn migrate<T: AsyncTransaction>(
transaction: &mut T,
migrations: Vec<Migration>,
) -> Result<(), Error> {
Expand All @@ -31,14 +26,14 @@ async fn migrate<T: AsyncExecuteMultiple>(
"INSERT INTO refinery_schema_history (version, name, applied_on, checksum) VALUES ({}, '{}', '{}', '{}')",
migration.version, migration.name, Local::now().to_rfc3339(), migration.checksum().to_string());
transaction
.execute_multiple(&[&migration.sql, update_query])
.execute(&[&migration.sql, update_query])
.await
.migration_err(&format!("error applying migration {}", migration))?;
}
Ok(())
}

async fn migrate_grouped<T: AsyncExecuteMultiple>(
async fn migrate_grouped<T: AsyncTransaction>(
transaction: &mut T,
migrations: Vec<Migration>,
) -> Result<(), Error> {
Expand All @@ -61,15 +56,15 @@ async fn migrate_grouped<T: AsyncExecuteMultiple>(
let refs: Vec<&str> = grouped_migrations.iter().map(AsRef::as_ref).collect();

transaction
.execute_multiple(refs.as_ref())
.execute(refs.as_ref())
.await
.migration_err("error applying migrations")?;

Ok(())
}

#[async_trait]
pub trait AsyncMigrate: AsyncQuery<Vec<AppliedMigration>> + AsyncExecuteMultiple
pub trait AsyncMigrate: AsyncQuery<Vec<AppliedMigration>>
where
Self: Sized,
{
Expand All @@ -80,7 +75,7 @@ where
abort_missing: bool,
grouped: bool,
) -> Result<(), Error> {
self.execute(ASSERT_MIGRATIONS_TABLE)
self.execute(&[ASSERT_MIGRATIONS_TABLE])
.await
.migration_err("error asserting migrations table")?;

Expand All @@ -102,13 +97,13 @@ where
}

if grouped {
migrate(self, migrations).await?
} else {
migrate_grouped(self, migrations).await?
} else {
migrate(self, migrations).await?
}

Ok(())
}
}

impl<T> AsyncMigrate for T where T: AsyncQuery<Vec<AppliedMigration>> + AsyncExecuteMultiple {}
impl<T> AsyncMigrate for T where T: AsyncQuery<Vec<AppliedMigration>> {}
5 changes: 4 additions & 1 deletion refinery_migrations/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "async")]
mod async_traits;
mod config;
mod error;
Expand All @@ -10,6 +11,7 @@ use std::collections::hash_map::DefaultHasher;
use std::fmt;
use std::hash::{Hash, Hasher};

#[cfg(feature = "async")]
pub use async_traits::AsyncMigrate;
pub use config::{Config, ConfigDbType, Main};
pub use error::{Error, WrapMigrationError};
Expand All @@ -23,7 +25,7 @@ pub use utils::migrate_from_config;
#[cfg(feature = "rusqlite")]
pub mod rusqlite;

// #[cfg(feature = "tokio-postgres")]
#[cfg(feature = "tokio-postgres")]
pub mod tokio_postgres;

#[cfg(feature = "postgres")]
Expand Down Expand Up @@ -210,6 +212,7 @@ impl Runner {
}

/// Runs the Migrations asynchronously in the supplied database connection
#[cfg(feature = "async")]
pub async fn run_async<'a, C>(&self, conn: &'a mut C) -> Result<(), Error>
where
C: AsyncMigrate + Send,
Expand Down
4 changes: 2 additions & 2 deletions refinery_migrations/src/mysql.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
AppliedMigration, CommitTransaction, Error, ExecuteMultiple, Migrate,
MigrateGrouped, Query, Transaction, WrapMigrationError,
AppliedMigration, CommitTransaction, Error, ExecuteMultiple, Migrate, MigrateGrouped, Query,
Transaction, WrapMigrationError,
};
use chrono::{DateTime, Local};
use mysql::{
Expand Down
Loading

0 comments on commit 846e9a9

Please sign in to comment.