From 82f6ce1b1f003903c0e8ffceafe17682ecd7fb88 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 23 Jan 2024 09:33:44 +0100 Subject: Create database if it doesn't exist --- cats-radio-node.db | 0 src/db.rs | 14 ++++++++++++++ 2 files changed, 14 insertions(+) delete mode 100644 cats-radio-node.db diff --git a/cats-radio-node.db b/cats-radio-node.db deleted file mode 100644 index e69de29..0000000 diff --git a/src/db.rs b/src/db.rs index 5d0f627..d5285ba 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,4 +1,5 @@ use std::time::{SystemTime, UNIX_EPOCH}; +use std::io; use log::debug; use sqlx::SqlitePool; @@ -18,6 +19,19 @@ pub struct Packet { impl Database { pub async fn new() -> Self { + { + // Ensure the database file exists + match std::fs::OpenOptions::new().write(true) + .create_new(true) + .open("cats-radio-node.db") { + Ok(_f) => (), + Err(e) if e.kind() == io::ErrorKind::AlreadyExists => (), + Err(e) => { + panic!("Failed to ensure DB exists: {e}"); + }, + } + } + let pool = SqlitePool::connect("sqlite:cats-radio-node.db").await.unwrap(); let mut conn = pool.acquire().await.unwrap(); -- cgit v1.2.3