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 Mar 25, 2020
1 parent 38c0290 commit 97b799a
Show file tree
Hide file tree
Showing 13 changed files with 355 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ orbs:
parameters:
stable:
type: string
default: "1.41"
default: "1.42"
previous:
type: string
default: "1.40"
default: "1.41"
nightly:
type: string
default: "rustlang/rust:nightly"
Expand Down
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};
60 changes: 55 additions & 5 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 @@ -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,6 +381,53 @@ 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(|| {
Expand All @@ -394,7 +442,9 @@ mod mysql {
&"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 @@ -421,7 +471,7 @@ mod mysql {
)
.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
63 changes: 58 additions & 5 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 @@ -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,6 +468,59 @@ 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 {
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 Down Expand Up @@ -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
57 changes: 52 additions & 5 deletions refinery/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod postgres {
use predicates::str::contains;
use refinery::{
config::{migrate_from_config, Config, ConfigDbType},
Error, Migrate, Migration,
Error, Migrate, Migration, Target,
};
use refinery_core::postgres::{Client, NoTls};
use std::process::Command;
Expand Down Expand Up @@ -354,7 +354,9 @@ mod postgres {
let migrations = get_migrations();

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

for row in &client
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)", &[])
Expand All @@ -368,6 +370,49 @@ mod postgres {
});
}

#[test]
fn migrates_to_target_migration() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();

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

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

#[test]
fn migrates_to_target_migration_grouped() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();

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

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

#[test]
fn aborts_on_missing_migration_on_filesystem() {
run_test(|| {
Expand All @@ -383,7 +428,9 @@ mod postgres {
&"ALTER TABLE cars ADD year INTEGER;",
)
.unwrap();
let err = client.migrate(&[migration], true, true, false).unwrap_err();
let err = client
.migrate(&[migration], true, true, false, Target::Latest)
.unwrap_err();

match err {
Error::MissingVersion(missing) => {
Expand Down Expand Up @@ -411,7 +458,7 @@ mod postgres {
)
.unwrap();
let err = client
.migrate(&[migration.clone()], true, false, false)
.migrate(&[migration.clone()], true, false, false, Target::Latest)
.unwrap_err();

match err {
Expand Down Expand Up @@ -451,7 +498,7 @@ mod postgres {
)
.unwrap();
let err = client
.migrate(&[migration1, migration2], true, true, false)
.migrate(&[migration1, migration2], true, true, false, Target::Latest)
.unwrap_err();
match err {
Error::MissingVersion(missing) => {
Expand Down
Loading

0 comments on commit 97b799a

Please sign in to comment.