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

Migrate test database at build time instead of runtime #767

Merged
merged 1 commit into from
Jun 12, 2017
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ civet = "0.9"
conduit-test = "0.8"
bufstream = "0.1"

[build-dependencies]
dotenv = "0.10"
diesel = { version = "0.13", features = ["postgres"] }

[features]
unstable = []
lint = ["clippy"]
20 changes: 20 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extern crate diesel;
extern crate dotenv;

use diesel::migrations::run_pending_migrations;
use diesel::pg::PgConnection;
use diesel::prelude::*;
use dotenv::dotenv;
use std::env;

fn main() {
if env::var("PROFILE") == Ok("debug".into()) {
let _ = dotenv();
if let Ok(database_url) = env::var("TEST_DATABASE_URL") {
let connection = PgConnection::establish(&database_url)
.expect("Could not connect to TEST_DATABASE_URL");
run_pending_migrations(&connection)
.expect("Error running migrations");
}
}
}
11 changes: 1 addition & 10 deletions src/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::borrow::Cow;
use std::collections::HashMap;
use std::env;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::{Once, ONCE_INIT, Arc};
use std::sync::Arc;

use cargo_registry::app::App;
use cargo_registry::category::NewCategory;
Expand Down Expand Up @@ -84,7 +84,6 @@ mod version;

fn app() -> (record::Bomb, Arc<App>, conduit_middleware::MiddlewareBuilder) {
dotenv::dotenv().ok();
static INIT: Once = ONCE_INIT;
git::init();

let (proxy, bomb) = record::proxy();
Expand Down Expand Up @@ -114,19 +113,11 @@ fn app() -> (record::Bomb, Arc<App>, conduit_middleware::MiddlewareBuilder) {
mirror: Replica::Primary,
api_protocol: api_protocol,
};
INIT.call_once(|| db_setup(&config.db_url));
let app = App::new(&config);
t!(t!(app.diesel_database.get()).begin_test_transaction());
let app = Arc::new(app);
let middleware = cargo_registry::middleware(app.clone());
return (bomb, app, middleware);

fn db_setup(db: &str) {
use diesel::migrations::run_pending_migrations;

let connection = PgConnection::establish(db).unwrap();
run_pending_migrations(&connection).unwrap();
}
}

fn env(s: &str) -> String {
Expand Down