Skip to content

Commit

Permalink
improved the transaction handling on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Dec 21, 2023
1 parent 7f8690d commit 59a5be3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
9 changes: 4 additions & 5 deletions server/main-api/src/setup/database/alias.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use log::info;
use serde::Deserialize;
use sqlx::PgPool;
use std::time::Instant;

#[derive(Debug)]
Expand Down Expand Up @@ -103,7 +102,9 @@ impl Alias {
}
}

pub(crate) async fn load_all_to_db(pool: &PgPool) -> Result<(), Box<dyn std::error::Error>> {
pub(crate) async fn load_all_to_db(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), Box<dyn std::error::Error>> {
let cdn_url = std::env::var("CDN_URL").unwrap_or_else(|_| "https://nav.tum.de/cdn".to_string());
let raw_aliase = reqwest::get(format!("{cdn_url}/api_data.json"))
.await?
Expand All @@ -114,11 +115,9 @@ pub(crate) async fn load_all_to_db(pool: &PgPool) -> Result<(), Box<dyn std::err
.into_iter()
.map(AliasIterator::from)
.flat_map(|alias| alias.into_iter());
let mut tx = pool.begin().await?;
for task in set_aliase {
task.store(&mut tx).await?;
task.store(tx).await?;
}
tx.commit().await?;
info!("loaded aliases in {elapsed:?}", elapsed = start.elapsed());

Ok(())
Expand Down
9 changes: 4 additions & 5 deletions server/main-api/src/setup/database/data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use log::info;
use serde_json::Value;
use sqlx::PgPool;
use std::collections::HashMap;
use std::time::Instant;

Expand Down Expand Up @@ -148,7 +147,9 @@ impl DelocalisedValues {
Ok(())
}
}
pub(crate) async fn load_all_to_db(pool: &PgPool) -> Result<(), Box<dyn std::error::Error>> {
pub(crate) async fn load_all_to_db(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), Box<dyn std::error::Error>> {
let start = Instant::now();
let cdn_url = std::env::var("CDN_URL").unwrap_or_else(|_| "https://nav.tum.de/cdn".to_string());
let tasks = reqwest::get(format!("{cdn_url}/api_data.json"))
Expand All @@ -159,11 +160,9 @@ pub(crate) async fn load_all_to_db(pool: &PgPool) -> Result<(), Box<dyn std::err
.map(DelocalisedValues::from);
info!("downloaded data in {elapsed:?}", elapsed = start.elapsed());
let start = Instant::now();
let mut tx = pool.begin().await?;
for task in tasks {
task.store(&mut tx).await?;
task.store(tx).await?;
}
tx.commit().await?;
info!("loaded data in {elapsed:?}", elapsed = start.elapsed());

Ok(())
Expand Down
26 changes: 18 additions & 8 deletions server/main-api/src/setup/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ mod alias;
mod data;

use log::info;
use sqlx::PgPool;

pub(crate) async fn setup_database(pool: &PgPool) -> Result<(), Box<dyn std::error::Error>> {
pub(crate) async fn setup_database(pool: &sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
sqlx::migrate!("./migrations").run(pool).await?;
info!("migrations complete");

info!("database setup complete, deleting old data");
sqlx::query!("DELETE FROM aliases").execute(pool).await?;
sqlx::query!("DELETE FROM en").execute(pool).await?;
sqlx::query!("DELETE FROM de").execute(pool).await?;
let mut tx = pool.begin().await?;
load_data(&mut tx).await?;
tx.commit().await?;
Ok(())
}
async fn load_data(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), Box<dyn std::error::Error>> {
info!("deleting old data");
sqlx::query!("DELETE FROM aliases")
.execute(&mut **tx)
.await?;
sqlx::query!("DELETE FROM en").execute(&mut **tx).await?;
sqlx::query!("DELETE FROM de").execute(&mut **tx).await?;

info!("loading new data");
data::load_all_to_db(pool).await?;
data::load_all_to_db(tx).await?;
info!("loading new aliases");
alias::load_all_to_db(pool).await?;
alias::load_all_to_db(tx).await?;
Ok(())
}

0 comments on commit 59a5be3

Please sign in to comment.