fix migration test with hack

Without sleeping after migration, the tests fail, so...
This commit is contained in:
Joe Ardent 2023-06-01 12:24:43 -07:00
parent b691e6ee8d
commit 81adffe7f0
1 changed files with 24 additions and 9 deletions

View File

@ -48,6 +48,7 @@ pub async fn get_pool() -> SqlitePool {
.await
.expect("can't connect to database");
{
let mut m = Migrator::new(std::path::Path::new("./migrations"))
.await
.expect("Should be able to read the migration directory.");
@ -57,6 +58,11 @@ pub async fn get_pool() -> SqlitePool {
m.run(&pool)
.await
.expect("Should be able to run the migration.");
}
// hack to ensure that migration has actually run; surely there is a better
// way!!
tokio::time::sleep(Duration::from_secs(1)).await;
tracing::info!("Ran migrations");
@ -70,6 +76,10 @@ pub async fn session_layer(pool: SqlitePool, secret: &[u8]) -> SessionLayer<Sqli
.await
.expect("Calling `migrate()` should be reliable, is the DB gone?");
// hack to ensure that migration has actually run; surely there is a better
// way!!
tokio::time::sleep(Duration::from_secs(1)).await;
// since the secret is new every time the server starts, old sessions won't be
// valid anymore; if there were ever more than one service host or there were
// managed secrets, this would need to go away.
@ -93,14 +103,15 @@ pub async fn auth_layer(
AuthLayer::new(store, secret)
}
//-************************************************************************
// Tests for `db` module.
//-************************************************************************
#[cfg(test)]
mod tests {
use std::time::Duration;
#[tokio::test]
async fn it_migrates_the_db() {
let db = super::get_pool().await;
tokio::time::sleep(Duration::from_secs(2)).await;
let r = sqlx::query("select count(*) from witches")
.fetch_one(&db)
.await;
@ -108,6 +119,10 @@ mod tests {
}
}
//-************************************************************************
// End public interface.
//-************************************************************************
//-************************************************************************
// Session store sub-module, not a public lib.
//-************************************************************************