From fa9ccda392ccfdfbbc585b20b226ffa33603bb14 Mon Sep 17 00:00:00 2001 From: Alexander Tesfamichael Date: Thu, 6 Jul 2023 20:47:03 +0200 Subject: [PATCH] feat(server): migrate DB on start --- src/bin/server/app.rs | 9 ++------- src/bin/server/main.rs | 5 ++++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/bin/server/app.rs b/src/bin/server/app.rs index d3e14cd..d107d3f 100644 --- a/src/bin/server/app.rs +++ b/src/bin/server/app.rs @@ -2,7 +2,7 @@ use crate::attestations::{ get_aggregate_price_interval_attestations, get_price_aggregate, get_price_interval_attestations, get_price_value_attestations, post_oracle_message, }; -use crate::db::{get_db_pool, DbPool}; +use crate::db::DbPool; use crate::state::AppState; use axum::{ routing::{get, post}, @@ -10,12 +10,7 @@ use axum::{ }; use std::sync::Arc; -pub async fn get_router() -> Router { - let db_pool = get_db_pool().await; - get_router_with_db_pool(db_pool) -} - -fn get_router_with_db_pool(db_pool: DbPool) -> Router { +pub fn get_router_with_db_pool(db_pool: DbPool) -> Router { let shared_state = Arc::new(AppState { db_pool }); Router::new() .route( diff --git a/src/bin/server/main.rs b/src/bin/server/main.rs index 6da33d6..a884c35 100644 --- a/src/bin/server/main.rs +++ b/src/bin/server/main.rs @@ -1,4 +1,5 @@ use std::net::SocketAddr; + mod app; mod attestations; mod db; @@ -8,7 +9,9 @@ mod state; #[tokio::main] async fn main() { tracing_subscriber::fmt::init(); - let app = app::get_router().await; + let db_pool = db::get_db_pool().await; + sqlx::migrate!().run(&db_pool).await.unwrap(); + let app = app::get_router_with_db_pool(db_pool); let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); tracing::info!("Listening on {}", addr); axum::Server::bind(&addr)