log migrations to logger, not to stdout directly

feat/vaults
Tomáš Mládek 2020-08-27 01:02:28 +02:00
parent 48cf5889e7
commit f1c5a59073
1 changed files with 36 additions and 1 deletions

View File

@ -87,6 +87,36 @@ impl diesel::r2d2::CustomizeConnection<SqliteConnection, diesel::r2d2::Error>
}
}
#[derive(Default)]
struct LoggerSink {
buffer: Vec<u8>,
}
impl std::io::Write for LoggerSink {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.buffer.extend(buf.into_iter());
if self.buffer.ends_with(b"\n") {
self.flush()?;
}
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
use std::str;
debug!(
"{}",
str::from_utf8(self.buffer.as_mut())
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?
.trim()
);
Ok(())
}
}
pub fn open_upend<P: AsRef<Path>>(dirpath: P) -> Result<DbPool, Box<dyn Error>> {
embed_migrations!("./migrations/upend/");
@ -101,7 +131,12 @@ pub fn open_upend<P: AsRef<Path>>(dirpath: P) -> Result<DbPool, Box<dyn Error>>
.build(manager)
.expect("Failed to create pool.");
embedded_migrations::run_with_output(&pool.get().unwrap(), &mut std::io::stdout())?;
embedded_migrations::run_with_output(
&pool.get().unwrap(),
&mut LoggerSink {
..Default::default()
},
)?;
Ok(pool)
}