|
| 1 | +use anyhow::Result; |
| 2 | +use serde::{Serialize, de::DeserializeOwned}; |
| 3 | + |
| 4 | +#[derive(strum::IntoStaticStr)] |
| 5 | +#[strum(serialize_all = "snake_case")] |
| 6 | +pub enum ConfigName { |
| 7 | + RustcVersion, |
| 8 | + LastSeenIndexReference, |
| 9 | + QueueLocked, |
| 10 | + Toolchain, |
| 11 | +} |
| 12 | + |
| 13 | +pub async fn set_config( |
| 14 | + conn: &mut sqlx::PgConnection, |
| 15 | + name: ConfigName, |
| 16 | + value: impl Serialize, |
| 17 | +) -> anyhow::Result<()> { |
| 18 | + let name: &'static str = name.into(); |
| 19 | + sqlx::query!( |
| 20 | + "INSERT INTO config (name, value) |
| 21 | + VALUES ($1, $2) |
| 22 | + ON CONFLICT (name) DO UPDATE SET value = $2;", |
| 23 | + name, |
| 24 | + &serde_json::to_value(value)?, |
| 25 | + ) |
| 26 | + .execute(conn) |
| 27 | + .await?; |
| 28 | + Ok(()) |
| 29 | +} |
| 30 | + |
| 31 | +pub async fn get_config<T>(conn: &mut sqlx::PgConnection, name: ConfigName) -> Result<Option<T>> |
| 32 | +where |
| 33 | + T: DeserializeOwned, |
| 34 | +{ |
| 35 | + let name: &'static str = name.into(); |
| 36 | + Ok( |
| 37 | + match sqlx::query!("SELECT value FROM config WHERE name = $1;", name) |
| 38 | + .fetch_optional(conn) |
| 39 | + .await? |
| 40 | + { |
| 41 | + Some(row) => serde_json::from_value(row.value)?, |
| 42 | + None => None, |
| 43 | + }, |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +#[cfg(test)] |
| 48 | +mod tests { |
| 49 | + use super::*; |
| 50 | + use crate::{Config, testing::TestDatabase}; |
| 51 | + use docs_rs_opentelemetry::testing::TestMetrics; |
| 52 | + use serde_json::Value; |
| 53 | + use test_case::test_case; |
| 54 | + |
| 55 | + #[test_case(ConfigName::RustcVersion, "rustc_version")] |
| 56 | + #[test_case(ConfigName::QueueLocked, "queue_locked")] |
| 57 | + #[test_case(ConfigName::LastSeenIndexReference, "last_seen_index_reference")] |
| 58 | + fn test_configname_variants(variant: ConfigName, expected: &'static str) { |
| 59 | + let name: &'static str = variant.into(); |
| 60 | + assert_eq!(name, expected); |
| 61 | + } |
| 62 | + |
| 63 | + #[tokio::test(flavor = "multi_thread")] |
| 64 | + async fn test_get_config_empty() -> anyhow::Result<()> { |
| 65 | + let test_metrics = TestMetrics::new(); |
| 66 | + let db = TestDatabase::new(&Config::test_config()?, test_metrics.provider()).await?; |
| 67 | + |
| 68 | + let mut conn = db.async_conn().await; |
| 69 | + sqlx::query!("DELETE FROM config") |
| 70 | + .execute(&mut *conn) |
| 71 | + .await?; |
| 72 | + |
| 73 | + assert!( |
| 74 | + get_config::<String>(&mut conn, ConfigName::RustcVersion) |
| 75 | + .await? |
| 76 | + .is_none() |
| 77 | + ); |
| 78 | + Ok(()) |
| 79 | + } |
| 80 | + |
| 81 | + #[tokio::test(flavor = "multi_thread")] |
| 82 | + async fn test_set_and_get_config_() -> anyhow::Result<()> { |
| 83 | + let test_metrics = TestMetrics::new(); |
| 84 | + let db = TestDatabase::new(&Config::test_config()?, test_metrics.provider()).await?; |
| 85 | + |
| 86 | + let mut conn = db.async_conn().await; |
| 87 | + sqlx::query!("DELETE FROM config") |
| 88 | + .execute(&mut *conn) |
| 89 | + .await?; |
| 90 | + |
| 91 | + assert!( |
| 92 | + get_config::<String>(&mut conn, ConfigName::RustcVersion) |
| 93 | + .await? |
| 94 | + .is_none() |
| 95 | + ); |
| 96 | + |
| 97 | + set_config( |
| 98 | + &mut conn, |
| 99 | + ConfigName::RustcVersion, |
| 100 | + Value::String("some value".into()), |
| 101 | + ) |
| 102 | + .await?; |
| 103 | + assert_eq!( |
| 104 | + get_config(&mut conn, ConfigName::RustcVersion).await?, |
| 105 | + Some("some value".to_string()) |
| 106 | + ); |
| 107 | + Ok(()) |
| 108 | + } |
| 109 | +} |
0 commit comments