Skip to content

Commit 0e5eee8

Browse files
committed
add Target enum and allow Runner to a Target migration
closes #60
1 parent 0102c06 commit 0e5eee8

File tree

12 files changed

+378
-83
lines changed

12 files changed

+378
-83
lines changed

refinery/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ for more examples refer to the [examples](https://github.com/rust-db/refinery/tr
3333
*/
3434

3535
pub use refinery_core::config;
36-
pub use refinery_core::{AppliedMigration, AsyncMigrate, Error, Migrate, Migration, Runner};
36+
#[doc(hidden)]
37+
pub use refinery_core::{AppliedMigration, AsyncMigrate, Migrate};
38+
pub use refinery_core::{Error, Migration, Runner, Target};
3739
pub use refinery_macros::{embed_migrations, include_migration_mods};

refinery/tests/mod_migrations/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub mod migrations {
2-
refinery::include_migration_mods!("./tests/mod_migrations");
3-
}
1+
refinery::include_migration_mods!("./tests/mod_migrations");

refinery/tests/mysql.rs

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod mysql {
99
use predicates::str::contains;
1010
use refinery::{
1111
config::{migrate_from_config, Config, ConfigDbType},
12-
Error, Migrate, Migration,
12+
Error, Migrate, Migration, Target,
1313
};
1414
use refinery_core::mysql;
1515
use std::process::Command;
@@ -288,7 +288,7 @@ mod mysql {
288288
let pool =
289289
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
290290
let mut conn = pool.get_conn().unwrap();
291-
mod_migrations::migrations::runner().run(&mut conn).unwrap();
291+
mod_migrations::runner().run(&mut conn).unwrap();
292292
for row in conn
293293
.query(
294294
"SELECT table_name FROM information_schema.tables WHERE table_name='refinery_schema_history'"
@@ -308,7 +308,7 @@ mod mysql {
308308
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
309309
let mut conn = pool.get_conn().unwrap();
310310

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

334-
mod_migrations::migrations::runner().run(&mut conn).unwrap();
334+
mod_migrations::runner().run(&mut conn).unwrap();
335335

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

367367
let mchecksum = migrations[4].checksum();
368-
conn.migrate(&migrations, true, true, false).unwrap();
368+
conn.migrate(&migrations, true, true, false, Target::Latest)
369+
.unwrap();
369370

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

384+
#[test]
385+
fn migrates_to_target_migration() {
386+
run_test(|| {
387+
let pool =
388+
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
389+
let mut conn = pool.get_conn().unwrap();
390+
391+
embedded::migrations::runner()
392+
.set_target(Target::Version(3))
393+
.run(&mut conn)
394+
.unwrap();
395+
396+
for _row in conn
397+
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
398+
.unwrap()
399+
{
400+
let row = _row.unwrap();
401+
let current: i32 = row.get(0).unwrap();
402+
assert_eq!(3, current);
403+
}
404+
});
405+
}
406+
407+
#[test]
408+
fn migrates_to_target_migration_grouped() {
409+
run_test(|| {
410+
let pool =
411+
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
412+
let mut conn = pool.get_conn().unwrap();
413+
414+
embedded::migrations::runner()
415+
.set_target(Target::Version(3))
416+
.set_grouped(true)
417+
.run(&mut conn)
418+
.unwrap();
419+
420+
for _row in conn
421+
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
422+
.unwrap()
423+
{
424+
let row = _row.unwrap();
425+
let current: i32 = row.get(0).unwrap();
426+
assert_eq!(3, current);
427+
}
428+
});
429+
}
430+
383431
#[test]
384432
fn aborts_on_missing_migration_on_filesystem() {
385433
run_test(|| {
386434
let pool =
387435
mysql::Pool::new("mysql://refinery:root@localhost:3306/refinery_test").unwrap();
388436
let mut conn = pool.get_conn().unwrap();
389437

390-
mod_migrations::migrations::runner().run(&mut conn).unwrap();
438+
mod_migrations::runner().run(&mut conn).unwrap();
391439

392440
let migration = Migration::from_filename(
393441
"V4__add_year_field_to_cars",
394442
&"ALTER TABLE cars ADD year INTEGER;",
395443
)
396444
.unwrap();
397-
let err = conn.migrate(&[migration], true, true, false).unwrap_err();
445+
let err = conn
446+
.migrate(&[migration], true, true, false, Target::Latest)
447+
.unwrap_err();
398448

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

416-
mod_migrations::migrations::runner().run(&mut conn).unwrap();
466+
mod_migrations::runner().run(&mut conn).unwrap();
417467

418468
let migration = Migration::from_filename(
419469
"V2__add_year_field_to_cars",
420470
&"ALTER TABLE cars ADD year INTEGER;",
421471
)
422472
.unwrap();
423473
let err = conn
424-
.migrate(&[migration.clone()], true, false, false)
474+
.migrate(&[migration.clone()], true, false, false, Target::Latest)
425475
.unwrap_err();
426476

427477
match err {
@@ -462,7 +512,7 @@ mod mysql {
462512
)
463513
.unwrap();
464514
let err = conn
465-
.migrate(&[migration1, migration2], true, true, false)
515+
.migrate(&[migration1, migration2], true, true, false, Target::Latest)
466516
.unwrap_err();
467517
match err {
468518
Error::MissingVersion(missing) => {

refinery/tests/mysql_async.rs

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod mysql_async {
88
use futures::FutureExt;
99
use refinery::{
1010
config::{migrate_from_config_async, Config, ConfigDbType},
11-
AsyncMigrate, Error, Migration,
11+
AsyncMigrate, Error, Migration, Target,
1212
};
1313
use refinery_core::mysql_async::prelude::Queryable;
1414
use refinery_core::{mysql_async, tokio};
@@ -333,7 +333,7 @@ mod mysql_async {
333333
let mut pool = mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
334334
let conn = pool.get_conn().await.unwrap();
335335

336-
mod_migrations::migrations::runner()
336+
mod_migrations::runner()
337337
.run_async(&mut pool)
338338
.await
339339
.unwrap();
@@ -360,10 +360,7 @@ mod mysql_async {
360360
mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
361361
let mut conn = pool.get_conn().await.unwrap();
362362

363-
mod_migrations::migrations::runner()
364-
.run_async(&mut pool)
365-
.await
366-
.unwrap();
363+
mod_migrations::runner().run_async(&mut pool).await.unwrap();
367364

368365
conn = conn
369366
.query("INSERT INTO persons (name, city) VALUES ('John Legend', 'New York')")
@@ -399,7 +396,7 @@ mod mysql_async {
399396
let mut pool = mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
400397
let conn = pool.get_conn().await.unwrap();
401398

402-
mod_migrations::migrations::runner()
399+
mod_migrations::runner()
403400
.run_async(&mut pool)
404401
.await
405402
.unwrap();
@@ -449,7 +446,7 @@ mod mysql_async {
449446
let migrations = get_migrations();
450447

451448
let mchecksum = migrations[4].checksum();
452-
pool.migrate(&migrations, true, true, false).await.unwrap();
449+
pool.migrate(&migrations, true, true, false, Target::Latest).await.unwrap();
453450

454451
conn
455452
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
@@ -469,24 +466,74 @@ mod mysql_async {
469466
}
470467

471468
#[tokio::test]
472-
async fn aborts_on_missing_migration_on_filesystem() {
469+
async fn migrates_to_target_migration() {
473470
run_test(async {
474-
let mut pool =
475-
mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
471+
let mut pool = mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
472+
let conn = pool.get_conn().await.unwrap();
476473

477-
mod_migrations::migrations::runner()
474+
embedded::migrations::runner()
475+
.set_grouped(true)
476+
.set_target(Target::Version(3))
478477
.run_async(&mut pool)
479478
.await
480479
.unwrap();
481480

481+
conn
482+
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
483+
.await
484+
.unwrap()
485+
.for_each(|row| {
486+
let current: i32 = row.get(0).unwrap();
487+
assert_eq!(3, current);
488+
})
489+
.await
490+
.unwrap();
491+
492+
}).await;
493+
}
494+
495+
#[tokio::test]
496+
async fn migrates_to_target_migration_grouped() {
497+
run_test(async {
498+
let mut pool = mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
499+
let conn = pool.get_conn().await.unwrap();
500+
501+
embedded::migrations::runner()
502+
.set_target(Target::Version(3))
503+
.run_async(&mut pool)
504+
.await
505+
.unwrap();
506+
507+
conn
508+
.query("SELECT version, checksum FROM refinery_schema_history where version = (SELECT MAX(version) from refinery_schema_history)")
509+
.await
510+
.unwrap()
511+
.for_each(|row| {
512+
let current: i32 = row.get(0).unwrap();
513+
assert_eq!(3, current);
514+
})
515+
.await
516+
.unwrap();
517+
518+
}).await;
519+
}
520+
521+
#[tokio::test]
522+
async fn aborts_on_missing_migration_on_filesystem() {
523+
run_test(async {
524+
let mut pool =
525+
mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
526+
527+
mod_migrations::runner().run_async(&mut pool).await.unwrap();
528+
482529
let migration = Migration::from_filename(
483530
"V4__add_year_field_to_cars",
484531
&"ALTER TABLE cars ADD year INTEGER;",
485532
)
486533
.unwrap();
487534

488535
let err = pool
489-
.migrate(&[migration.clone()], true, true, false)
536+
.migrate(&[migration.clone()], true, true, false, Target::Latest)
490537
.await
491538
.unwrap_err();
492539

@@ -507,15 +554,9 @@ mod mysql_async {
507554
let mut pool =
508555
mysql_async::Pool::new("mysql://refinery:root@localhost:3306/refinery_test");
509556

510-
mod_migrations::migrations::runner()
511-
.run_async(&mut pool)
512-
.await
513-
.unwrap();
557+
mod_migrations::runner().run_async(&mut pool).await.unwrap();
514558

515-
mod_migrations::migrations::runner()
516-
.run_async(&mut pool)
517-
.await
518-
.unwrap();
559+
mod_migrations::runner().run_async(&mut pool).await.unwrap();
519560

520561
let migration = Migration::from_filename(
521562
"V2__add_year_field_to_cars",
@@ -524,7 +565,7 @@ mod mysql_async {
524565
.unwrap();
525566

526567
let err = pool
527-
.migrate(&[migration.clone()], true, false, false)
568+
.migrate(&[migration.clone()], true, false, false, Target::Latest)
528569
.await
529570
.unwrap_err();
530571

@@ -569,7 +610,7 @@ mod mysql_async {
569610
)
570611
.unwrap();
571612
let err = pool
572-
.migrate(&[migration1, migration2], true, true, false)
613+
.migrate(&[migration1, migration2], true, true, false, Target::Latest)
573614
.await
574615
.unwrap_err();
575616

0 commit comments

Comments
 (0)