From b3124948c4656542dc18509973f740cfed8a0588 Mon Sep 17 00:00:00 2001 From: Imbus Date: Sat, 4 Nov 2023 21:53:55 +0100 Subject: [PATCH] Create sqlite db if not exist, constrain debug deps to debug builds --- justfile | 2 +- server/src/state.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/justfile b/justfile index bedd409..1ec67a7 100644 --- a/justfile +++ b/justfile @@ -32,7 +32,7 @@ build-container-release: # Builds a release container and runs it start-release: build-container-release remove-podman-containers - {{runtime}} run -d -p 8080:8080 --name frostbyte fb-server + {{runtime}} run -d -e DATABASE_URL=sqlite:release.db -p 8080:8080 --name frostbyte fb-server init-sqlx: echo "DATABASE_URL=sqlite:debug.db" > server/.env diff --git a/server/src/state.rs b/server/src/state.rs index 43b2519..b828927 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -4,9 +4,9 @@ use std::sync::Mutex; use log::error; use log::info; +use sqlx::migrate::MigrateDatabase; use sqlx::Pool; use sqlx::Sqlite; -use sqlx::SqlitePool; use sqlx::{self, sqlite}; #[derive(Clone)] @@ -34,6 +34,10 @@ impl ServerState { let db_url = dotenvy::var("DATABASE_URL").unwrap_or(":memory:".to_string()); info!("Using db_url: {}", &db_url); + if !sqlx::Sqlite::database_exists(&db_url).await.unwrap() { + sqlx::Sqlite::create_database(&db_url).await.unwrap(); + } + let pool = sqlite::SqlitePoolOptions::new() .max_connections(5) .connect(&db_url) @@ -55,6 +59,9 @@ impl ServerState { } } +#[cfg(debug_assertions)] +use sqlx::SqlitePool; + // Inserts a bunch of dummy data into the database // Mostly useful for debugging new posts, as we need to satisfy foreign key constraints. #[cfg(debug_assertions)]