From 393ff04ff4a3cfd944e08e62bdda133dc1e5e3da Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 22 Oct 2021 21:16:59 +0200 Subject: Run migrations on service startup Signed-off-by: Matthias Beyer --- service-person/Cargo.toml | 1 + service-person/src/main.rs | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/service-person/Cargo.toml b/service-person/Cargo.toml index 7f447a2..530e944 100644 --- a/service-person/Cargo.toml +++ b/service-person/Cargo.toml @@ -10,3 +10,4 @@ actix-web = "3.3" anyhow = "1" serde = "1" diesel = { version = "1.4.4", features = ["postgres", "r2d2"] } +diesel_migrations = "1.4" diff --git a/service-person/src/main.rs b/service-person/src/main.rs index 1a47c51..3c5dc74 100644 --- a/service-person/src/main.rs +++ b/service-person/src/main.rs @@ -1,5 +1,7 @@ #[macro_use] extern crate diesel; +#[macro_use] +extern crate diesel_migrations; use std::str::FromStr; @@ -14,6 +16,7 @@ mod model; use model::*; +embed_migrations!("migrations"); type DbPool = Pool>; @@ -52,6 +55,11 @@ async fn main() -> anyhow::Result<()> { let db_connection_pool = establish_connection(); + { + let conn = db_connection_pool.get().expect("Failed to get connection from pool"); + embedded_migrations::run_with_output(&conn, &mut std::io::stdout()); + } // drop `conn` here + HttpServer::new(move || { App::new() .data(db_connection_pool.clone()) -- cgit v1.2.3