From fcc69d2a91f14c0d75e2b108aedf1d99421dd727 Mon Sep 17 00:00:00 2001 From: Joe Bellus Date: Fri, 11 Feb 2022 16:34:33 -0500 Subject: [PATCH] Database initialization During startup, an empty data.db is generated if it doesnt exist. This will then be migrated. --- src/error.rs | 9 +++++++++ src/main.rs | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/error.rs b/src/error.rs index 71e334b..008bb95 100644 --- a/src/error.rs +++ b/src/error.rs @@ -40,6 +40,15 @@ impl Error { } } +impl From for Error { + fn from(source: std::io::Error) -> Self { + Self { + code: ErrorCode::Internal, + message: source.to_string(), + } + } +} + impl From<&str> for Error { fn from(s: &str) -> Self { Error { diff --git a/src/main.rs b/src/main.rs index 1dd2fea..afaf998 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::path::Path; + use actix_web::{get, web, App, HttpResponse, HttpServer}; use rust_embed::RustEmbed; use sea_orm::{Database, DatabaseConnection}; @@ -71,6 +73,12 @@ async fn dist(path: web::Path) -> HttpResponse { #[instrument] async fn setup_database() -> error::Result { + let db_fname = "data.db"; + + if !Path::new(db_fname).exists() { + std::fs::File::create(db_fname)?; + } + let pool = sqlx::SqlitePool::connect("sqlite://data.db").await?; sqlx::migrate!("./migrations").run(&pool).await?; tracing::info!("Database migrated");