From 3a007f00931e0dc30c4e5e908d5b303aa93f5344 Mon Sep 17 00:00:00 2001 From: Imbus Date: Sat, 21 Oct 2023 08:58:21 +0200 Subject: [PATCH] Useful debug post that only happens once per db --- server/src/state.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/server/src/state.rs b/server/src/state.rs index 7d038fd..6264987 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -56,10 +56,19 @@ async fn debug_setup(pool: &SqlitePool) -> Result<(), sqlx::Error> { db_new_user("user".to_string(), "pass".to_string(), pool).await; - // This requires that the user with id 1 exists in the user table - query!("INSERT INTO posts (user_id, content) VALUES (1, 'Hello world!')",) + // Check if the demo post already exists + let posted = query!("SELECT * FROM posts WHERE id = 1",) + .fetch_one(pool) + .await + .ok(); + + // If the demo user already has a post, don't insert another one + if !posted.is_some() { + // This requires that the user with id 1 exists in the user table + query!("INSERT INTO posts (user_id, content) VALUES (1, 'Hello world! The demo username is user and the password is pass.')",) .execute(pool) .await?; + } Ok(()) }