Skip to content

Commit

Permalink
- simplify traits, use only Transaction and Query traits,
Browse files Browse the repository at this point in the history
    make fn execute support more than one statement to allow this
- remove no longed needed traits and impls
  • Loading branch information
jxs committed Dec 17, 2019
1 parent f369a83 commit e00dff8
Show file tree
Hide file tree
Showing 27 changed files with 718 additions and 661 deletions.
12 changes: 6 additions & 6 deletions .circleci/config.yml
Expand Up @@ -37,7 +37,7 @@ jobs:
- checkout
- run: rustup self update
- run: cd refinery_macros && cargo test
- run: cd refinery_migrations && cargo test
- run: cd refinery_migrations && cargo test --features=sync,postgres
- run: cd refinery_cli && cargo test
cargo-clippy-and-test-macros-and-cli-stable:
docker:
Expand All @@ -51,7 +51,7 @@ jobs:
- run: RUSTFLAGS="-D warnings" cd refinery_migrations && cargo clippy
- run: RUSTFLAGS="-D warnings" cd refinery_cli && cargo clippy
- run: cd refinery_macros && cargo test
- run: cd refinery_migrations && cargo test
- run: cd refinery_migrations && cargo test --features=sync,postgres
- run: cd refinery_cli && cargo test
test-macros-and-cli-nightly:
docker:
Expand All @@ -61,7 +61,7 @@ jobs:
- run: rustup self update
- run: RUSTFLAGS="-D warnings" cd refinery && cargo build --all-features
- run: cd refinery_macros && cargo test
- run: cd refinery_migrations && cargo test
- run: cd refinery_migrations && cargo test --features=sync,postgres
- run: cd refinery_cli && cargo test
test-sqlite-previous:
docker:
Expand Down Expand Up @@ -127,7 +127,7 @@ jobs:
test-mysql-previous:
docker:
- image: << pipeline.parameters.previous >>
- image: mariadb:5.5.64-trusty
- image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: refinery
Expand All @@ -140,7 +140,7 @@ jobs:
test-mysql-stable:
docker:
- image: << pipeline.parameters.stable >>
- image: mariadb:5.5.64-trusty
- image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: refinery
Expand All @@ -153,7 +153,7 @@ jobs:
test-mysql-nightly:
docker:
- image: << pipeline.parameters.nightly >>
- image: mariadb:5.5.64-trusty
- image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: refinery
Expand Down
7 changes: 7 additions & 0 deletions example_config.toml
@@ -0,0 +1,7 @@
[main]
db_type = "Postgres"
db_host = "localhost"
db_port = "5432"
db_user = "root"
db_pass = "1234"
db_name = "refinery"
17 changes: 9 additions & 8 deletions refinery/Cargo.toml
Expand Up @@ -13,23 +13,23 @@ edition = "2018"

[features]
default = []
rusqlite = ["refinery-migrations/rusqlite", "barrel/sqlite3"]
postgres = ["refinery-migrations/postgres", "barrel/pg"]
tokio-postgres = ["refinery-migrations/async", "refinery-migrations/tokio-postgres"]
mysql = ["refinery-migrations/mysql", "barrel/mysql"]
rusqlite = ["refinery-migrations/sync", "refinery-migrations/rusqlite", "barrel/sqlite3"]
postgres = ["refinery-migrations/sync", "refinery-migrations/postgres", "barrel/pg"]
mysql = ["refinery-migrations/sync", "refinery-migrations/mysql", "barrel/mysql"]
tokio-postgres = ["refinery-migrations/async", "refinery-migrations/tokio-postgres", "barrel/pg"]

#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"]
trusqlite = ["refinery-migrations/sync", "rusqlite", "refinery-migrations/rusqlite", "mod_migrations/sqlite"]
tpostgres = ["refinery-migrations/sync", "postgres", "refinery-migrations/postgres", "mod_migrations/pg"]
tmysql = ["refinery-migrations/sync", "mysql", "refinery-migrations/mysql", "mod_migrations/mysql"]
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
# and rust 1.38 doesn't support async fn's, TODO: remove when previous version is 1.38
tokio = { version = "0.2", features = ["full"], optional = true }
ttokio_postgres = {package = "tokio-postgres", version = "0.5.0-alpha.2", optional = true }

Expand All @@ -43,6 +43,7 @@ mod_migrations = {path = "./tests/mod_migrations"}
chrono = "0.4.6"
assert_cmd = "0.11.1"
predicates = "1.0.1"
tempfile = "3.1.0"

