This commit is contained in:
Adrian Woźniak 2024-10-31 16:54:27 +01:00
parent fead4e970a
commit e8f7f46fb8
4 changed files with 124 additions and 58 deletions

2
Cargo.lock generated
View File

@ -4069,6 +4069,8 @@ dependencies = [
"sea-orm", "sea-orm",
"serde", "serde",
"tokio", "tokio",
"tracing",
"tracing-subscriber",
] ]
[[package]] [[package]]

View File

@ -1,4 +1,3 @@
psql -b cooked postgres -h localhost < ./seed/recipies.sql && export PSQL=postgres://postgres@localhost/cooked
psql -b cooked postgres -h localhost < ./seed/recipe_tags.sql && export RUST_LOG=debug
psql -b cooked postgres -h localhost < ./seed/recipe_ingeredients.sql && cargo run --bin seed
psql -b cooked postgres -h localhost < ./seed/recipe_steps.sql

View File

@ -13,3 +13,5 @@ entities = { path = "../entities" }
migration = { path = "../migration" } migration = { path = "../migration" }
fake = "3.0.0" fake = "3.0.0"
rand = "0.8.5" rand = "0.8.5"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

View File

@ -7,6 +7,7 @@ use sea_orm::*;
#[actix_web::main] #[actix_web::main]
async fn main() { async fn main() {
tracing_subscriber::fmt::init();
let psql = let psql =
std::env::var("PSQL").expect("PSQL is required. Please provide postgresql connection url"); std::env::var("PSQL").expect("PSQL is required. Please provide postgresql connection url");
let db = Database::connect(&psql) let db = Database::connect(&psql)
@ -18,79 +19,139 @@ async fn main() {
Migrator::up(&db, None).await.unwrap(); Migrator::up(&db, None).await.unwrap();
} }
let mut t = db.begin().await.unwrap();
let mut tags = Vec::with_capacity(30); let mut tags = Vec::with_capacity(30);
for _ in 0..30 { for _ in 0..30 {
use entities::tags::ActiveModel; use entities::tags::ActiveModel;
tags.push( let r = Tags::insert(ActiveModel {
Tags::insert(ActiveModel {
id: NotSet, id: NotSet,
name: Set(fake::faker::name::en::FirstName().fake()), name: Set(fake::faker::name::en::FirstName().fake()),
}) })
.exec_with_returning(&db) .exec_with_returning(&mut t)
.await .await;
.unwrap(), if let Err(e) = &r {
); tracing::error!("{e}");
t.rollback().await.unwrap();
panic!("{e}")
} }
let mut ingredients = Vec::with_capacity(INGREDIENTS.len()); tags.push(r.unwrap());
}
let ingredients = Vec::with_capacity(INGREDIENTS.len());
for name in INGREDIENTS { for name in INGREDIENTS {
use entities::tags::ActiveModel; use entities::tags::ActiveModel;
tags.push( let r = Tags::insert(ActiveModel {
Tags::insert(ActiveModel {
id: NotSet, id: NotSet,
name: Set(name.to_string()), name: Set(name.to_string()),
}) })
.exec_with_returning(&db) .exec_with_returning(&mut t)
.await .await;
.unwrap(), 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); let mut recipies = Vec::with_capacity(1_000);
for name in DISHES { for name in DISHES {
use entities::recipies::ActiveModel; use entities::recipies::ActiveModel;
recipies.push( let r = Recipies::insert(ActiveModel {
Recipies::insert(ActiveModel {
id: NotSet, id: NotSet,
title: Set(name.to_string()), title: Set(name.to_string()),
image_url: Set(format!( image_url: Set(format!(
"https://picsum.photos/{w}/{h}", "https://picsum.photos/{w}/{h}",
w = fake::rand::random::<i32>() + 200, w = rand::thread_rng().gen_range(300..400),
h = fake::rand::random::<i32>() + 200 h = rand::thread_rng().gen_range(200..300)
)), )),
author: Set(Some(fake::faker::name::en::FirstName().fake())), author: Set(Some(fake::faker::name::en::FirstName().fake())),
time: Set(Some(fake::rand::random::<i32>() * 15)), time: Set(Some(rand::thread_rng().gen_range(20..100) * 15)),
summary: Set(fake::faker::lorem::en::Paragraph(2..4).fake()), summary: Set(fake::faker::lorem::en::Paragraph(2..4).fake()),
}) })
.exec_with_returning(&db) .exec_with_returning(&mut t)
.await .await;
.unwrap(), if let Err(e) = &r {
); tracing::error!("{e}");
t.rollback().await.unwrap();
panic!("{e}")
}
recipies.push(r.unwrap());
} }
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
{ {
use entities::recipe_ingredients::ActiveModel; use entities::recipe_ingredients::ActiveModel;
RecipeIngredients::insert_many( for row in recipies.iter().flat_map(|recipe| {
recipies ingredients
.iter() .choose_multiple(&mut rand::thread_rng(), rand::thread_rng().gen_range(3..6))
.flat_map(|recipe| { .map(|ingredient: &entities::ingredients::Model| ActiveModel {
(0..rand::thread_rng().gen_range(3..6))
.map(|_| ingredients.choose(&mut rand::thread_rng()))
.map(|igredient| ActiveModel {
id: NotSet, id: NotSet,
ingredient_id: Set(ingredient.id), ingredient_id: Set(ingredient.id),
qty: Set(rand::thread_rng().get_range(4..8)), qty: Set(rand::thread_rng().gen_range(4..8)),
unit: Set(Some(UNIT.choose(&mut rand::thread_rng()))), unit: Set(UNIT
.choose(&mut rand::thread_rng())
.map(|s| s.to_string())
.unwrap_or("szt".to_string())),
recipe_id: Set(recipe.id), recipe_id: Set(recipe.id),
}) })
}) .inspect(|row| tracing::info!("recipe ingredient: {row:#?}"))
.collect::<Vec<_>>(), }) {
) let r = RecipeIngredients::insert(row).exec(&mut t).await;
.exec(&db) if let Err(e) = r {
.await tracing::error!("{e}");
.unwrap(); 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::<Vec<String>>()
.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] = [ const INGREDIENTS: [&'static str; 69] = [
"Almond Meal", "Almond Meal",
@ -263,3 +324,5 @@ const DISHES: [&'static str; 96] = [
"Tamatar Chaat", "Tamatar Chaat",
"Tandoori Fish Tikka", "Tandoori Fish Tikka",
]; ];
const UNIT: [&'static str; 6] = ["kg", "g", "łyżka stołowa", "łyżeczka", "szt", "szklanki"];