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",
"serde",
"tokio",
"tracing",
"tracing-subscriber",
]
[[package]]

View File

@ -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

View File

@ -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"

View File

@ -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,79 +19,139 @@ 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 {
let r = Tags::insert(ActiveModel {
id: NotSet,
name: Set(fake::faker::name::en::FirstName().fake()),
})
.exec_with_returning(&db)
.await
.unwrap(),
);
.exec_with_returning(&mut t)
.await;
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 {
use entities::tags::ActiveModel;
tags.push(
Tags::insert(ActiveModel {
let r = Tags::insert(ActiveModel {
id: NotSet,
name: Set(name.to_string()),
})
.exec_with_returning(&db)
.await
.unwrap(),
);
.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 {
let r = Recipies::insert(ActiveModel {
id: NotSet,
title: Set(name.to_string()),
image_url: Set(format!(
"https://picsum.photos/{w}/{h}",
w = fake::rand::random::<i32>() + 200,
h = fake::rand::random::<i32>() + 200
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(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()),
})
.exec_with_returning(&db)
.await
.unwrap(),
);
.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 {
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().get_range(4..8)),
unit: Set(Some(UNIT.choose(&mut rand::thread_rng()))),
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::<Vec<_>>(),
)
.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::<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] = [
"Almond Meal",
@ -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"];