Skip to content

Commit

Permalink
Allow Postgres tests to be run on a different database
Browse files Browse the repository at this point in the history
Not everyone has a "scratch" PostgreSQL running on localhost:5432 for refinery to scribble all over.
Now you can specify an arbitrary PostgreSQL server to work on with the `DB_URI` environment variable (which appears to be what `refinery-cli` already uses) to test in.
  • Loading branch information
mpalmer committed May 3, 2024
1 parent ff1c5c0 commit 8903c89
Showing 1 changed file with 46 additions and 63 deletions.
109 changes: 46 additions & 63 deletions refinery/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ mod postgres {
use assert_cmd::prelude::*;
use predicates::str::contains;
use refinery::{
config::{Config, ConfigDbType},
embed_migrations,
error::Kind,
Migrate, Migration, Runner, Target,
config::Config, embed_migrations, error::Kind, Migrate, Migration, Runner, Target,
};
use refinery_core::postgres::{Client, NoTls};
use std::process::Command;
use std::str::FromStr;
use time::OffsetDateTime;

const DEFAULT_TABLE_NAME: &str = "refinery_schema_history";
Expand All @@ -31,6 +29,10 @@ mod postgres {
embed_migrations!("./tests/migrations_missing");
}

fn db_uri() -> String {
std::env::var("DB_URI").unwrap_or("postgres://postgres@localhost:5432/postgres".to_string())
}

fn get_migrations() -> Vec<Migration> {
embed_migrations!("./tests/migrations");

Expand Down Expand Up @@ -65,35 +67,45 @@ mod postgres {
}

fn clean_database() {
let mut client =
Client::connect("postgres://postgres@localhost:5432/template1", NoTls).unwrap();
let mut config = Config::from_str(&db_uri()).unwrap();
let uri = db_uri();
let db_name = uri.split('/').last().unwrap();

let mut client = Client::connect(
&(uri.strip_suffix(db_name).unwrap().to_string() + "template1"),
NoTls,
)
.unwrap();

client
.execute(
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='postgres'",
&[],
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname=$1",
&[&db_name],
)
.unwrap();
client.execute("DROP DATABASE POSTGRES", &[]).unwrap();
client.execute("CREATE DATABASE POSTGRES", &[]).unwrap();
client
.execute(&"DROP DATABASE IF EXISTS $1".replace("$1", db_name), &[])
.unwrap();
client
.execute(&"CREATE DATABASE $1".replace("$1", db_name), &[])
.unwrap();
}

fn run_test<T>(test: T)
where
T: FnOnce() + std::panic::UnwindSafe,
{
let result = std::panic::catch_unwind(test);

clean_database();

let result = std::panic::catch_unwind(test);

assert!(result.is_ok())
}

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

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

Expand Down Expand Up @@ -122,8 +134,7 @@ mod postgres {
#[test]
fn creates_migration_table() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
embedded::migrations::runner().run(&mut client).unwrap();
for row in &client
.query(
Expand All @@ -144,8 +155,7 @@ mod postgres {
#[test]
fn creates_migration_table_grouped_transaction() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner()
.set_grouped(true)
Expand All @@ -171,8 +181,7 @@ mod postgres {
#[test]
fn applies_migration() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
embedded::migrations::runner().run(&mut client).unwrap();
client
.execute(
Expand All @@ -192,8 +201,7 @@ mod postgres {
#[test]
fn applies_migration_grouped_transaction() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner()
.set_grouped(false)
Expand All @@ -218,8 +226,7 @@ mod postgres {
#[test]
fn updates_schema_history() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner().run(&mut client).unwrap();

Expand All @@ -239,8 +246,7 @@ mod postgres {
#[test]
fn updates_schema_history_grouped_transaction() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner()
.set_grouped(false)
Expand All @@ -262,8 +268,7 @@ mod postgres {
#[test]
fn updates_to_last_working_if_not_grouped() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let result = broken::migrations::runner().run(&mut client);

Expand Down Expand Up @@ -300,8 +305,7 @@ mod postgres {
#[test]
fn doesnt_update_to_last_working_if_grouped() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let result = broken::migrations::runner()
.set_grouped(true)
Expand All @@ -320,8 +324,7 @@ mod postgres {
#[test]
fn gets_applied_migrations() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -349,8 +352,7 @@ mod postgres {
#[test]
fn applies_new_migration() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -381,8 +383,7 @@ mod postgres {
#[test]
fn migrates_to_target_migration() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let report = embedded::migrations::runner()
.set_target(Target::Version(3))
Expand Down Expand Up @@ -417,8 +418,7 @@ mod postgres {
#[test]
fn migrates_to_target_migration_grouped() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let report = embedded::migrations::runner()
.set_target(Target::Version(3))
Expand Down Expand Up @@ -454,8 +454,7 @@ mod postgres {
#[test]
fn aborts_on_missing_migration_on_filesystem() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -488,8 +487,7 @@ mod postgres {
#[test]
fn aborts_on_divergent_migration() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -523,8 +521,7 @@ mod postgres {
#[test]
fn aborts_on_missing_migration_on_database() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

missing::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -568,11 +565,7 @@ mod postgres {
#[test]
fn migrates_from_config() {
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 mut config = Config::from_str(&db_uri()).unwrap();

let migrations = get_migrations();
let runner = Runner::new(&migrations)
Expand Down Expand Up @@ -608,11 +601,7 @@ mod postgres {
#[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 mut config = Config::from_str(&db_uri()).unwrap();

let migrations = get_migrations();
let runner = Runner::new(&migrations)
Expand Down Expand Up @@ -648,11 +637,7 @@ mod postgres {
#[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 mut config = Config::from_str(&db_uri()).unwrap();

let migrations = get_migrations();
let runner = Runner::new(&migrations)
Expand All @@ -677,8 +662,7 @@ mod postgres {
#[test]
fn doesnt_run_migrations_if_fake() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let report = embedded::migrations::runner()
.set_target(Target::Fake)
Expand Down Expand Up @@ -712,8 +696,7 @@ mod postgres {
#[test]
fn doesnt_run_migrations_if_fake_version() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let report = embedded::migrations::runner()
.set_target(Target::FakeVersion(2))
Expand Down

0 comments on commit 8903c89

Please sign in to comment.