re-added sqlite check_db code, cleanup

This commit is contained in:
Nils Domrose 2019-05-27 22:58:52 +02:00
parent 6c38026ef5
commit dc36f0cb6c
4 changed files with 28 additions and 11 deletions

View File

@ -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"

View File

@ -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

View File

@ -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<ConnectionManager<Connection>>;
/// Connection request guard type: a wrapper around an r2d2 pooled connection.

View File

@ -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");
}