set database to absolute path

This commit is contained in:
liyunze 2026-01-06 22:41:28 +08:00
parent a47bb29e7d
commit d1c8d66cca
4 changed files with 8 additions and 7 deletions

View file

@ -16,6 +16,7 @@ jobs:
run: | run: |
cat > .env << EOF cat > .env << EOF
DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}
DATABASE_PATH=${{ secrets.DATABASE_PATH }}
EOF EOF
chmod 600 .env chmod 600 .env

View file

@ -10,5 +10,6 @@ To do:
FAQ table FAQ table
- ID: Primary Key - ID: Primary Key
- Guild ID (Server ID): FK string
- title: string - title: string
- description: string - description: string

View file

@ -1,16 +1,15 @@
use std::path::Path; use std::{env, path::Path};
// Using SQLite // Using SQLite
use rusqlite::{Connection, OpenFlags, Result}; use rusqlite::{Connection, OpenFlags, Result};
pub fn init_db(filename: &str) -> Result<Connection, rusqlite::Error> { pub fn init_db() -> Result<Connection, rusqlite::Error> {
let filepath = Path::new(filename); 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 open_flags = OpenFlags::default();
let conn = Connection::open_with_flags(filepath, open_flags); Connection::open_with_flags(filepath, open_flags)
conn
} }
fn table_exists(conn: &Connection, table_name: &str) -> Result<bool> { fn table_exists(conn: &Connection, table_name: &str) -> Result<bool> {

View file

@ -70,7 +70,7 @@ async fn main() {
dotenv::dotenv().ok(); dotenv::dotenv().ok();
// SQLite // 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"); init_tables(&conn).expect("Failed to init tables");
let conn = Arc::new(Mutex::new(conn)); let conn = Arc::new(Mutex::new(conn));