Skip to content

Commit

Permalink
general: introduce Fake migrations,
Browse files Browse the repository at this point in the history
that allow to only insert and update refinery schema history table.
Add fake migrations to refinery_cli
Add Target migrations to refinery_cli
  • Loading branch information
jxs committed Sep 19, 2021
1 parent 273eb2f commit 74b7063
Show file tree
Hide file tree
Showing 11 changed files with 523 additions and 29 deletions.
64 changes: 64 additions & 0 deletions refinery/tests/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,70 @@ mod mysql {
})
}

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

let report = embedded::migrations::runner()
.set_target(Target::Fake)
.run(&mut conn)
.unwrap();

let applied_migrations = report.applied_migrations();

assert!(applied_migrations.is_empty());

let current = conn.get_last_applied_migration().unwrap().unwrap();
let migrations = get_migrations();
let mchecksum = migrations[3].checksum();

assert_eq!(4, current.version());
assert_eq!(mchecksum, current.checksum());

let mut row =
"SELECT table_name FROM information_schema.tables WHERE table_name='persons'"
.run(conn)
.unwrap();
assert!(row.next().is_none());
});
}

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

let report = embedded::migrations::runner()
.set_target(Target::FakeVersion(2))
.run(&mut conn)
.unwrap();

let applied_migrations = report.applied_migrations();

assert!(applied_migrations.is_empty());

let current = conn.get_last_applied_migration().unwrap().unwrap();
let migrations = get_migrations();
let mchecksum = migrations[1].checksum();

assert_eq!(2, current.version());
assert_eq!(mchecksum, current.checksum());

let mut row =
"SELECT table_name FROM information_schema.tables WHERE table_name='persons'"
.run(conn)
.unwrap();
assert!(row.next().is_none());
});
}

#[test]
fn migrates_from_cli() {
// cli only finds .sql migration files
Expand Down
70 changes: 70 additions & 0 deletions refinery/tests/mysql_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,74 @@ mod mysql_async {
})
.await;
}

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

let report = embedded::migrations::runner()
.set_target(Target::Fake)
.run_async(&mut pool)
.await
.unwrap();

let applied_migrations = report.applied_migrations();
assert!(applied_migrations.is_empty());

let current = pool.get_last_applied_migration().await.unwrap().unwrap();
let migrations = get_migrations();
let mchecksum = migrations[3].checksum();

assert_eq!(4, current.version());
assert_eq!(mchecksum, current.checksum());

let row: Vec<String> = conn
.query(
"SELECT table_name FROM information_schema.tables WHERE table_name='persons'",
)
.await
.unwrap();

assert!(row.is_empty());
})
.await;
}

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

let report = embedded::migrations::runner()
.set_target(Target::FakeVersion(2))
.run_async(&mut pool)
.await
.unwrap();

let applied_migrations = report.applied_migrations();
assert!(applied_migrations.is_empty());

let current = pool.get_last_applied_migration().await.unwrap().unwrap();
let migrations = get_migrations();
let mchecksum = migrations[1].checksum();

assert_eq!(2, current.version());
assert_eq!(mchecksum, current.checksum());

let row: Vec<String> = conn
.query(
"SELECT table_name FROM information_schema.tables WHERE table_name='persons'",
)
.await
.unwrap();

assert!(row.is_empty());
})
.await;
}
}
64 changes: 64 additions & 0 deletions refinery/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,70 @@ mod postgres {
})
}

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

let report = embedded::migrations::runner()
.set_target(Target::Fake)
.run(&mut client)
.unwrap();

let applied_migrations = report.applied_migrations();

assert!(applied_migrations.is_empty());

let current = client.get_last_applied_migration().unwrap().unwrap();
let migrations = get_migrations();
let mchecksum = migrations[3].checksum();

assert_eq!(4, current.version());
assert_eq!(mchecksum, current.checksum());

let row = &client
.query(
"SELECT table_name FROM information_schema.tables WHERE table_name='persons'",
&[],
)
.unwrap();
assert!(row.is_empty());
});
}

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

let report = embedded::migrations::runner()
.set_target(Target::FakeVersion(2))
.run(&mut client)
.unwrap();

let applied_migrations = report.applied_migrations();

assert!(applied_migrations.is_empty());

let current = client.get_last_applied_migration().unwrap().unwrap();
let migrations = get_migrations();
let mchecksum = migrations[1].checksum();

assert_eq!(2, current.version());
assert_eq!(mchecksum, current.checksum());

let row = &client
.query(
"SELECT table_name FROM information_schema.tables WHERE table_name='persons'",
&[],
)
.unwrap();
assert!(row.is_empty());
});
}

