Database initialization

During startup, an empty data.db is generated if it doesnt exist. This will then be
migrated.
This commit is contained in:
Joe Bellus 2022-02-11 16:34:33 -05:00
parent 0bf72ecb91
commit fcc69d2a91
2 changed files with 17 additions and 0 deletions

View File

@ -40,6 +40,15 @@ impl Error {
}
}
impl From<std::io::Error> 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 {

View File

@ -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<String>) -> HttpResponse {
#[instrument]
async fn setup_database() -> error::Result<DatabaseConnection> {
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");