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

@ -0,0 +1,44 @@
{
"db_name": "PostgreSQL",
"query": "SELECT * FROM users",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int8"
},
{
"ordinal": 1,
"name": "username",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "password",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "created_at",
"type_info": "Timestamp"
},
{
"ordinal": 4,
"name": "updated_at",
"type_info": "Timestamp"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
false,
false,
false,
false
]
},
"hash": "26e7e05427bc7dabcd7815d27764fda2baf4cfe60a2d2d6ee2a1f773dccbbce2"
}

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(())
}