From 84a7064991647018aeb67b6a9e92ec662ab4ebad Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Mon, 20 Feb 2023 10:15:41 +0200 Subject: [PATCH] feat(api): embed migrations --- api/src/main.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/api/src/main.rs b/api/src/main.rs index 0aae4b2..a2a63b7 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -13,14 +13,21 @@ use diesel::{ r2d2::{self, ConnectionManager}, PgConnection, }; +use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; type PgPool = r2d2::Pool>; +const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); + #[get("/ping")] async fn ping() -> impl Responder { HttpResponse::Ok().body("pong") } +fn run_migrations(conn: &mut PgConnection) { + conn.run_pending_migrations(MIGRATIONS).unwrap(); +} + #[actix_web::main] async fn main() -> std::io::Result<()> { pretty_env_logger::init(); @@ -35,6 +42,10 @@ async fn main() -> std::io::Result<()> { .build(manager) .expect("failed to create pool"); + // run migrations + log::info!("running migrations"); + run_migrations(&mut pg_pool.get().unwrap()); + HttpServer::new(move || { App::new() .app_data(web::Data::new(pg_pool.clone())) -- 2.44.1