changes
This commit is contained in:
parent
fead4e970a
commit
e8f7f46fb8
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -4069,6 +4069,8 @@ dependencies = [
|
|||||||
"sea-orm",
|
"sea-orm",
|
||||||
"serde",
|
"serde",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tracing",
|
||||||
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -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
|
|
||||||
|
@ -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"
|
||||||
|
171
seed/src/main.rs
171
seed/src/main.rs
@ -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,78 +19,138 @@ 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(&mut t)
|
||||||
.exec_with_returning(&db)
|
.await;
|
||||||
.await
|
if let Err(e) = &r {
|
||||||
.unwrap(),
|
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 {
|
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(&mut t)
|
||||||
.exec_with_returning(&db)
|
.await;
|
||||||
.await
|
if let Err(e) = &r {
|
||||||
.unwrap(),
|
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 = rand::thread_rng().gen_range(300..400),
|
||||||
w = fake::rand::random::<i32>() + 200,
|
h = rand::thread_rng().gen_range(200..300)
|
||||||
h = fake::rand::random::<i32>() + 200
|
)),
|
||||||
)),
|
author: Set(Some(fake::faker::name::en::FirstName().fake())),
|
||||||
author: Set(Some(fake::faker::name::en::FirstName().fake())),
|
time: Set(Some(rand::thread_rng().gen_range(20..100) * 15)),
|
||||||
time: Set(Some(fake::rand::random::<i32>() * 15)),
|
summary: Set(fake::faker::lorem::en::Paragraph(2..4).fake()),
|
||||||
summary: Set(fake::faker::lorem::en::Paragraph(2..4).fake()),
|
})
|
||||||
})
|
.exec_with_returning(&mut t)
|
||||||
.exec_with_returning(&db)
|
.await;
|
||||||
.await
|
if let Err(e) = &r {
|
||||||
.unwrap(),
|
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))
|
id: NotSet,
|
||||||
.map(|_| ingredients.choose(&mut rand::thread_rng()))
|
ingredient_id: Set(ingredient.id),
|
||||||
.map(|igredient| ActiveModel {
|
qty: Set(rand::thread_rng().gen_range(4..8)),
|
||||||
id: NotSet,
|
unit: Set(UNIT
|
||||||
ingredient_id: Set(ingredient.id),
|
.choose(&mut rand::thread_rng())
|
||||||
qty: Set(rand::thread_rng().get_range(4..8)),
|
.map(|s| s.to_string())
|
||||||
unit: Set(Some(UNIT.choose(&mut rand::thread_rng()))),
|
.unwrap_or("szt".to_string())),
|
||||||
recipe_id: Set(recipe.id),
|
recipe_id: Set(recipe.id),
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>(),
|
.inspect(|row| tracing::info!("recipe ingredient: {row:#?}"))
|
||||||
)
|
}) {
|
||||||
.exec(&db)
|
let r = RecipeIngredients::insert(row).exec(&mut t).await;
|
||||||
.await
|
if let Err(e) = r {
|
||||||
.unwrap();
|
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] = [
|
const INGREDIENTS: [&'static str; 69] = [
|
||||||
@ -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"];
|
||||||
|
Loading…
Reference in New Issue
Block a user