diff --git a/apps/labrinth/src/background_task.rs b/apps/labrinth/src/background_task.rs index 9a5150b3c..393635684 100644 --- a/apps/labrinth/src/background_task.rs +++ b/apps/labrinth/src/background_task.rs @@ -168,6 +168,9 @@ pub async fn update_bank_balances(pool: PgPool) -> eyre::Result<()> { pub async fn run_migrations() -> eyre::Result<()> { database::check_for_migrations().await?; + crate::clickhouse::run_migrations() + .await + .wrap_err("failed to run ClickHouse migrations")?; Ok(()) } diff --git a/apps/labrinth/src/clickhouse/mod.rs b/apps/labrinth/src/clickhouse/mod.rs index d20353d6e..e511bc048 100644 --- a/apps/labrinth/src/clickhouse/mod.rs +++ b/apps/labrinth/src/clickhouse/mod.rs @@ -19,29 +19,36 @@ pub async fn init_client() -> clickhouse::error::Result { pub async fn init_client_with_database( database: &str, ) -> clickhouse::error::Result { + Ok(connect()?.with_database(database)) +} + +fn connect() -> clickhouse::error::Result { + let https_connector = HttpsConnectorBuilder::new() + .with_native_roots()? + .https_or_http() + .enable_all_versions() + .build(); + let hyper_client = + hyper_util::client::legacy::Client::builder(TokioExecutor::new()) + .build(https_connector); + + Ok(clickhouse::Client::with_http_client(hyper_client) + .with_url(&ENV.CLICKHOUSE_URL) + .with_user(&ENV.CLICKHOUSE_USER) + .with_password(&ENV.CLICKHOUSE_PASSWORD) + .with_validation(false)) +} + +pub async fn run_migrations() -> clickhouse::error::Result<()> { + run_migrations_on_database(&ENV.CLICKHOUSE_DATABASE).await +} + +pub async fn run_migrations_on_database( + database: &str, +) -> clickhouse::error::Result<()> { const MINECRAFT_JAVA_SERVER_PINGS: &str = server_ping::CLICKHOUSE_TABLE; - let client = { - let https_connector = HttpsConnectorBuilder::new() - .with_native_roots()? - .https_or_http() - .enable_all_versions() - .build(); - let hyper_client = - hyper_util::client::legacy::Client::builder(TokioExecutor::new()) - .build(https_connector); - - clickhouse::Client::with_http_client(hyper_client) - .with_url(&ENV.CLICKHOUSE_URL) - .with_user(&ENV.CLICKHOUSE_USER) - .with_password(&ENV.CLICKHOUSE_PASSWORD) - .with_validation(false) - }; - - client - .query(&format!("CREATE DATABASE IF NOT EXISTS {database}")) - .execute() - .await?; + let client = connect()?; let clickhouse_replicated = ENV.CLICKHOUSE_REPLICATED; let cluster_line = if clickhouse_replicated { @@ -270,5 +277,5 @@ pub async fn init_client_with_database( .execute() .await?; - Ok(client.with_database(database)) + Ok(()) } diff --git a/apps/labrinth/src/main.rs b/apps/labrinth/src/main.rs index d78834911..b456b77a6 100644 --- a/apps/labrinth/src/main.rs +++ b/apps/labrinth/src/main.rs @@ -98,7 +98,7 @@ async fn app() -> std::io::Result<()> { info!("Starting labrinth on {}", &ENV.BIND_ADDR); if !args.no_migrations { - database::check_for_migrations() + labrinth::background_task::run_migrations() .await .expect("An error occurred while running migrations."); } diff --git a/apps/labrinth/src/test/mod.rs b/apps/labrinth/src/test/mod.rs index e81289e48..778439201 100644 --- a/apps/labrinth/src/test/mod.rs +++ b/apps/labrinth/src/test/mod.rs @@ -37,6 +37,7 @@ pub async fn setup(db: &database::TemporaryDatabase) -> LabrinthConfig { let search_backend = db.search_backend.clone(); let file_host: Arc = Arc::new(file_hosting::MockHost::new()); let file_host = web::Data::::from(file_host); + clickhouse::run_migrations().await.unwrap(); let mut clickhouse = clickhouse::init_client().await.unwrap(); let stripe_client = stripe::Client::new(ENV.STRIPE_API_KEY.clone());