feat: add admin initialization and database migration tasks
This commit is contained in:
@@ -1,25 +1,34 @@
|
||||
use migration::migrate_database;
|
||||
use tracing::{debug, info};
|
||||
mod db_migrate;
|
||||
mod init_admin;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use sea_orm::ConnectOptions;
|
||||
use tracing::info;
|
||||
|
||||
use crate::configs::ProgramSettings;
|
||||
use database::get_connection;
|
||||
|
||||
pub async fn run_startup_tasks(config: &ProgramSettings) -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Here you can add any startup tasks you want to run when the application starts.
|
||||
info!("Running startup tasks...");
|
||||
|
||||
let db_options = |options: &mut ConnectOptions| {
|
||||
options.max_connections(config.database.max_connections);
|
||||
};
|
||||
|
||||
let db_connection = Arc::new(
|
||||
get_connection(&config.database.url, Some(db_options))
|
||||
.await
|
||||
.map_err(|err| format!("Failed to establish database connection: {}", err))?,
|
||||
);
|
||||
|
||||
if config.database.migrate_on_startup {
|
||||
run_database_migrations(&config.database.url).await?;
|
||||
db_migrate::run_database_migrations(&config.database.url).await?;
|
||||
} else {
|
||||
info!("Database migration on startup is disabled. Skipping migration.");
|
||||
}
|
||||
init_admin::init_admin(config, db_connection.clone()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_database_migrations(db_url: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Logic to run database migrations
|
||||
info!("Running database migrations...");
|
||||
debug!("Database URL: {}", db_url);
|
||||
migrate_database(db_url).await.map_err(Box::new)?;
|
||||
info!("Database migrations completed.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user