Skip to content

Commit 9855df8

Browse files
committed
migrate service-config-in-db to database subcrate
1 parent ca2f7c5 commit 9855df8

File tree

11 files changed

+131
-128
lines changed

11 files changed

+131
-128
lines changed

.sqlx/query-4ec79e1fa4249f4e1b085d1bbdffa537d1e3160d642e4a3325f4aea6c21974eb.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ opentelemetry_sdk = { workspace = true }
9393
rustwide = { version = "0.21.0", features = ["unstable-toolchain-ci", "unstable"] }
9494
hostname = "0.4.0"
9595
base64 = "0.22"
96-
strum = { workspace = true }
9796
lol_html = "2.0.0"
9897
font-awesome-as-a-crate = { path = "crates/lib/font-awesome-as-a-crate" }
9998
getrandom = "0.3.1"

crates/lib/docs_rs_database/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ futures-util = { workspace = true }
1212
hex = "0.4.3"
1313
opentelemetry = { workspace = true }
1414
rand = { workspace = true, optional = true }
15+
serde = { workspace = true }
16+
serde_json = { workspace = true }
1517
sqlx = { workspace = true }
18+
strum = { workspace = true }
1619
thiserror = { workspace = true }
1720
tokio = { workspace = true }
1821
tracing = { workspace = true }

crates/lib/docs_rs_database/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod errors;
33
mod metrics;
44
mod migrations;
55
mod pool;
6+
pub mod service_config;
67
#[cfg(any(test, feature = "testing"))]
78
pub mod testing;
89

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

src/bin/cratesfyi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use docs_rs::{
66
db::{self, Overrides},
77
queue_rebuilds_faulty_rustdoc, start_web_server,
88
utils::{
9-
ConfigName, daemon::start_background_service_metric_collector, get_config,
10-
get_crate_pattern_and_priority, list_crate_priorities, queue_builder,
11-
remove_crate_priority, set_config, set_crate_priority,
9+
daemon::start_background_service_metric_collector, get_crate_pattern_and_priority,
10+
list_crate_priorities, queue_builder, remove_crate_priority, set_crate_priority,
1211
},
1312
};
13+
use docs_rs_database::service_config::{ConfigName, get_config, set_config};
1414
use docs_rs_storage::add_path_into_database;
1515
use docs_rs_types::{CrateId, Version};
1616
use futures_util::StreamExt;

src/build_queue.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ use crate::{
33
db::{delete_crate, delete_version, update_latest_version_id},
44
docbuilder::{BuilderMetrics, PackageKind},
55
error::Result,
6-
utils::{ConfigName, get_config, get_crate_priority, report_error, set_config},
6+
utils::{get_crate_priority, report_error},
77
};
88
use anyhow::Context as _;
99
use chrono::NaiveDate;
1010
use crates_index_diff::{Change, CrateVersion};
11-
use docs_rs_database::{AsyncPoolClient, Pool};
11+
use docs_rs_database::{
12+
AsyncPoolClient, Pool,
13+
service_config::{ConfigName, get_config, set_config},
14+
};
1215
use docs_rs_fastly::{Cdn, CdnBehaviour as _};
1316
use docs_rs_opentelemetry::AnyMeterProvider;
1417
use docs_rs_storage::AsyncStorage;

src/docbuilder/rustwide_builder.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ use crate::{
88
docbuilder::Limits,
99
error::Result,
1010
metrics::{BUILD_TIME_HISTOGRAM_BUCKETS, DOCUMENTATION_SIZE_BUCKETS},
11-
utils::{ConfigName, copy_dir_all, get_config, report_error, set_config},
11+
utils::{copy_dir_all, report_error},
1212
};
1313
use anyhow::{Context as _, Error, anyhow, bail};
1414
use docs_rs_cargo_metadata::{CargoMetadata, MetadataPackage};
15-
use docs_rs_database::Pool;
15+
use docs_rs_database::{
16+
Pool,
17+
service_config::{ConfigName, get_config, set_config},
18+
};
1619
use docs_rs_opentelemetry::AnyMeterProvider;
1720
use docs_rs_registry_api::RegistryApi;
1821
use docs_rs_repository_stats::RepositoryStatsUpdater;

