systemprompt_database/lifecycle/
validation.rs1use 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 &[&table_name],
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 &[&table_name, &column_name],
45 )
46 .await?;
47
48 result
49 .first()
50 .and_then(|row| row.get("exists"))
51 .and_then(serde_json::Value::as_bool)
52 .ok_or_else(|| {
53 RepositoryError::Internal(format!(
54 "Failed to check column existence for '{table_name}.{column_name}'"
55 ))
56 })
57}