Compare commits

...

3 Commits

Author SHA1 Message Date
boring_nick 25d290b640 Merge branch 'master' into supibot 2023-06-27 17:32:16 +03:00
boring_nick c7be67cc71 generic migratable 2023-06-27 17:25:47 +03:00
boring_nick 672cdd93df treat any query parameter value as true in bool params 2023-06-27 16:10:09 +03:00
5 changed files with 41 additions and 7 deletions

1
Cargo.lock generated
View File

@ -1727,6 +1727,7 @@ version = "0.1.0"
dependencies = [
"aide",
"anyhow",
"async-trait",
"axum",
"axum-prometheus",
"chrono",

View File

@ -57,6 +57,7 @@ axum-prometheus = "0.3.3"
metrics-prometheus = "0.4.1"
csv = "1.2.2"
itertools = "0.11.0"
async-trait = "0.1.68"
[dev-dependencies]
pretty_assertions = "1.3.0"

View File

@ -0,0 +1,27 @@
use async_trait::async_trait;
use clickhouse::Client;
use futures::Future;
#[async_trait]
pub trait Migratable<'a> {
async fn run(&self, db: &'a Client) -> anyhow::Result<()>;
}
#[async_trait]
impl<'a> Migratable<'a> for &str {
async fn run(&self, db: &'a Client) -> anyhow::Result<()> {
db.query(self).execute().await?;
Ok(())
}
}
#[async_trait]
impl<'a, F, O> Migratable<'a> for F
where
F: Fn(&'a Client) -> O + Sync + Send,
O: Future<Output = anyhow::Result<()>> + Send,
{
async fn run(&self, db: &'a Client) -> anyhow::Result<()> {
self(db).await
}
}

View File

@ -1,7 +1,11 @@
mod migratable;
use crate::Result;
use clickhouse::Client;
use tracing::{debug, info};
use self::migratable::Migratable;
pub async fn run(db: &Client) -> Result<()> {
create_migrations_table(db).await?;
@ -44,7 +48,11 @@ MATERIALIZE PROJECTION channel_log_dates",
Ok(())
}
async fn run_migration(db: &Client, name: &str, query: &str) -> Result<()> {
async fn run_migration<'a, T: Migratable<'a>>(
db: &'a Client,
name: &str,
migratable: T,
) -> Result<()> {
let count = db
.query("SELECT count(*) FROM __rustlog_migrations WHERE name = ?")
.bind(name)
@ -53,7 +61,8 @@ async fn run_migration(db: &Client, name: &str, query: &str) -> Result<()> {
if count == 0 {
info!("Running migration {name}");
db.query(query).execute().await?;
migratable.run(db).await?;
db.query("INSERT INTO __rustlog_migrations VALUES (?, now())")
.bind(name)
.execute()

View File

@ -111,11 +111,7 @@ fn deserialize_bool_param<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
let opt = Option::<String>::deserialize(deserializer)?;
match opt.as_deref() {
Some("1") | Some("true") | Some("") => Ok(true),
_ => Ok(false),
}
Ok(Option::<&str>::deserialize(deserializer)?.is_some())
}
impl TryFrom<&UserLogsPath> for UserLogDate {