Files
zhealth/backend/src/db.rs

48 lines
1.8 KiB
Rust

//! Database connection and migrations.
use sea_orm::{ConnectionTrait, Database, DatabaseConnection, DbBackend, DbErr, Schema, Statement};
use sea_orm::sea_query::SqliteQueryBuilder;
use crate::config::Config;
use crate::models::bio::{biomarker, biomarker_category, biomarker_entry, biomarker_reference_rule, source};
use crate::models::user::{diet, role, session, user};
/// Connect to the SQLite database.
pub async fn connect(config: &Config) -> Result<DatabaseConnection, DbErr> {
let db_path = &config.paths.database;
// Ensure the data directory exists
if let Some(parent) = std::path::Path::new(db_path).parent() {
std::fs::create_dir_all(parent).ok();
}
let db_url = format!("sqlite:{}?mode=rwc", db_path);
Database::connect(&db_url).await
}
/// Run migrations to create tables if they don't exist.
pub async fn run_migrations(db: &DatabaseConnection) -> Result<(), DbErr> {
let schema = Schema::new(DbBackend::Sqlite);
// Create table statements (order matters for foreign keys)
let statements = vec![
schema.create_table_from_entity(role::Entity),
schema.create_table_from_entity(diet::Entity),
schema.create_table_from_entity(user::Entity),
schema.create_table_from_entity(session::Entity),
schema.create_table_from_entity(biomarker_category::Entity),
schema.create_table_from_entity(biomarker::Entity),
schema.create_table_from_entity(biomarker_reference_rule::Entity),
schema.create_table_from_entity(source::Entity),
schema.create_table_from_entity(biomarker_entry::Entity),
];
for mut stmt in statements {
let sql = stmt.if_not_exists().to_string(SqliteQueryBuilder);
db.execute(Statement::from_string(DbBackend::Sqlite, sql))
.await?;
}
Ok(())
}