From e8f7f46fb8503b469a7226cacb5345ffe672606a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Wo=C5=BAniak?= Date: Thu, 31 Oct 2024 16:54:27 +0100 Subject: [PATCH] changes --- Cargo.lock | 2 + scripts/seed.sh | 7 +- seed/Cargo.toml | 2 + seed/src/main.rs | 171 ++++++++++++++++++++++++++++++++--------------- 4 files changed, 124 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 65b64f0..2fa83ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4069,6 +4069,8 @@ dependencies = [ "sea-orm", "serde", "tokio", + "tracing", + "tracing-subscriber", ] [[package]] diff --git a/scripts/seed.sh b/scripts/seed.sh index 45d3d10..1c6799f 100755 --- a/scripts/seed.sh +++ b/scripts/seed.sh @@ -1,4 +1,3 @@ - psql -b cooked postgres -h localhost < ./seed/recipies.sql && - psql -b cooked postgres -h localhost < ./seed/recipe_tags.sql && - psql -b cooked postgres -h localhost < ./seed/recipe_ingeredients.sql && - psql -b cooked postgres -h localhost < ./seed/recipe_steps.sql +export PSQL=postgres://postgres@localhost/cooked +export RUST_LOG=debug +cargo run --bin seed diff --git a/seed/Cargo.toml b/seed/Cargo.toml index 1d8d97c..2f10fe9 100644 --- a/seed/Cargo.toml +++ b/seed/Cargo.toml @@ -13,3 +13,5 @@ entities = { path = "../entities" } migration = { path = "../migration" } fake = "3.0.0" rand = "0.8.5" +tracing = "0.1.40" +tracing-subscriber = "0.3.18" diff --git a/seed/src/main.rs b/seed/src/main.rs index 3ec7eb8..2342c8e 100644 --- a/seed/src/main.rs +++ b/seed/src/main.rs @@ -7,6 +7,7 @@ use sea_orm::*; #[actix_web::main] async fn main() { + tracing_subscriber::fmt::init(); let psql = std::env::var("PSQL").expect("PSQL is required. Please provide postgresql connection url"); let db = Database::connect(&psql) @@ -18,78 +19,138 @@ async fn main() { Migrator::up(&db, None).await.unwrap(); } + let mut t = db.begin().await.unwrap(); + let mut tags = Vec::with_capacity(30); for _ in 0..30 { use entities::tags::ActiveModel; - tags.push( - Tags::insert(ActiveModel { - id: NotSet, - name: Set(fake::faker::name::en::FirstName().fake()), - }) - .exec_with_returning(&db) - .await - .unwrap(), - ); + let r = Tags::insert(ActiveModel { + id: NotSet, + name: Set(fake::faker::name::en::FirstName().fake()), + }) + .exec_with_returning(&mut t) + .await; + if let Err(e) = &r { + tracing::error!("{e}"); + t.rollback().await.unwrap(); + panic!("{e}") + } + tags.push(r.unwrap()); } - let mut ingredients = Vec::with_capacity(INGREDIENTS.len()); + let ingredients = Vec::with_capacity(INGREDIENTS.len()); for name in INGREDIENTS { use entities::tags::ActiveModel; - tags.push( - Tags::insert(ActiveModel { - id: NotSet, - name: Set(name.to_string()), - }) - .exec_with_returning(&db) - .await - .unwrap(), - ); + let r = Tags::insert(ActiveModel { + id: NotSet, + name: Set(name.to_string()), + }) + .exec_with_returning(&mut t) + .await; + if let Err(e) = &r { + tracing::error!("{e}"); + t.rollback().await.unwrap(); + panic!("{e}") + } + tags.push(r.unwrap()); } let mut recipies = Vec::with_capacity(1_000); for name in DISHES { use entities::recipies::ActiveModel; - recipies.push( - Recipies::insert(ActiveModel { - id: NotSet, - title: Set(name.to_string()), - image_url: Set(format!( - "https://picsum.photos/{w}/{h}", - w = fake::rand::random::() + 200, - h = fake::rand::random::() + 200 - )), - author: Set(Some(fake::faker::name::en::FirstName().fake())), - time: Set(Some(fake::rand::random::() * 15)), - summary: Set(fake::faker::lorem::en::Paragraph(2..4).fake()), - }) - .exec_with_returning(&db) - .await - .unwrap(), - ); + let r = Recipies::insert(ActiveModel { + id: NotSet, + title: Set(name.to_string()), + image_url: Set(format!( + "https://picsum.photos/{w}/{h}", + w = rand::thread_rng().gen_range(300..400), + h = rand::thread_rng().gen_range(200..300) + )), + author: Set(Some(fake::faker::name::en::FirstName().fake())), + time: Set(Some(rand::thread_rng().gen_range(20..100) * 15)), + summary: Set(fake::faker::lorem::en::Paragraph(2..4).fake()), + }) + .exec_with_returning(&mut t) + .await; + if let Err(e) = &r { + tracing::error!("{e}"); + t.rollback().await.unwrap(); + panic!("{e}") + } + recipies.push(r.unwrap()); } use rand::seq::SliceRandom; { use entities::recipe_ingredients::ActiveModel; - RecipeIngredients::insert_many( - recipies - .iter() - .flat_map(|recipe| { - (0..rand::thread_rng().gen_range(3..6)) - .map(|_| ingredients.choose(&mut rand::thread_rng())) - .map(|igredient| ActiveModel { - id: NotSet, - ingredient_id: Set(ingredient.id), - qty: Set(rand::thread_rng().get_range(4..8)), - unit: Set(Some(UNIT.choose(&mut rand::thread_rng()))), - recipe_id: Set(recipe.id), - }) + for row in recipies.iter().flat_map(|recipe| { + ingredients + .choose_multiple(&mut rand::thread_rng(), rand::thread_rng().gen_range(3..6)) + .map(|ingredient: &entities::ingredients::Model| ActiveModel { + id: NotSet, + ingredient_id: Set(ingredient.id), + qty: Set(rand::thread_rng().gen_range(4..8)), + unit: Set(UNIT + .choose(&mut rand::thread_rng()) + .map(|s| s.to_string()) + .unwrap_or("szt".to_string())), + recipe_id: Set(recipe.id), }) - .collect::>(), - ) - .exec(&db) - .await - .unwrap(); + .inspect(|row| tracing::info!("recipe ingredient: {row:#?}")) + }) { + let r = RecipeIngredients::insert(row).exec(&mut t).await; + if let Err(e) = r { + tracing::error!("{e}"); + t.rollback().await.unwrap(); + panic!("{e}") + } + } } + { + use entities::recipe_steps::ActiveModel; + for row in recipies + .iter() + .flat_map(|recipe| { + (3..rand::thread_rng().gen_range(5..7)).map(|_: i8| ActiveModel { + body: Set(fake::faker::lorem::en::Paragraphs(2..5) + .fake::>() + .join("\n")), + hint: Set(rand::thread_rng() + .gen_bool(0.4) + .then(|| fake::faker::lorem::en::Paragraph(2..3).fake())), + recipe_id: Set(recipe.id), + ..Default::default() + }) + }) + .inspect(|row| tracing::info!("recipe step: {row:?}")) + { + let r = RecipeSteps::insert(row).exec(&mut t).await; + if let Err(e) = r { + tracing::error!("{e}"); + t.rollback().await.unwrap(); + panic!("{e}") + } + } + } + { + use entities::recipe_tags::ActiveModel; + for row in recipies.iter().flat_map(|recipe| { + tags.choose_multiple(&mut rand::thread_rng(), rand::thread_rng().gen_range(3..6)) + .map(|tag: &entities::tags::Model| ActiveModel { + id: NotSet, + tag_id: Set(tag.id), + recipe_id: Set(recipe.id), + }) + }) { + let r = RecipeTags::insert(row).exec(&mut t).await; + if let Err(e) = r { + tracing::error!("{e}"); + t.rollback().await.unwrap(); + panic!("{e}") + } + } + } + + t.commit().await.unwrap(); } const INGREDIENTS: [&'static str; 69] = [ @@ -263,3 +324,5 @@ const DISHES: [&'static str; 96] = [ "Tamatar Chaat", "Tandoori Fish Tikka", ]; + +const UNIT: [&'static str; 6] = ["kg", "g", "łyżka stołowa", "łyżeczka", "szt", "szklanki"];