Database sanity settings
This commit is contained in:
parent
95f6c62531
commit
e64435efa5
1 changed files with 20 additions and 4 deletions
|
@ -1,6 +1,7 @@
|
||||||
use anyhow::*;
|
use anyhow::*;
|
||||||
use diesel::r2d2::{self, ConnectionManager, PooledConnection};
|
use diesel::r2d2::{self, ConnectionManager, PooledConnection};
|
||||||
use diesel::sqlite::SqliteConnection;
|
use diesel::sqlite::SqliteConnection;
|
||||||
|
use diesel::RunQueryDsl;
|
||||||
use diesel_migrations;
|
use diesel_migrations;
|
||||||
use log::info;
|
use log::info;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
@ -18,14 +19,29 @@ pub struct DB {
|
||||||
pool: r2d2::Pool<ConnectionManager<SqliteConnection>>,
|
pool: r2d2::Pool<ConnectionManager<SqliteConnection>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct ConnectionCustomizer {}
|
||||||
|
impl diesel::r2d2::CustomizeConnection<SqliteConnection, diesel::r2d2::Error> for ConnectionCustomizer {
|
||||||
|
fn on_acquire(&self, connection: &mut SqliteConnection) -> Result<(), diesel::r2d2::Error> {
|
||||||
|
let query = diesel::sql_query(r#"
|
||||||
|
PRAGMA busy_timeout = 60000;
|
||||||
|
PRAGMA journal_mode = WAL;
|
||||||
|
PRAGMA synchronous = NORMAL;
|
||||||
|
PRAGMA foreign_keys = ON;
|
||||||
|
"#);
|
||||||
|
query.execute(connection)
|
||||||
|
.map_err(|e| diesel::r2d2::Error::QueryError(e))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl DB {
|
impl DB {
|
||||||
pub fn new(path: &Path) -> Result<DB> {
|
pub fn new(path: &Path) -> Result<DB> {
|
||||||
info!("Database file path: {}", path.to_string_lossy());
|
info!("Database file path: {}", path.to_string_lossy());
|
||||||
let manager = ConnectionManager::<SqliteConnection>::new(path.to_string_lossy());
|
let manager = ConnectionManager::<SqliteConnection>::new(path.to_string_lossy());
|
||||||
let pool = r2d2::Pool::builder()
|
let pool = diesel::r2d2::Pool::builder()
|
||||||
.build(manager)
|
.connection_customizer(Box::new(ConnectionCustomizer {}))
|
||||||
.expect("Failed to create pool."); // TODO handle error
|
.build(manager)?;
|
||||||
|
|
||||||
let db = DB { pool: pool };
|
let db = DB { pool: pool };
|
||||||
db.migrate_up()?;
|
db.migrate_up()?;
|
||||||
Ok(db)
|
Ok(db)
|
||||||
|
|
Loading…
Add table
Reference in a new issue