Skip to main content

systemprompt_database/lifecycle/
validation.rs

1//! Pre-flight validation helpers used by the boot path and tests.
2
3use crate::error::{DatabaseResult, RepositoryError};
4use crate::services::DatabaseProvider;
5
6pub async fn validate_database_connection(db: &dyn DatabaseProvider) -> DatabaseResult<()> {
7    db.test_connection().await.map_err(|e| {
8        RepositoryError::Internal(format!("Failed to establish database connection: {e}"))
9    })
10}
11
12pub async fn validate_table_exists(
13    db: &dyn DatabaseProvider,
14    table_name: &str,
15) -> DatabaseResult<bool> {
16    let result = db
17        .query_raw_with(
18            &"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \
19              'public' AND table_name = $1) as exists",
20            vec![serde_json::Value::String(table_name.to_string())],
21        )
22        .await?;
23
24    result
25        .first()
26        .and_then(|row| row.get("exists"))
27        .and_then(serde_json::Value::as_bool)
28        .ok_or_else(|| {
29            RepositoryError::Internal(format!(
30                "Failed to check table existence for '{table_name}'"
31            ))
32        })
33}
34
35pub async fn validate_column_exists(
36    db: &dyn DatabaseProvider,
37    table_name: &str,
38    column_name: &str,
39) -> DatabaseResult<bool> {
40    let result = db
41        .query_raw_with(
42            &"SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = \
43              'public' AND table_name = $1 AND column_name = $2) as exists",
44            vec![
45                serde_json::Value::String(table_name.to_string()),
46                serde_json::Value::String(column_name.to_string()),
47            ],
48        )
49        .await?;
50
51    result
52        .first()
53        .and_then(|row| row.get("exists"))
54        .and_then(serde_json::Value::as_bool)
55        .ok_or_else(|| {
56            RepositoryError::Internal(format!(
57                "Failed to check column existence for '{table_name}.{column_name}'"
58            ))
59        })
60}