diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2024-01-23 09:33:44 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2024-01-23 09:33:44 +0100 |
commit | 82f6ce1b1f003903c0e8ffceafe17682ecd7fb88 (patch) | |
tree | b495570bf579214ba3581a8afeff6a67e6f31927 | |
parent | 47f59cfe19e7d4cc43b4a19c746504016b0881d8 (diff) | |
download | cats-radio-node-82f6ce1b1f003903c0e8ffceafe17682ecd7fb88.tar.gz cats-radio-node-82f6ce1b1f003903c0e8ffceafe17682ecd7fb88.tar.bz2 cats-radio-node-82f6ce1b1f003903c0e8ffceafe17682ecd7fb88.zip |
Create database if it doesn't exist
-rw-r--r-- | cats-radio-node.db | 0 | ||||
-rw-r--r-- | src/db.rs | 14 |
2 files changed, 14 insertions, 0 deletions
diff --git a/cats-radio-node.db b/cats-radio-node.db deleted file mode 100644 index e69de29..0000000 --- a/cats-radio-node.db +++ /dev/null @@ -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(); |