Skip to main content

systemprompt_runtime/
validation.rs

1use crate::AppContext;
2use anyhow::{bail, Result};
3use std::path::Path;
4use systemprompt_database::validate_database_connection;
5
6pub async fn validate_system(ctx: &AppContext) -> Result<()> {
7    validate_database(ctx).await?;
8    Ok(())
9}
10
11async fn validate_database(ctx: &AppContext) -> Result<()> {
12    validate_database_path(&ctx.config().database_url)?;
13    validate_database_connection(ctx.db_pool().as_ref()).await?;
14    Ok(())
15}
16
17fn validate_database_path(db_path: &str) -> Result<()> {
18    if db_path.is_empty() {
19        bail!("DATABASE_URL is empty");
20    }
21
22    if db_path.starts_with("postgresql://") || db_path.starts_with("postgres://") {
23        return Ok(());
24    }
25
26    let path = Path::new(db_path);
27
28    if !path.exists() {
29        bail!("Database not found at '{db_path}'. Run setup first");
30    }
31
32    if !path.is_file() {
33        bail!("Database path '{db_path}' exists but is not a file");
34    }
35
36    Ok(())
37}