src/utils/mod.rs

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ mod html;
1717
mod queue;
1818
pub(crate) mod queue_builder;
1919

20-
use anyhow::Result;
21-
use serde::{Serialize, de::DeserializeOwned};
2220
use tracing::error;
2321

2422
pub(crate) fn report_error(err: &anyhow::Error) {
@@ -30,107 +28,3 @@ pub(crate) fn report_error(err: &anyhow::Error) {
3028
error!("{err:?}");
3129
}
3230
}
33-
34-
#[derive(strum::IntoStaticStr)]
35-
#[strum(serialize_all = "snake_case")]
36-
pub enum ConfigName {
37-
RustcVersion,
38-
LastSeenIndexReference,
39-
QueueLocked,
40-
Toolchain,
41-
}
42-
43-
pub async fn set_config(
44-
conn: &mut sqlx::PgConnection,
45-
name: ConfigName,
46-
value: impl Serialize,
47-
) -> anyhow::Result<()> {
48-
let name: &'static str = name.into();
49-
sqlx::query!(
50-
"INSERT INTO config (name, value)
51-
VALUES ($1, $2)
52-
ON CONFLICT (name) DO UPDATE SET value = $2;",
53-
name,
54-
&serde_json::to_value(value)?,
55-
)
56-
.execute(conn)
57-
.await?;
58-
Ok(())
59-
}
60-
61-
pub async fn get_config<T>(conn: &mut sqlx::PgConnection, name: ConfigName) -> Result<Option<T>>
62-
where
63-
T: DeserializeOwned,
64-
{
65-
let name: &'static str = name.into();
66-
Ok(
67-
match sqlx::query!("SELECT value FROM config WHERE name = $1;", name)
68-
.fetch_optional(conn)
69-
.await?
70-
{
71-
Some(row) => serde_json::from_value(row.value)?,
72-
None => None,
73-
},
74-
)
75-
}
76-
77-
#[cfg(test)]
78-
mod tests {
79-
use super::*;
80-
use crate::test::async_wrapper;
81-
use serde_json::Value;
82-
use test_case::test_case;
83-
84-
#[test_case(ConfigName::RustcVersion, "rustc_version")]
85-
#[test_case(ConfigName::QueueLocked, "queue_locked")]
86-
#[test_case(ConfigName::LastSeenIndexReference, "last_seen_index_reference")]
87-
fn test_configname_variants(variant: ConfigName, expected: &'static str) {
88-
let name: &'static str = variant.into();
89-
assert_eq!(name, expected);
90-
}
91-
92-
#[test]
93-
fn test_get_config_empty() {
94-
async_wrapper(|env| async move {
95-
let mut conn = env.async_db().async_conn().await;
96-
sqlx::query!("DELETE FROM config")
97-
.execute(&mut *conn)
98-
.await?;
99-
100-
assert!(
101-
get_config::<String>(&mut conn, ConfigName::RustcVersion)
102-
.await?
103-
.is_none()
104-
);
105-
Ok(())
106-
});
107-
}
108-
109-
#[test]
110-
fn test_set_and_get_config_() {
111-
async_wrapper(|env| async move {
112-
let mut conn = env.async_db().async_conn().await;
113-
sqlx::query!("DELETE FROM config")
114-
.execute(&mut *conn)
115-
.await?;
116-
117-
assert!(
118-
get_config::<String>(&mut conn, ConfigName::RustcVersion)
119-
.await?
120-
.is_none()
121-
);
122-
123-
set_config(
124-
&mut conn,
125-
ConfigName::RustcVersion,
126-
Value::String("some value".into()),
127-
)
128-
.await?;
129-
assert_eq!(
130-
get_config(&mut conn, ConfigName::RustcVersion).await?,
131-
Some("some value".to_string())
132-
);
133-
Ok(())
134-
});
135-
}
136-
}

0 commit comments

Comments
 (0)