You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
inventory-app/src/db/bootstrap_data.rs

52 lines
1.3 KiB

use crate::db::user::{add_user, get_user_by_name, get_user_count, DbUserRole};
use anyhow::{anyhow, Result};
use serde::Deserialize;
use sqlx::SqlitePool;
use tracing::info;
#[derive(Debug, Deserialize)]
struct BootstrapData {
users: Vec<BootstrapUser>,
}
#[derive(Debug, Deserialize)]
struct BootstrapUser {
name: String,
role: String
}
pub async fn bootstrap_database(db: &SqlitePool) -> Result<()> {
let bootstrap_str = match std::env::var("BOOTSTRAP_DATA") {
Ok(s) => {
info!("bootstrap data found, updating db: {}", s);
s
},
Err(_) => {
info!("no Bootstrap data found");
return Ok(());
},
};
let data = ron::from_str::<BootstrapData>(&bootstrap_str)?;
if db_needs_users(db).await? {
for user in &data.users {
let role = DbUserRole::try_from_str(&user.role).ok_or(anyhow!("invalid role {}", user.role))?;
if get_user_by_name(db, &user.name).await?.is_none() {
let new_id = add_user(db, &user.name, role).await?;
info!("bootstrap new user {}:{} ({})", new_id, user.name, user.role);
}
}
}
Ok(())
}
async fn db_needs_users(db: &SqlitePool) -> Result<bool> {
let count = get_user_count(&db).await?;
Ok(count <= 0)
}

Powered by TurnKey Linux.