0.7.27 +Refactor
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
// file: kb_lib/src/db/queries/known_ws_endpoint.rs
|
||||
|
||||
//! Queries for `kb_known_ws_endpoints`.
|
||||
//! Queries for `k_sol_known_ws_endpoints`.
|
||||
|
||||
/// Inserts or updates one known WS endpoint row.
|
||||
pub async fn upsert_known_ws_endpoint(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbKnownWsEndpointDto,
|
||||
) -> Result<(), crate::KbError> {
|
||||
let entity_result = crate::KbKnownWsEndpointEntity::try_from(dto.clone());
|
||||
pub async fn query_known_ws_endpoints_upsert(
|
||||
database: &crate::Database,
|
||||
dto: &crate::KnownWsEndpointDto,
|
||||
) -> Result<(), crate::Error> {
|
||||
let entity_result = crate::KnownWsEndpointEntity::try_from(dto.clone());
|
||||
let entity = match entity_result {
|
||||
Ok(entity) => entity,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
crate::DatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_known_ws_endpoints (
|
||||
INSERT INTO k_sol_known_ws_endpoints (
|
||||
name,
|
||||
provider,
|
||||
url,
|
||||
@@ -47,8 +47,8 @@ ON CONFLICT(name) DO UPDATE SET
|
||||
match query_result {
|
||||
Ok(_) => return Ok(()),
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot upsert kb_known_ws_endpoints on sqlite: {}",
|
||||
return Err(crate::Error::Db(format!(
|
||||
"cannot upsert k_sol_known_ws_endpoints on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
},
|
||||
@@ -58,13 +58,13 @@ ON CONFLICT(name) DO UPDATE SET
|
||||
}
|
||||
|
||||
/// Reads one known WS endpoint by name.
|
||||
pub async fn get_known_ws_endpoint(
|
||||
database: &crate::KbDatabase,
|
||||
pub async fn query_known_ws_endpoints_get(
|
||||
database: &crate::Database,
|
||||
name: &str,
|
||||
) -> Result<std::option::Option<crate::KbKnownWsEndpointDto>, crate::KbError> {
|
||||
) -> Result<std::option::Option<crate::KnownWsEndpointDto>, crate::Error> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbKnownWsEndpointEntity>(
|
||||
crate::DatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KnownWsEndpointEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
name,
|
||||
@@ -74,7 +74,7 @@ SELECT
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
FROM kb_known_ws_endpoints
|
||||
FROM k_sol_known_ws_endpoints
|
||||
WHERE name = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
@@ -85,7 +85,7 @@ LIMIT 1
|
||||
let entity_option = match query_result {
|
||||
Ok(entity_option) => entity_option,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
return Err(crate::Error::Db(format!(
|
||||
"cannot read known ws endpoint '{}' on sqlite: {}",
|
||||
name, error
|
||||
)));
|
||||
@@ -93,7 +93,7 @@ LIMIT 1
|
||||
};
|
||||
match entity_option {
|
||||
Some(entity) => {
|
||||
let dto_result = crate::KbKnownWsEndpointDto::try_from(entity);
|
||||
let dto_result = crate::KnownWsEndpointDto::try_from(entity);
|
||||
match dto_result {
|
||||
Ok(dto) => return Ok(Some(dto)),
|
||||
Err(error) => return Err(error),
|
||||
@@ -106,12 +106,12 @@ LIMIT 1
|
||||
}
|
||||
|
||||
/// Lists all known WS endpoints.
|
||||
pub async fn list_known_ws_endpoints(
|
||||
database: &crate::KbDatabase,
|
||||
) -> Result<std::vec::Vec<crate::KbKnownWsEndpointDto>, crate::KbError> {
|
||||
pub async fn query_known_ws_endpoints_list(
|
||||
database: &crate::Database,
|
||||
) -> Result<std::vec::Vec<crate::KnownWsEndpointDto>, crate::Error> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbKnownWsEndpointEntity>(
|
||||
crate::DatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KnownWsEndpointEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
name,
|
||||
@@ -121,7 +121,7 @@ SELECT
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
FROM kb_known_ws_endpoints
|
||||
FROM k_sol_known_ws_endpoints
|
||||
ORDER BY name ASC
|
||||
"#,
|
||||
)
|
||||
@@ -130,7 +130,7 @@ ORDER BY name ASC
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
return Err(crate::Error::Db(format!(
|
||||
"cannot list known ws endpoints on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
@@ -138,7 +138,7 @@ ORDER BY name ASC
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbKnownWsEndpointDto::try_from(entity);
|
||||
let dto_result = crate::KnownWsEndpointDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
@@ -156,10 +156,10 @@ mod tests {
|
||||
async fn known_ws_endpoint_roundtrip_works() {
|
||||
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
|
||||
let database_path = tempdir.path().join("known_ws_endpoint.sqlite3");
|
||||
let config = crate::KbDatabaseConfig {
|
||||
let config = crate::DatabaseConfig {
|
||||
enabled: true,
|
||||
backend: crate::KbDatabaseBackend::Sqlite,
|
||||
sqlite: crate::KbSqliteDatabaseConfig {
|
||||
backend: crate::DatabaseBackend::Sqlite,
|
||||
sqlite: crate::SqliteDatabaseConfig {
|
||||
path: database_path.to_string_lossy().to_string(),
|
||||
create_if_missing: true,
|
||||
busy_timeout_ms: 5000,
|
||||
@@ -168,27 +168,29 @@ mod tests {
|
||||
use_wal: true,
|
||||
},
|
||||
};
|
||||
let database = crate::KbDatabase::connect_and_initialize(&config)
|
||||
let database = crate::Database::connect_and_initialize(&config)
|
||||
.await
|
||||
.expect("database init must succeed");
|
||||
let dto = crate::KbKnownWsEndpointDto::new(
|
||||
let dto = crate::KnownWsEndpointDto::new(
|
||||
"mainnet_public_ws_slots".to_string(),
|
||||
"solana".to_string(),
|
||||
"wss://api.mainnet.solana.com".to_string(),
|
||||
true,
|
||||
vec!["ws_slots".to_string(), "ws_subscriptions".to_string()],
|
||||
);
|
||||
crate::upsert_known_ws_endpoint(&database, &dto)
|
||||
crate::query_known_ws_endpoints_upsert(&database, &dto)
|
||||
.await
|
||||
.expect("upsert must succeed");
|
||||
let fetched = crate::get_known_ws_endpoint(&database, "mainnet_public_ws_slots")
|
||||
let fetched = crate::query_known_ws_endpoints_get(&database, "mainnet_public_ws_slots")
|
||||
.await
|
||||
.expect("fetch must succeed");
|
||||
assert!(fetched.is_some());
|
||||
let fetched = fetched.expect("endpoint must exist");
|
||||
assert_eq!(fetched.provider, "solana");
|
||||
assert_eq!(fetched.roles.len(), 2);
|
||||
let listed = crate::list_known_ws_endpoints(&database).await.expect("list must succeed");
|
||||
let listed = crate::query_known_ws_endpoints_list(&database)
|
||||
.await
|
||||
.expect("list must succeed");
|
||||
assert_eq!(listed.len(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user