From dc36f0cb6c47017e9e188ca55e42fdb75b7b87ce Mon Sep 17 00:00:00 2001 From: Nils Domrose Date: Mon, 27 May 2019 22:58:52 +0200 Subject: [PATCH] re-added sqlite check_db code, cleanup --- Cargo.toml | 4 ++-- src/config.rs | 2 -- src/db/mod.rs | 10 +++------- src/main.rs | 23 +++++++++++++++++++++++ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 292bfb3..1260c9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ build = "build.rs" [features] # Empty to keep compatibility, prefer to set USE_SYSLOG=true enable_syslog = [] -mysql = [] -sqlite = [] +mysql = ["diesel/mysql", "diesel_migrations/mysql"] +sqlite = ["diesel/sqlite", "diesel_migrations/sqlite"] [target."cfg(not(windows))".dependencies] syslog = "4.0.1" diff --git a/src/config.rs b/src/config.rs index be0b515..b17df57 100644 --- a/src/config.rs +++ b/src/config.rs @@ -202,8 +202,6 @@ make_config! { folders { /// Data folder |> Main data folder data_folder: String, false, def, "data".to_string(); - - /// Database URL /// Database URL database_url: String, false, auto, |c| format!("{}/{}", c.data_folder, "db.sqlite3"); /// Icon chache folder diff --git a/src/db/mod.rs b/src/db/mod.rs index 6a60ae4..9d7a9f4 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,10 +2,6 @@ use std::ops::Deref; use diesel::r2d2; use diesel::r2d2::ConnectionManager; -#[cfg(feature = "sqlite")] -use diesel::sqlite::SqliteConnection; -#[cfg(feature = "mysql")] -use diesel::mysql::MysqlConnection; use diesel::{Connection as DieselConnection, ConnectionError}; use rocket::http::Status; @@ -16,11 +12,11 @@ use crate::CONFIG; /// An alias to the database connection used #[cfg(feature = "sqlite")] -type Connection = SqliteConnection; +type Connection = diesel::sqlite::SqliteConnection; #[cfg(feature = "mysql")] -type Connection = MysqlConnection; +type Connection = diesel::mysql::MysqlConnection; -/// An alias to the type for a pool of Diesel MySQL connections. +/// An alias to the type for a pool of Diesel connections. type Pool = r2d2::Pool>; /// Connection request guard type: a wrapper around an r2d2 pooled connection. diff --git a/src/main.rs b/src/main.rs index 21e55eb..5526ef7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,9 @@ fn main() { init_logging().ok(); } + #[cfg(all(feature = "sqlite", feature = "mysql"))] + compile_error!("Can't enable both backends"); + check_db(); check_rsa_keys(); check_web_vault(); @@ -123,6 +126,26 @@ fn chain_syslog(logger: fern::Dispatch) -> fern::Dispatch { fn check_db() { let url = CONFIG.database_url(); + if cfg!(feature = "sqlite") { + let path = Path::new(&url); + + if let Some(parent) = path.parent() { + use std::fs; + if fs::create_dir_all(parent).is_err() { + error!("Error creating database directory"); + exit(1); + } + } + + // Turn on WAL in SQLite + if CONFIG.enable_db_wal() { + use diesel::RunQueryDsl; + let connection = db::get_connection().expect("Can't conect to DB"); + diesel::sql_query("PRAGMA journal_mode=wal") + .execute(&connection) + .expect("Failed to turn on WAL"); + } + } println!("{}", url.to_string()); db::get_connection().expect("Can't conect to DB"); }