Compare commits

...

2 Commits
master ... test

Author SHA1 Message Date
boring_nick c90e3b201c broken code 2023-06-26 21:37:44 +03:00
boring_nick 7aac7852fa generic migratable 2023-06-26 21:23:23 +03:00
4 changed files with 43 additions and 2 deletions

1
Cargo.lock generated
View File

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

View File

@ -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"

View File

@ -0,0 +1,28 @@
use crate::Result;
use async_trait::async_trait;
use clickhouse::Client;
use futures::Future;
#[async_trait]
pub trait Migratable {
async fn run(&self, db: &Client) -> Result<()>;
}
#[async_trait]
impl Migratable for &str {
async fn run(&self, db: &Client) -> Result<()> {
db.query(self).execute().await?;
Ok(())
}
}
#[async_trait]
impl<F, O> Migratable for F
where
F: Fn(&Client) -> O + Sync + Send,
O: Future<Output = Result<()>> + Send,
{
async fn run(&self, db: &Client) -> 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?;
@ -41,10 +45,12 @@ MATERIALIZE PROJECTION channel_log_dates",
)
.await?;
run_migration(db, "4_try_new_format", asd).await?;
Ok(())
}
async fn run_migration(db: &Client, name: &str, query: &str) -> Result<()> {
async fn run_migration<T: Migratable>(db: &Client, name: &str, migratable: T) -> Result<()> {
let count = db
.query("SELECT count(*) FROM __rustlog_migrations WHERE name = ?")
.bind(name)
@ -53,7 +59,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()
@ -65,6 +72,10 @@ async fn run_migration(db: &Client, name: &str, query: &str) -> Result<()> {
Ok(())
}
async fn asd(db: &Client) -> Result<()> {
todo!()
}
async fn create_migrations_table(db: &Client) -> Result<()> {
db.query(
"