#[test]
fn migrates_from_cli() {
run_test(|| {
Expand Down
75 changes: 67 additions & 8 deletions refinery/tests/rusqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod rusqlite {
error::Kind,
Migrate, Migration, Runner, Target,
};
use refinery_core::rusqlite::Error;
use refinery_core::rusqlite::{Connection, OptionalExtension};
use std::fs::{self, File};
use std::process::Command;
Expand Down Expand Up @@ -104,7 +105,7 @@ mod rusqlite {
}

#[test]
fn embedded_creates_migration_table() {
fn creates_migration_table() {
let mut conn = Connection::open_in_memory().unwrap();
embedded::migrations::runner().run(&mut conn).unwrap();
let table_name: String = conn
Expand All @@ -118,7 +119,7 @@ mod rusqlite {
}

#[test]
fn embedded_creates_migration_table_grouped_transaction() {
fn creates_migration_table_grouped_transaction() {
let mut conn = Connection::open_in_memory().unwrap();
embedded::migrations::runner()
.set_grouped(true)
Expand All @@ -135,7 +136,7 @@ mod rusqlite {
}

#[test]
fn embedded_applies_migration() {
fn applies_migration() {
let mut conn = Connection::open_in_memory().unwrap();

embedded::migrations::runner().run(&mut conn).unwrap();
Expand All @@ -155,7 +156,7 @@ mod rusqlite {
}

#[test]
fn embedded_applies_migration_grouped_transaction() {
fn applies_migration_grouped_transaction() {
let mut conn = Connection::open_in_memory().unwrap();

embedded::migrations::runner()
Expand All @@ -178,7 +179,7 @@ mod rusqlite {
}

#[test]
fn embedded_updates_schema_history() {
fn updates_schema_history() {
let mut conn = Connection::open_in_memory().unwrap();

embedded::migrations::runner().run(&mut conn).unwrap();
Expand All @@ -191,7 +192,7 @@ mod rusqlite {
}

#[test]
fn embedded_updates_schema_history_grouped_transaction() {
fn updates_schema_history_grouped_transaction() {
let mut conn = Connection::open_in_memory().unwrap();

embedded::migrations::runner()
Expand All @@ -207,7 +208,7 @@ mod rusqlite {
}

#[test]
fn embedded_updates_to_last_working_if_not_grouped() {
fn updates_to_last_working_if_not_grouped() {
let mut conn = Connection::open_in_memory().unwrap();

let result = broken::migrations::runner().run(&mut conn);
Expand All @@ -234,7 +235,7 @@ mod rusqlite {
}

#[test]
fn embedded_doesnt_update_to_last_working_if_grouped() {
fn doesnt_update_to_last_working_if_grouped() {
let mut conn = Connection::open_in_memory().unwrap();

let result = broken::migrations::runner()
Expand Down Expand Up @@ -536,6 +537,64 @@ mod rusqlite {
assert_eq!(migrations[4].checksum(), applied_migration.checksum());
}

#[test]
fn doesnt_run_migrations_if_fake_version() {
let mut conn = Connection::open_in_memory().unwrap();

let report = embedded::migrations::runner()
.set_target(Target::FakeVersion(2))
.run(&mut conn)
.unwrap();

let applied_migrations = report.applied_migrations();

assert!(applied_migrations.is_empty());

let current = conn.get_last_applied_migration().unwrap().unwrap();
let migrations = get_migrations();
let mchecksum = migrations[1].checksum();

assert_eq!(2, current.version());
assert_eq!(mchecksum, current.checksum());

let err: Result<String, Error> = conn.query_row(
"SELECT name FROM sqlite_master WHERE type='table' AND name='persons'",
[],
|row| row.get(0),
);

assert!(matches!(err.unwrap_err(), Error::QueryReturnedNoRows));
}

#[test]
fn doesnt_run_migrations_if_fake() {
let mut conn = Connection::open_in_memory().unwrap();

let report = embedded::migrations::runner()
.set_target(Target::Fake)
.run(&mut conn)
.unwrap();

let applied_migrations = report.applied_migrations();

assert!(applied_migrations.is_empty());

let current = conn.get_last_applied_migration().unwrap().unwrap();
let migrations = get_migrations();
let mchecksum = migrations[3].checksum();

assert_eq!(4, current.version());
assert_eq!(mchecksum, current.checksum());

let err: Result<String, Error> = conn.query_row(
"SELECT name FROM sqlite_master WHERE type='table' AND name='persons'",
[],
|row| row.get(0),
);

assert!(matches!(err.unwrap_err(), Error::QueryReturnedNoRows));
}

#[test]
fn migrates_from_cli() {
run_test(|| {
Expand Down
Loading

0 comments on commit 74b7063

Please sign in to comment.