fix: clickhouse migrations should not run normally

This commit is contained in:
Michael H.
2026-07-30 19:23:10 +02:00
parent 0a53211389
commit 572b8027ff
4 changed files with 34 additions and 23 deletions
+3
View File
@@ -168,6 +168,9 @@ pub async fn update_bank_balances(pool: PgPool) -> eyre::Result<()> {
pub async fn run_migrations() -> eyre::Result<()> { pub async fn run_migrations() -> eyre::Result<()> {
database::check_for_migrations().await?; database::check_for_migrations().await?;
crate::clickhouse::run_migrations()
.await
.wrap_err("failed to run ClickHouse migrations")?;
Ok(()) Ok(())
} }
+29 -22
View File
@@ -19,29 +19,36 @@ pub async fn init_client() -> clickhouse::error::Result<clickhouse::Client> {
pub async fn init_client_with_database( pub async fn init_client_with_database(
database: &str, database: &str,
) -> clickhouse::error::Result<clickhouse::Client> { ) -> clickhouse::error::Result<clickhouse::Client> {
Ok(connect()?.with_database(database))
}
fn connect() -> clickhouse::error::Result<clickhouse::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);
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; const MINECRAFT_JAVA_SERVER_PINGS: &str = server_ping::CLICKHOUSE_TABLE;
let client = { let client = connect()?;
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 clickhouse_replicated = ENV.CLICKHOUSE_REPLICATED; let clickhouse_replicated = ENV.CLICKHOUSE_REPLICATED;
let cluster_line = if clickhouse_replicated { let cluster_line = if clickhouse_replicated {
@@ -270,5 +277,5 @@ pub async fn init_client_with_database(
.execute() .execute()
.await?; .await?;
Ok(client.with_database(database)) Ok(())
} }
+1 -1
View File
@@ -98,7 +98,7 @@ async fn app() -> std::io::Result<()> {
info!("Starting labrinth on {}", &ENV.BIND_ADDR); info!("Starting labrinth on {}", &ENV.BIND_ADDR);
if !args.no_migrations { if !args.no_migrations {
database::check_for_migrations() labrinth::background_task::run_migrations()
.await .await
.expect("An error occurred while running migrations."); .expect("An error occurred while running migrations.");
} }
+1
View File
@@ -37,6 +37,7 @@ pub async fn setup(db: &database::TemporaryDatabase) -> LabrinthConfig {
let search_backend = db.search_backend.clone(); let search_backend = db.search_backend.clone();
let file_host: Arc<dyn FileHost> = Arc::new(file_hosting::MockHost::new()); let file_host: Arc<dyn FileHost> = Arc::new(file_hosting::MockHost::new());
let file_host = web::Data::<dyn FileHost>::from(file_host); let file_host = web::Data::<dyn FileHost>::from(file_host);
clickhouse::run_migrations().await.unwrap();
let mut clickhouse = clickhouse::init_client().await.unwrap(); let mut clickhouse = clickhouse::init_client().await.unwrap();
let stripe_client = stripe::Client::new(ENV.STRIPE_API_KEY.clone()); let stripe_client = stripe::Client::new(ENV.STRIPE_API_KEY.clone());