Skip to content

Commit

Permalink
add Target enum and allow Runner to a Target migration
Browse files Browse the repository at this point in the history
closes #60
  • Loading branch information
jxs committed May 9, 2020
1 parent 0102c06 commit b801ba3
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 62 deletions.
4 changes: 3 additions & 1 deletion refinery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ for more examples refer to the [examples](https://github.com/rust-db/refinery/tr
*/

pub use refinery_core::config;
pub use refinery_core::{AppliedMigration, AsyncMigrate, Error, Migrate, Migration, Runner};
#[doc(hidden)]
pub use refinery_core::{AppliedMigration, AsyncMigrate, Migrate};
pub use refinery_core::{Error, Migration, Runner, Target};
pub use refinery_macros::{embed_migrations, include_migration_mods};
4 changes: 1 addition & 3 deletions refinery/tests/mod_migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub mod migrations {
refinery::include_migration_mods!("./tests/mod_migrations");
}
refinery::include_migration_mods!("./tests/mod_migrations");
70 changes: 60 additions & 10 deletions refinery/tests/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod mysql {
use predicates::str::contains;
use refinery::{
config::{migrate_from_config, Config, ConfigDbType},
Error, Migrate, Migration,
Error, Migrate, Migration, Target,
};
use refinery_core::mysql;
use std::process::Command;
Expand Down Expand Up @@ -288,7 +288,7 @@ mod mysql {
let pool =
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();
mod_migrations::migrations::runner().run(&mut conn).unwrap();
mod_migrations::runner().run(&mut conn).unwrap();
for row in conn
.query(
"SELECT table_name FROM information_schema.tables WHERE table_name='refinery_schema_history'"
Expand All @@ -308,7 +308,7 @@ mod mysql {
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();

mod_migrations::migrations::runner().run(&mut conn).unwrap();
mod_migrations::runner().run(&mut conn).unwrap();
conn.prep_exec(
"INSERT INTO persons (name, city) VALUES (:a, :b)",
(&"John Legend", &"New York"),
Expand All @@ -331,7 +331,7 @@ mod mysql {
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();

mod_migrations::migrations::runner().run(&mut conn).unwrap();
mod_migrations::runner().run(&mut conn).unwrap();

for _row in conn
.query("SELECT MAX(version) FROM refinery_schema_history")
Expand Down Expand Up @@ -365,7 +365,8 @@ mod mysql {
let migrations = get_migrations();

let mchecksum = migrations[4].checksum();
conn.migrate(&migrations, true, true, false).unwrap();
conn.migrate(&migrations, true, true, false, Target::Latest)
.unwrap();

for _row in conn
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
Expand All @@ -380,21 +381,70 @@ mod mysql {
});
}

#[test]
fn migrates_to_target_migration() {
run_test(|| {
let pool =
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();

embedded::migrations::runner()
.set_target(Target::Version(3))
.run(&mut conn)
.unwrap();

for _row in conn
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
.unwrap()
{
let row = _row.unwrap();
let current: i32 = row.get(0).unwrap();
assert_eq!(3, current);
}
});
}

#[test]
fn migrates_to_target_migration_grouped() {
run_test(|| {
let pool =
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();

embedded::migrations::runner()
.set_target(Target::Version(3))
.set_grouped(true)
.run(&mut conn)
.unwrap();

for _row in conn
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
.unwrap()
{
let row = _row.unwrap();
let current: i32 = row.get(0).unwrap();
assert_eq!(3, current);
}
});
}

#[test]
fn aborts_on_missing_migration_on_filesystem() {
run_test(|| {
let pool =
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();

mod_migrations::migrations::runner().run(&mut conn).unwrap();
mod_migrations::runner().run(&mut conn).unwrap();

let migration = Migration::from_filename(
"V4__add_year_field_to_cars",
&"ALTER TABLE cars ADD year INTEGER;",
)
.unwrap();
let err = conn.migrate(&[migration], true, true, false).unwrap_err();
let err = conn
.migrate(&[migration], true, true, false, Target::Latest)
.unwrap_err();

match err {
Error::MissingVersion(missing) => {
Expand All @@ -413,15 +463,15 @@ mod mysql {
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
let mut conn = pool.get_conn().unwrap();

mod_migrations::migrations::runner().run(&mut conn).unwrap();
mod_migrations::runner().run(&mut conn).unwrap();

let migration = Migration::from_filename(
"V2__add_year_field_to_cars",
&"ALTER TABLE cars ADD year INTEGER;",
)
.unwrap();
let err = conn
.migrate(&[migration.clone()], true, false, false)
.migrate(&[migration.clone()], true, false, false, Target::Latest)
.unwrap_err();

match err {
Expand Down Expand Up @@ -462,7 +512,7 @@ mod mysql {
)
.unwrap();
let err = conn
.migrate(&[migration1, migration2], true, true, false)
.migrate(&[migration1, migration2], true, true, false, Target::Latest)
.unwrap_err();
match err {
Error::MissingVersion(missing) => {
Expand Down
75 changes: 64 additions & 11 deletions refinery/tests/mysql_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod mysql_async {
use futures::FutureExt;
use refinery::{
config::{migrate_from_config_async, Config, ConfigDbType},
AsyncMigrate, Error, Migration,
AsyncMigrate, Error, Migration, Target,
};
use refinery_core::mysql_async::prelude::Queryable;
use refinery_core::{mysql_async, tokio};
Expand Down Expand Up @@ -333,7 +333,7 @@ mod mysql_async {
let mut pool = mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
let conn = pool.get_conn().await.unwrap();

mod_migrations::migrations::runner()
mod_migrations::runner()
.run_async(&mut pool)
.await
.unwrap();
Expand All @@ -360,7 +360,7 @@ mod mysql_async {
mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
let mut conn = pool.get_conn().await.unwrap();

mod_migrations::migrations::runner()
mod_migrations::runner()
.run_async(&mut pool)
.await
.unwrap();
Expand Down Expand Up @@ -399,7 +399,7 @@ mod mysql_async {
let mut pool = mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
let conn = pool.get_conn().await.unwrap();

mod_migrations::migrations::runner()
mod_migrations::runner()
.run_async(&mut pool)
.await
.unwrap();
Expand Down Expand Up @@ -449,7 +449,7 @@ mod mysql_async {
let migrations = get_migrations();

let mchecksum = migrations[4].checksum();
pool.migrate(&migrations, true, true, false).await.unwrap();
pool.migrate(&migrations, true, true, false, Target::Latest).await.unwrap();

conn
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
Expand All @@ -468,13 +468,66 @@ mod mysql_async {
}).await;
}

#[tokio::test]
async fn migrates_to_target_migration() {
run_test(async {
let mut pool = mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
let conn = pool.get_conn().await.unwrap();

embedded::migrations::runner()
.set_grouped(true)
.set_target(Target::Version(3))
.run_async(&mut pool)
.await
.unwrap();

conn
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
.await
.unwrap()
.for_each(|row| {
let current: i32 = row.get(0).unwrap();
assert_eq!(3, current);
})
.await
.unwrap();

}).await;
}

#[tokio::test]
async fn migrates_to_target_migration_grouped() {
run_test(async {
let mut pool = mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
let conn = pool.get_conn().await.unwrap();

embedded::migrations::runner()
.set_target(Target::Version(3))
.run_async(&mut pool)
.await
.unwrap();

conn
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
.await
.unwrap()
.for_each(|row| {
let current: i32 = row.get(0).unwrap();
assert_eq!(3, current);
})
.await
.unwrap();

}).await;
}

#[tokio::test]
async fn aborts_on_missing_migration_on_filesystem() {
run_test(async {
let mut pool =
mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");

mod_migrations::migrations::runner()
mod_migrations::runner()
.run_async(&mut pool)
.await
.unwrap();
Expand All @@ -486,7 +539,7 @@ mod mysql_async {
.unwrap();

let err = pool
.migrate(&[migration.clone()], true, true, false)
.migrate(&[migration.clone()], true, true, false, Target::Latest)
.await
.unwrap_err();

Expand All @@ -507,12 +560,12 @@ mod mysql_async {
let mut pool =
mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");

mod_migrations::migrations::runner()
mod_migrations::runner()
.run_async(&mut pool)
.await
.unwrap();

mod_migrations::migrations::runner()
mod_migrations::runner()
.run_async(&mut pool)
.await
.unwrap();
Expand All @@ -524,7 +577,7 @@ mod mysql_async {
.unwrap();

let err = pool
.migrate(&[migration.clone()], true, false, false)
.migrate(&[migration.clone()], true, false, false, Target::Latest)
.await
.unwrap_err();

Expand Down Expand Up @@ -569,7 +622,7 @@ mod mysql_async {
)
.unwrap();
let err = pool
.migrate(&[migration1, migration2], true, true, false)
.migrate(&[migration1, migration2], true, true, false, Target::Latest)
.await
.unwrap_err();

Expand Down
Loading

0 comments on commit b801ba3

Please sign in to comment.