Demo user and lipsum posts in prod

This commit is contained in:
Imbus 2023-12-14 22:50:37 +01:00
parent f10234b72f
commit 9ac8daf403
2 changed files with 77 additions and 1 deletions

View file

@ -50,10 +50,14 @@ impl ServerState {
Some(u) => info!("Created default user {}", u.username),
None => error!("Failed to create default user..."),
}
match crate::db::db_new_user("demouser".to_string(), "demopw".to_string(), &pool).await {
Some(u) => info!("Created default user {}", u.username),
None => error!("Failed to create default user..."),
}
#[cfg(debug_assertions)]
debug_setup(&pool).await.unwrap();
lipsum_setup(&pool).await.unwrap();
Self { pool }
}
}
@ -98,3 +102,31 @@ async fn debug_setup(pool: &PgPool) -> Result<(), sqlx::Error> {
Ok(())
}
async fn lipsum_setup(pool: &PgPool) -> Result<(), sqlx::Error> {
use lipsum::lipsum;
use rand::prelude::*;
use sqlx::query;
let user_exist = query!("SELECT * FROM users",)
.fetch_one(pool)
.await
.ok()
.is_none();
if user_exist {
let mut rng = rand::thread_rng();
// This requires that the user with id 1 exists in the user table
for _ in 0..100 {
query!(
"INSERT INTO posts (user_id, content) VALUES (1, $1)",
lipsum(rng.gen_range(10..100))
)
.execute(pool)
.await?;
}
}
Ok(())
}