Skip to main content

systemprompt_runtime/
validation.rs

1//! System pre-flight validation: database URL shape and connectivity.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use crate::AppContext;
7use crate::error::{RuntimeError, RuntimeResult};
8use std::path::Path;
9use systemprompt_database::validate_database_connection;
10
11pub async fn validate_system(ctx: &AppContext) -> RuntimeResult<()> {
12    validate_database(ctx).await
13}
14
15async fn validate_database(ctx: &AppContext) -> RuntimeResult<()> {
16    validate_database_path(&ctx.config().database_url)?;
17    validate_database_connection(ctx.db_pool().as_ref()).await?;
18    Ok(())
19}
20
21pub fn validate_database_path(db_path: &str) -> RuntimeResult<()> {
22    if db_path.is_empty() {
23        return Err(RuntimeError::EmptyDatabaseUrl);
24    }
25
26    if db_path.starts_with("postgresql://") || db_path.starts_with("postgres://") {
27        return Ok(());
28    }
29
30    let path = Path::new(db_path);
31
32    if !path.exists() {
33        return Err(RuntimeError::DatabaseNotFound {
34            path: db_path.to_owned(),
35        });
36    }
37
38    if !path.is_file() {
39        return Err(RuntimeError::DatabaseNotFile {
40            path: db_path.to_owned(),
41        });
42    }
43
44    Ok(())
45}