From d1c8d66cca277bbb9c8df51c5033e74bc1309b22 Mon Sep 17 00:00:00 2001 From: liyunze <50455574+liyunze-coding@users.noreply.github.com> Date: Tue, 6 Jan 2026 22:41:28 +0800 Subject: [PATCH] set database to absolute path --- .forgejo/workflows/deploy.yml | 1 + README.md | 1 + src/database/database.rs | 11 +++++------ src/main.rs | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.forgejo/workflows/deploy.yml b/.forgejo/workflows/deploy.yml index 28b142c..6fafe57 100644 --- a/.forgejo/workflows/deploy.yml +++ b/.forgejo/workflows/deploy.yml @@ -16,6 +16,7 @@ jobs: run: | cat > .env << EOF DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} + DATABASE_PATH=${{ secrets.DATABASE_PATH }} EOF chmod 600 .env diff --git a/README.md b/README.md index d451906..f34decb 100644 --- a/README.md +++ b/README.md @@ -10,5 +10,6 @@ To do: FAQ table - ID: Primary Key +- Guild ID (Server ID): FK string - title: string - description: string \ No newline at end of file diff --git a/src/database/database.rs b/src/database/database.rs index ab0143d..f01aa79 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -1,16 +1,15 @@ -use std::path::Path; +use std::{env, path::Path}; // Using SQLite use rusqlite::{Connection, OpenFlags, Result}; -pub fn init_db(filename: &str) -> Result { - let filepath = Path::new(filename); +pub fn init_db() -> Result { + let filename = env::var("DATABASE_PATH").unwrap_or_else(|_| "bot_data.sqlite".to_string()); + let filepath = Path::new(&filename); let open_flags = OpenFlags::default(); - let conn = Connection::open_with_flags(filepath, open_flags); - - conn + Connection::open_with_flags(filepath, open_flags) } fn table_exists(conn: &Connection, table_name: &str) -> Result { diff --git a/src/main.rs b/src/main.rs index 43db73e..5f50af0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,7 +70,7 @@ async fn main() { dotenv::dotenv().ok(); // SQLite - let conn = init_db("bot_data.sqlite").expect("Failed to start database"); + let conn = init_db().expect("Failed to start database"); init_tables(&conn).expect("Failed to init tables"); let conn = Arc::new(Mutex::new(conn));