diff --git a/diesel/src/migrations/mod.rs b/diesel/src/migrations/mod.rs index 2c150c7f7308..dc95e4e2888c 100644 --- a/diesel/src/migrations/mod.rs +++ b/diesel/src/migrations/mod.rs @@ -61,6 +61,8 @@ mod schema; pub use self::migration_error::*; +use std::io::{stdout, Write}; + use ::expression::expression_methods::*; use ::query_dsl::*; use self::migration::*; @@ -92,11 +94,11 @@ pub fn run_pending_migrations(conn: &Conn) -> Result<(), RunMigrationsErro String: FromSql, { let migrations_dir = try!(find_migrations_directory()); - run_pending_migrations_in_directory(conn, &migrations_dir) + run_pending_migrations_in_directory(conn, &migrations_dir, &mut stdout()) } #[doc(hidden)] -pub fn run_pending_migrations_in_directory(conn: &Conn, migrations_dir: &Path) +pub fn run_pending_migrations_in_directory(conn: &Conn, migrations_dir: &Path, output: &mut Write) -> Result<(), RunMigrationsError> where Conn: Connection, String: FromSql, @@ -107,7 +109,7 @@ pub fn run_pending_migrations_in_directory(conn: &Conn, migrations_dir: &P let pending_migrations = all_migrations.into_iter().filter(|m| { !already_run.contains(m.version()) }); - run_migrations(conn, pending_migrations.collect()) + run_migrations(conn, pending_migrations.collect(), output) } /// Reverts the last migration that was run. Returns the version that was reverted. Returns an @@ -121,22 +123,26 @@ pub fn revert_latest_migration(conn: &Conn) -> Result(conn: &Conn, ver: &str) -> Result<(), RunMigrationsError> { +pub fn revert_migration_with_version(conn: &Conn, ver: &str, output: &mut Write) + -> Result<(), RunMigrationsError> +{ migration_with_version(ver) .map_err(|e| e.into()) - .and_then(|m| revert_migration(conn, m)) + .and_then(|m| revert_migration(conn, m, output)) } #[doc(hidden)] -pub fn run_migration_with_version(conn: &Conn, ver: &str) -> Result<(), RunMigrationsError> { +pub fn run_migration_with_version(conn: &Conn, ver: &str, output: &mut Write) + -> Result<(), RunMigrationsError> +{ migration_with_version(ver) .map_err(|e| e.into()) - .and_then(|m| run_migration(conn, m)) + .and_then(|m| run_migration(conn, m, output)) } fn migration_with_version(ver: &str) -> Result, MigrationError> { @@ -196,21 +202,21 @@ fn migrations_in_directory(path: &Path) -> Result>, Migration }).collect() } -fn run_migrations(conn: &Conn, mut migrations: Vec>) +fn run_migrations(conn: &Conn, mut migrations: Vec>, output: &mut Write) -> Result<(), RunMigrationsError> { migrations.sort_by(|a, b| a.version().cmp(b.version())); for migration in migrations { - try!(run_migration(conn, migration)); + try!(run_migration(conn, migration, output)); } Ok(()) } -fn run_migration(conn: &Conn, migration: Box) +fn run_migration(conn: &Conn, migration: Box, output: &mut Write) -> Result<(), RunMigrationsError> { conn.transaction(|| { - println!("Running migration {}", migration.version()); + try!(writeln!(output, "Running migration {}", migration.version())); try!(migration.run(conn)); try!(::insert(&NewMigration(migration.version())) .into(__diesel_schema_migrations) @@ -219,11 +225,11 @@ fn run_migration(conn: &Conn, migration: Box) }).map_err(|e| e.into()) } -fn revert_migration(conn: &Conn, migration: Box) +fn revert_migration(conn: &Conn, migration: Box, output: &mut Write) -> Result<(), RunMigrationsError> { try!(conn.transaction(|| { - println!("Rolling back migration {}", migration.version()); + try!(writeln!(output, "Rolling back migration {}", migration.version())); try!(migration.revert(conn)); let target = __diesel_schema_migrations.filter(version.eq(migration.version())); try!(::delete(target).execute(conn)); diff --git a/diesel_tests/build.rs b/diesel_tests/build.rs index 6814ca7d9fb2..85d1f56420e7 100644 --- a/diesel_tests/build.rs +++ b/diesel_tests/build.rs @@ -30,6 +30,7 @@ extern crate dotenv; use diesel::*; use diesel::connection::PgConnection; use dotenv::dotenv; +use std::io; fn main() { dotenv().ok(); @@ -37,6 +38,6 @@ fn main() { .expect("DATABASE_URL must be set to run tests"); let connection = PgConnection::establish(&database_url).unwrap(); let migrations_dir = migrations::find_migrations_directory().unwrap().join("postgresql"); - migrations::run_pending_migrations_in_directory(&connection, &migrations_dir).unwrap(); + migrations::run_pending_migrations_in_directory(&connection, &migrations_dir, &mut io::sink()).unwrap(); inner::main(); } diff --git a/diesel_tests/tests/schema.rs b/diesel_tests/tests/schema.rs index b6c509a3e334..4b64c0a44b06 100644 --- a/diesel_tests/tests/schema.rs +++ b/diesel_tests/tests/schema.rs @@ -127,9 +127,11 @@ pub fn connection_without_transaction() -> TestConnection { #[cfg(feature = "sqlite")] pub fn connection_without_transaction() -> TestConnection { + use std::io; + let connection = ::diesel::connection::SqliteConnection::establish(":memory:").unwrap(); let migrations_dir = migrations::find_migrations_directory().unwrap().join("sqlite"); - migrations::run_pending_migrations_in_directory(&connection, &migrations_dir).unwrap(); + migrations::run_pending_migrations_in_directory(&connection, &migrations_dir, &mut io::sink()).unwrap(); connection }