Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor Config #94

Merged
merged 1 commit into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 101 additions & 4 deletions refinery/tests/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ mod mysql {
use chrono::Local;
use predicates::str::contains;
use refinery::{
config::{migrate_from_config, Config, ConfigDbType},
config::{Config, ConfigDbType},
error::Kind,
Migrate, Migration, Target,
Migrate, Migration, Runner, Target,
};
use refinery_core::mysql;
use std::process::Command;
Expand Down Expand Up @@ -571,15 +571,112 @@ mod mysql {
#[test]
fn migrates_from_config() {
run_test(|| {
let config = Config::new(ConfigDbType::Mysql)
let mut 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 migrations = get_migrations();
migrate_from_config(&config, false, true, true, &migrations).unwrap();
let runner = Runner::new(&migrations)
.set_grouped(false)
.set_abort_divergent(true)
.set_abort_missing(true);

runner.run(&mut config).unwrap();

let applied_migrations = runner.get_applied_migrations(&mut config).unwrap();
assert_eq!(5, applied_migrations.len());

assert_eq!(migrations[0].version(), applied_migrations[0].version());
assert_eq!(migrations[1].version(), applied_migrations[1].version());
assert_eq!(migrations[2].version(), applied_migrations[2].version());
assert_eq!(migrations[3].version(), applied_migrations[3].version());
assert_eq!(migrations[4].version(), applied_migrations[4].version());

assert_eq!(migrations[0].name(), migrations[0].name());
assert_eq!(migrations[1].name(), applied_migrations[1].name());
assert_eq!(migrations[2].name(), applied_migrations[2].name());
assert_eq!(migrations[3].name(), applied_migrations[3].name());
assert_eq!(migrations[4].name(), applied_migrations[4].name());

assert_eq!(migrations[0].checksum(), applied_migrations[0].checksum());
assert_eq!(migrations[1].checksum(), applied_migrations[1].checksum());
assert_eq!(migrations[2].checksum(), applied_migrations[2].checksum());
assert_eq!(migrations[3].checksum(), applied_migrations[3].checksum());
assert_eq!(migrations[4].checksum(), applied_migrations[4].checksum());
})
}

#[test]
fn migrate_from_config_report_contains_migrations() {
run_test(|| {
let mut 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 migrations = get_migrations();
let runner = Runner::new(&migrations)
.set_grouped(false)
.set_abort_divergent(true)
.set_abort_missing(true);

let report = runner.run(&mut config).unwrap();

let applied_migrations = report.applied_migrations();
assert_eq!(5, applied_migrations.len());

assert_eq!(migrations[0].version(), applied_migrations[0].version());
assert_eq!(migrations[1].version(), applied_migrations[1].version());
assert_eq!(migrations[2].version(), applied_migrations[2].version());
assert_eq!(migrations[3].version(), applied_migrations[3].version());
assert_eq!(migrations[4].version(), applied_migrations[4].version());

assert_eq!(migrations[0].name(), migrations[0].name());
assert_eq!(migrations[1].name(), applied_migrations[1].name());
assert_eq!(migrations[2].name(), applied_migrations[2].name());
assert_eq!(migrations[3].name(), applied_migrations[3].name());
assert_eq!(migrations[4].name(), applied_migrations[4].name());

assert_eq!(migrations[0].checksum(), applied_migrations[0].checksum());
assert_eq!(migrations[1].checksum(), applied_migrations[1].checksum());
assert_eq!(migrations[2].checksum(), applied_migrations[2].checksum());
assert_eq!(migrations[3].checksum(), applied_migrations[3].checksum());
assert_eq!(migrations[4].checksum(), applied_migrations[4].checksum());
})
}

#[test]
fn migrate_from_config_report_returns_last_applied_migration() {
run_test(|| {
let mut 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 migrations = get_migrations();
let runner = Runner::new(&migrations)
.set_grouped(false)
.set_abort_divergent(true)
.set_abort_missing(true);

runner.run(&mut config).unwrap();

let applied_migration = runner
.get_last_applied_migration(&mut config)
.unwrap()
.unwrap();
assert_eq!(5, applied_migration.version());

assert_eq!(migrations[4].version(), applied_migration.version());
assert_eq!(migrations[4].name(), applied_migration.name());
assert_eq!(migrations[4].checksum(), applied_migration.checksum());
})
}

Expand Down
109 changes: 105 additions & 4 deletions refinery/tests/mysql_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ mod mysql_async {
use chrono::Local;
use futures::FutureExt;
use refinery::{
config::{migrate_from_config_async, Config, ConfigDbType},
config::{Config, ConfigDbType},
error::Kind,
AsyncMigrate, Migration, Target,
AsyncMigrate, Migration, Runner, Target,
};
use refinery_core::mysql_async::prelude::Queryable;
use refinery_core::{mysql_async, tokio};
Expand Down Expand Up @@ -641,17 +641,118 @@ mod mysql_async {
#[tokio::test]
async fn migrates_from_config() {
run_test(async {
let config = Config::new(ConfigDbType::Mysql)
let mut 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 migrations = get_migrations();
migrate_from_config_async(&config, false, true, true, &migrations)
let runner = Runner::new(&migrations)
.set_grouped(false)
.set_abort_divergent(true)
.set_abort_missing(true);

runner.run_async(&mut config).await.unwrap();

let applied_migrations = runner
.get_applied_migrations_async(&mut config)
.await
.unwrap();
assert_eq!(5, applied_migrations.len());

assert_eq!(migrations[0].version(), applied_migrations[0].version());
assert_eq!(migrations[1].version(), applied_migrations[1].version());
assert_eq!(migrations[2].version(), applied_migrations[2].version());
assert_eq!(migrations[3].version(), applied_migrations[3].version());
assert_eq!(migrations[4].version(), applied_migrations[4].version());

assert_eq!(migrations[0].name(), migrations[0].name());
assert_eq!(migrations[1].name(), applied_migrations[1].name());
assert_eq!(migrations[2].name(), applied_migrations[2].name());
assert_eq!(migrations[3].name(), applied_migrations[3].name());
assert_eq!(migrations[4].name(), applied_migrations[4].name());

assert_eq!(migrations[0].checksum(), applied_migrations[0].checksum());
assert_eq!(migrations[1].checksum(), applied_migrations[1].checksum());
assert_eq!(migrations[2].checksum(), applied_migrations[2].checksum());
assert_eq!(migrations[3].checksum(), applied_migrations[3].checksum());
assert_eq!(migrations[4].checksum(), applied_migrations[4].checksum());
})
.await;
}

#[tokio::test]
async fn migrate_from_config_report_contains_migrations() {
run_test(async {
let mut 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 migrations = get_migrations();
let runner = Runner::new(&migrations)
.set_grouped(false)
.set_abort_divergent(true)
.set_abort_missing(true);

let report = runner.run_async(&mut config).await.unwrap();

let applied_migrations = report.applied_migrations();
assert_eq!(5, applied_migrations.len());

assert_eq!(migrations[0].version(), applied_migrations[0].version());
assert_eq!(migrations[1].version(), applied_migrations[1].version());
assert_eq!(migrations[2].version(), applied_migrations[2].version());
assert_eq!(migrations[3].version(), applied_migrations[3].version());
assert_eq!(migrations[4].version(), applied_migrations[4].version());

assert_eq!(migrations[0].name(), migrations[0].name());
assert_eq!(migrations[1].name(), applied_migrations[1].name());
assert_eq!(migrations[2].name(), applied_migrations[2].name());
assert_eq!(migrations[3].name(), applied_migrations[3].name());
assert_eq!(migrations[4].name(), applied_migrations[4].name());

assert_eq!(migrations[0].checksum(), applied_migrations[0].checksum());
assert_eq!(migrations[1].checksum(), applied_migrations[1].checksum());
assert_eq!(migrations[2].checksum(), applied_migrations[2].checksum());
assert_eq!(migrations[3].checksum(), applied_migrations[3].checksum());
assert_eq!(migrations[4].checksum(), applied_migrations[4].checksum());
})
.await;
}

#[tokio::test]
async fn migrate_from_config_report_returns_last_applied_migration() {
run_test(async {
let mut 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 migrations = get_migrations();
let runner = Runner::new(&migrations)
.set_grouped(false)
.set_abort_divergent(true)
.set_abort_missing(true);

runner.run_async(&mut config).await.unwrap();

let applied_migration = runner
.get_last_applied_migration_async(&mut config)
.await
.unwrap()
.unwrap();
assert_eq!(5, applied_migration.version());

assert_eq!(migrations[4].version(), applied_migration.version());
assert_eq!(migrations[4].name(), applied_migration.name());
assert_eq!(migrations[4].checksum(), applied_migration.checksum());
})
.await;
}
Expand Down
103 changes: 99 additions & 4 deletions refinery/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ mod postgres {
use chrono::Local;
use predicates::str::contains;
use refinery::{
config::{migrate_from_config, Config, ConfigDbType},
config::{Config, ConfigDbType},
error::Kind,
Migrate, Migration, Target,
Migrate, Migration, Runner, Target,
};
use refinery_core::postgres::{Client, NoTls};
use std::process::Command;
Expand Down Expand Up @@ -556,14 +556,109 @@ mod postgres {
#[test]
fn migrates_from_config() {
run_test(|| {
let config = Config::new(ConfigDbType::Postgres)
let mut config = Config::new(ConfigDbType::Postgres)
.set_db_name("postgres")
.set_db_user("postgres")
.set_db_host("localhost")
.set_db_port("5432");

let migrations = get_migrations();
migrate_from_config(&config, false, true, true, &migrations).unwrap();
let runner = Runner::new(&migrations)
.set_grouped(false)
.set_abort_divergent(true)
.set_abort_missing(true);

runner.run(&mut config).unwrap();

let applied_migrations = runner.get_applied_migrations(&mut config).unwrap();
assert_eq!(5, applied_migrations.len());

assert_eq!(migrations[0].version(), applied_migrations[0].version());
assert_eq!(migrations[1].version(), applied_migrations[1].version());
assert_eq!(migrations[2].version(), applied_migrations[2].version());
assert_eq!(migrations[3].version(), applied_migrations[3].version());
assert_eq!(migrations[4].version(), applied_migrations[4].version());

assert_eq!(migrations[0].name(), migrations[0].name());
assert_eq!(migrations[1].name(), applied_migrations[1].name());
assert_eq!(migrations[2].name(), applied_migrations[2].name());
assert_eq!(migrations[3].name(), applied_migrations[3].name());
assert_eq!(migrations[4].name(), applied_migrations[4].name());

assert_eq!(migrations[0].checksum(), applied_migrations[0].checksum());
assert_eq!(migrations[1].checksum(), applied_migrations[1].checksum());
assert_eq!(migrations[2].checksum(), applied_migrations[2].checksum());
assert_eq!(migrations[3].checksum(), applied_migrations[3].checksum());
assert_eq!(migrations[4].checksum(), applied_migrations[4].checksum());
})
}

#[test]
fn migrate_from_config_report_contains_migrations() {
run_test(|| {
let mut config = Config::new(ConfigDbType::Postgres)
.set_db_name("postgres")
.set_db_user("postgres")
.set_db_host("localhost")
.set_db_port("5432");

let migrations = get_migrations();
let runner = Runner::new(&migrations)
.set_grouped(false)
.set_abort_divergent(true)
.set_abort_missing(true);

let report = runner.run(&mut config).unwrap();

let applied_migrations = report.applied_migrations();
assert_eq!(5, applied_migrations.len());

assert_eq!(migrations[0].version(), applied_migrations[0].version());
assert_eq!(migrations[1].version(), applied_migrations[1].version());
assert_eq!(migrations[2].version(), applied_migrations[2].version());
assert_eq!(migrations[3].version(), applied_migrations[3].version());
assert_eq!(migrations[4].version(), applied_migrations[4].version());

assert_eq!(migrations[0].name(), migrations[0].name());
assert_eq!(migrations[1].name(), applied_migrations[1].name());
assert_eq!(migrations[2].name(), applied_migrations[2].name());
assert_eq!(migrations[3].name(), applied_migrations[3].name());
assert_eq!(migrations[4].name(), applied_migrations[4].name());

assert_eq!(migrations[0].checksum(), applied_migrations[0].checksum());
assert_eq!(migrations[1].checksum(), applied_migrations[1].checksum());
assert_eq!(migrations[2].checksum(), applied_migrations[2].checksum());
assert_eq!(migrations[3].checksum(), applied_migrations[3].checksum());
assert_eq!(migrations[4].checksum(), applied_migrations[4].checksum());
})
}

#[test]
fn migrate_from_config_report_returns_last_applied_migration() {
run_test(|| {
let mut config = Config::new(ConfigDbType::Postgres)
.set_db_name("postgres")
.set_db_user("postgres")
.set_db_host("localhost")
.set_db_port("5432");

let migrations = get_migrations();
let runner = Runner::new(&migrations)
.set_grouped(false)
.set_abort_divergent(true)
.set_abort_missing(true);

runner.run(&mut config).unwrap();

let applied_migration = runner
.get_last_applied_migration(&mut config)
.unwrap()
.unwrap();
assert_eq!(5, applied_migration.version());

assert_eq!(migrations[4].version(), applied_migration.version());
assert_eq!(migrations[4].name(), applied_migration.name());
assert_eq!(migrations[4].checksum(), applied_migration.checksum());
})
}

Expand Down
Loading