# [package.metadata.docs.rs]
# features = ["postgres", "mysql", "sqlite", "extras"]
Expand Down
12 changes: 9 additions & 3 deletions refinery/src/lib.rs
Expand Up @@ -31,10 +31,16 @@ for more examples refer to the [`examples`](https://github.com/rust-db/refinery/
*/

pub use refinery_macros::{embed_migrations, include_migration_mods};
pub use refinery_migrations::{Config, ConfigDbType, Migration, Runner};
#[doc(hidden)]
pub use refinery_migrations::{Error, Migrate, MigrateGrouped};
pub use refinery_migrations::Error;
pub use refinery_migrations::{Config, ConfigDbType, Migration, Runner};

#[cfg(any(feature = "mysql", feature = "postgres", feature = "rusqlite"))]
pub use refinery_migrations::Migrate;

#[cfg(any(feature = "tokio-postgres"))]
pub use refinery_migrations::AsyncMigrate;

#[doc(hidden)]
#[cfg(all(feature = "mysql", feature = "postgres", feature = "rusqlite"))]
#[cfg(any(feature = "mysql", feature = "postgres", feature = "rusqlite"))]
pub use refinery_migrations::migrate_from_config;
64 changes: 48 additions & 16 deletions refinery/tests/mysql.rs
Expand Up @@ -2,7 +2,8 @@ mod mysql {
use assert_cmd::prelude::*;
use chrono::{DateTime, Local};
use predicates::str::contains;
use refinery::{migrate_from_config, Config, ConfigDbType, Error, Migrate as _, Migration};
use refinery::{migrate_from_config, Error, Migrate as _, Migration};
use std::io::Write;
use std::process::Command;
use ttmysql as my;

Expand Down Expand Up @@ -86,7 +87,7 @@ mod mysql {
}

#[test]
fn embedded_creates_migration_table_single_transaction() {
fn embedded_creates_migration_table_grouped_transaction() {
run_test(|| {
let pool = my::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();
Expand Down Expand Up @@ -130,7 +131,7 @@ mod mysql {
}

#[test]
fn embedded_applies_migration_single_transaction() {
fn embedded_applies_migration_grouped_transaction() {
run_test(|| {
let pool = my::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();
Expand Down Expand Up @@ -185,7 +186,7 @@ mod mysql {
}

#[test]
fn embedded_updates_schema_history_single_transaction() {
fn embedded_updates_schema_history_grouped_transaction() {
run_test(|| {
let pool = my::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();
Expand Down Expand Up @@ -217,7 +218,7 @@ mod mysql {
}

#[test]
fn embedded_updates_to_last_working_in_multiple_transaction() {
fn embedded_updates_to_last_working_if_not_grouped() {
run_test(|| {
let pool = my::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();
Expand All @@ -237,6 +238,29 @@ mod mysql {
});
}

/// maintain this test still here for self referencing purposes, Mysql doesn't support well transactions
/// TODO: maybe uncomment it one day when MySQL does :D
// #[test]
// fn embedded_doesnt_update_to_last_working_if_grouped_transaction() {
// // run_test(|| {
// let pool = my::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
// let mut conn = pool.get_conn().unwrap();

// let result = broken::migrations::runner().set_grouped(true).run(&mut conn);

// assert!(result.is_err());

// let mut query = conn
// .query("SELECT version FROM refinery_schema_history")
// .unwrap();
// let row = query.next();
// dbg!(&row);
// assert!(row.is_none());
// // let value: Option<i32> = row.get(0);
// // assert_eq!(0, value.unwrap());
// // });
// }

#[test]
fn mod_creates_migration_table() {
run_test(|| {
Expand Down Expand Up @@ -316,7 +340,7 @@ mod mysql {
let migrations = get_migrations();

let mchecksum = migrations[3].checksum();
conn.migrate(&migrations, true, true).unwrap();
conn.migrate(&migrations, true, true, false).unwrap();

for _row in conn
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
Expand Down Expand Up @@ -344,7 +368,9 @@ mod mysql {
&"ALTER TABLE cars ADD year INTEGER;",
)
.unwrap();
let err = conn.migrate(&[migration.clone()], true, true).unwrap_err();
let err = conn
.migrate(&[migration.clone()], true, true, false)
.unwrap_err();

match err {
Error::MissingVersion(missing) => {
Expand All @@ -369,7 +395,9 @@ mod mysql {
&"ALTER TABLE cars ADD year INTEGER;",
)
.unwrap();
let err = conn.migrate(&[migration.clone()], true, false).unwrap_err();
let err = conn
.migrate(&[migration.clone()], true, false, false)
.unwrap_err();

match err {
Error::DivergentVersion(applied, divergent) => {
Expand Down Expand Up @@ -408,7 +436,7 @@ mod mysql {
)
.unwrap();
let err = conn
.migrate(&[migration1, migration2], true, true)
.migrate(&[migration1, migration2], true, true, false)
.unwrap_err();
match err {
Error::MissingVersion(missing) => {
Expand All @@ -423,15 +451,19 @@ mod mysql {
#[test]
fn migrates_from_config() {
run_test(|| {
let config = Config::new(ConfigDbType::Mysql)
.set_db_name("refinery_test")
.set_db_user("refinery")
.set_db_pass("root")
.set_db_host("localhost")
.set_db_port("3306");
let config = "[main] \n
db_type = \"Mysql\" \n
db_name = \"refinery_test\" \n
db_user = \"refinery\" \n
db_pass= \"root\" \n
db_host = \"localhost\" \n
db_port = \"3306\" ";

let mut config_file = tempfile::NamedTempFile::new_in(".").unwrap();
config_file.write_all(config.as_bytes()).unwrap();

let migrations = get_migrations();
migrate_from_config(&config, false, true, true, &migrations).unwrap();
migrate_from_config(config_file.path(), false, true, true, &migrations).unwrap();
})
}

Expand Down
56 changes: 44 additions & 12 deletions refinery/tests/postgres.rs
Expand Up @@ -2,7 +2,8 @@ mod postgres {
use assert_cmd::prelude::*;
use chrono::{DateTime, Local};
use predicates::str::contains;
use refinery::{migrate_from_config, Config, ConfigDbType, Error, Migrate as _, Migration};
use refinery::{migrate_from_config, Error, Migrate as _, Migration};
use std::io::Write;
use std::process::Command;
use ttpostgres::{Connection, TlsMode};

Expand Down Expand Up @@ -103,7 +104,7 @@ mod postgres {
.unwrap();

embedded::migrations::runner()
.set_grouped(false)
.set_grouped(true)
.run(&mut conn)
.unwrap();

Expand Down Expand Up @@ -226,7 +227,7 @@ mod postgres {
}

#[test]
fn embedded_updates_to_last_working_in_multiple_transaction() {
fn embedded_updates_to_last_working_if_not_grouped() {
run_test(|| {
let mut conn =
Connection::connect("postgres://postgres@localhost:5432/postgres", TlsMode::None)
Expand All @@ -247,6 +248,27 @@ mod postgres {
});
}

#[test]
fn embedded_doesnt_update_to_last_working_if_grouped() {
run_test(|| {
let mut conn =
Connection::connect("postgres://postgres@localhost:5432/postgres", TlsMode::None)
.unwrap();

let result = broken::migrations::runner()
.set_grouped(true)
.run(&mut conn);

assert!(result.is_err());
println!("CURRENT: {:?}", result);

let query = &conn
.query("SELECT version FROM refinery_schema_history", &[])
.unwrap();
assert!(query.is_empty());
});
}

#[test]
fn mod_creates_migration_table() {
run_test(|| {
Expand Down Expand Up @@ -351,6 +373,7 @@ mod postgres {
&[migration1, migration2, migration3, migration4],
true,
true,
false,
)
.unwrap();

Expand Down Expand Up @@ -380,7 +403,9 @@ mod postgres {
&"ALTER TABLE cars ADD year INTEGER;",
)
.unwrap();
let err = conn.migrate(&[migration.clone()], true, true).unwrap_err();
let err = conn
.migrate(&[migration.clone()], true, true, false)
.unwrap_err();

match err {
Error::MissingVersion(missing) => {
Expand All @@ -406,7 +431,9 @@ mod postgres {
&"ALTER TABLE cars ADD year INTEGER;",
)
.unwrap();
let err = conn.migrate(&[migration.clone()], true, false).unwrap_err();
let err = conn
.migrate(&[migration.clone()], true, false, false)
.unwrap_err();

match err {
Error::DivergentVersion(applied, divergent) => {
Expand Down Expand Up @@ -446,7 +473,7 @@ mod postgres {
)
.unwrap();
let err = conn
.migrate(&[migration1, migration2], true, true)
.migrate(&[migration1, migration2], true, true, false)
.unwrap_err();
match err {
Error::MissingVersion(missing) => {
Expand All @@ -461,13 +488,18 @@ mod postgres {
#[test]
fn migrates_from_config() {
run_test(|| {
let config = Config::new(ConfigDbType::Postgres)
.set_db_name("postgres")
.set_db_user("postgres")
.set_db_host("localhost")
.set_db_port("5432");
let config = "[main] \n
db_type = \"Postgres\" \n
db_name = \"postgres\" \n
db_user = \"postgres\" \n
db_host = \"localhost\" \n
db_port = \"5432\" ";

let mut config_file = tempfile::NamedTempFile::new_in(".").unwrap();
config_file.write_all(config.as_bytes()).unwrap();

let migrations = get_migrations();
migrate_from_config(&config, false, true, true, &migrations).unwrap();
migrate_from_config(&config_file.path(), false, true, true, &migrations).unwrap();
})
}

Expand Down

0 comments on commit e00dff8

Please sign in to comment.