78 lines
3.0 KiB
Rust
78 lines
3.0 KiB
Rust
// file: kb-store/src/postgres/query/health_queries.rs
|
|
// version: 1
|
|
|
|
//! PostgreSQL health and diagnostic SQL queries.
|
|
|
|
pub(in crate::postgres) async fn run_health_check(pool: &sqlx::PgPool) -> kb_core::Result<()> {
|
|
let query_result = sqlx::query_scalar::<sqlx::Postgres, i32>("SELECT 1").fetch_one(pool).await;
|
|
return match query_result {
|
|
std::result::Result::Ok(_value) => std::result::Result::Ok(()),
|
|
std::result::Result::Err(error) => std::result::Result::Err(kb_core::Error::db(format!(
|
|
"postgres healthcheck failed: {error}"
|
|
))),
|
|
};
|
|
}
|
|
|
|
pub(in crate::postgres) async fn load_current_schema(
|
|
pool: &sqlx::PgPool,
|
|
) -> kb_core::Result<std::string::String> {
|
|
let query_result =
|
|
sqlx::query_scalar::<sqlx::Postgres, std::string::String>("SELECT current_schema()")
|
|
.fetch_one(pool)
|
|
.await;
|
|
return match query_result {
|
|
std::result::Result::Ok(schema) => std::result::Result::Ok(schema),
|
|
std::result::Result::Err(error) => std::result::Result::Err(kb_core::Error::db(format!(
|
|
"postgres current schema query failed: {error}"
|
|
))),
|
|
};
|
|
}
|
|
|
|
pub(in crate::postgres) async fn load_server_version(
|
|
pool: &sqlx::PgPool,
|
|
) -> kb_core::Result<std::string::String> {
|
|
let query_result =
|
|
sqlx::query_scalar::<sqlx::Postgres, std::string::String>("SELECT version()")
|
|
.fetch_one(pool)
|
|
.await;
|
|
return match query_result {
|
|
std::result::Result::Ok(version) => std::result::Result::Ok(version),
|
|
std::result::Result::Err(error) => std::result::Result::Err(kb_core::Error::db(format!(
|
|
"postgres version query failed: {error}"
|
|
))),
|
|
};
|
|
}
|
|
|
|
pub(in crate::postgres) async fn load_migration_table_name(
|
|
pool: &sqlx::PgPool,
|
|
) -> kb_core::Result<std::option::Option<std::string::String>> {
|
|
let query_result =
|
|
sqlx::query_scalar::<sqlx::Postgres, std::option::Option<std::string::String>>(
|
|
"SELECT to_regclass('_sqlx_migrations')::text",
|
|
)
|
|
.fetch_one(pool)
|
|
.await;
|
|
return match query_result {
|
|
std::result::Result::Ok(table_name) => std::result::Result::Ok(table_name),
|
|
std::result::Result::Err(error) => std::result::Result::Err(kb_core::Error::db(format!(
|
|
"postgres migration table query failed: {error}"
|
|
))),
|
|
};
|
|
}
|
|
|
|
pub(in crate::postgres) async fn load_latest_migration_version(
|
|
pool: &sqlx::PgPool,
|
|
) -> kb_core::Result<std::option::Option<std::string::String>> {
|
|
let query_result = sqlx::query_scalar::<sqlx::Postgres, std::option::Option<std::string::String>>(
|
|
"SELECT version::text FROM _sqlx_migrations WHERE success = true ORDER BY version DESC LIMIT 1",
|
|
)
|
|
.fetch_optional(pool)
|
|
.await;
|
|
return match query_result {
|
|
std::result::Result::Ok(version) => std::result::Result::Ok(version.flatten()),
|
|
std::result::Result::Err(error) => std::result::Result::Err(kb_core::Error::db(format!(
|
|
"postgres latest migration query failed: {error}"
|
|
))),
|
|
};
|
|
}
|