Compare commits
2 Commits
e83fac75aa
...
c7be67cc71
Author | SHA1 | Date |
---|---|---|
boring_nick | c7be67cc71 | |
boring_nick | 672cdd93df |
|
@ -1697,6 +1697,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"aide",
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"axum",
|
||||
"axum-prometheus",
|
||||
"chrono",
|
||||
|
|
|
@ -54,6 +54,7 @@ twitch_api2 = { version = "0.6.1", features = [
|
|||
twitch = { git = "https://github.com/jprochazk/twitch-rs", features = ["simd"] }
|
||||
axum-prometheus = "0.3.3"
|
||||
metrics-prometheus = "0.4.1"
|
||||
async-trait = "0.1.68"
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.3.0"
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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()
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue