Skip to content

Commit

Permalink
Run migrations in the library function only
Browse files Browse the repository at this point in the history
  • Loading branch information
alaarihan committed Apr 1, 2024
1 parent 76c8e7b commit 39a2155
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
14 changes: 11 additions & 3 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use utils::run_migrations;

pub mod app_state;
pub mod auth;
pub mod db_connection;
Expand All @@ -12,6 +14,7 @@ pub mod utils;
pub enum RustError {
Ok = 0,
Error = 1,
MigrationError = 2,
}

#[no_mangle]
Expand All @@ -24,6 +27,14 @@ pub extern "C" fn run_server() -> RustError {

// Run the server logic in a blocking thread
let result = runtime.block_on(async {
match run_migrations().await {
Ok(_) => (),
Err(err) => {
// Handle migration errors here (log, convert to RustError)
eprintln!("Migration error: {:?}", err);
return RustError::MigrationError;
}
};
match server::server_start().await {
Ok(_) => RustError::Ok,
Err(err) => {
Expand All @@ -34,8 +45,5 @@ pub extern "C" fn run_server() -> RustError {
}
});

// Free the runtime resources
drop(runtime);

result
}
7 changes: 1 addition & 6 deletions api/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use tower_http::{
services::ServeDir,
};

use crate::{
app_state,
utils::{copy_database_if_not_exists /* run_migrations */},
};
use crate::{app_state, utils::copy_database_if_not_exists};

use super::modbus_register::routes::modbus_register_routes;
use super::user::routes::user_routes;
Expand Down Expand Up @@ -49,8 +46,6 @@ pub async fn server_start() -> Result<(), Box<dyn Error>> {

copy_database_if_not_exists()?;

// run_migrations().await?;

let app = create_app().await?;

let server_port = env::var("PORT").unwrap_or_else(|_| "9103".to_string());
Expand Down

0 comments on commit 39a2155

Please sign in to comment.