// 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::("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 { let query_result = sqlx::query_scalar::("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 { let query_result = sqlx::query_scalar::("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> { let query_result = sqlx::query_scalar::>( "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> { let query_result = sqlx::query_scalar::>( "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}" ))), }; }