logging, fix "database locked" errors on init

feat/vaults
Tomáš Mládek 2021-12-05 12:23:02 +01:00
parent c8b8a76bba
commit 10eaab6e2d
1 changed files with 13 additions and 8 deletions

View File

@ -205,23 +205,18 @@ pub fn insert_entry<C: Connection<Backend = Sqlite>>(
#[derive(Debug)]
pub struct ConnectionOptions {
pub enable_foreign_keys: bool,
pub enable_wal_mode: bool,
pub busy_timeout: Option<Duration>,
}
impl ConnectionOptions {
pub fn apply(&self, conn: &SqliteConnection) -> QueryResult<()> {
if self.enable_foreign_keys {
trace!("Enabling foreign keys");
conn.execute("PRAGMA foreign_keys = ON;")?;
}
conn.execute(if self.enable_wal_mode {
"PRAGMA journal_mode = WAL;"
} else {
"PRAGMA journal_mode = TRUNCATE;"
})?;
if let Some(duration) = self.busy_timeout {
trace!("Setting busy_timeout to {:?}", duration);
conn.execute(&format!("PRAGMA busy_timeout = {};", duration.as_millis()))?;
}
Ok(())
@ -255,19 +250,29 @@ pub fn open_upend<P: AsRef<Path>>(
let database_path = db_path.unwrap_or_else(|| dirpath.as_ref().join(DATABASE_FILENAME));
if reinitialize {
trace!("Reinitializing - removing previous database...");
let _ = fs::remove_file(&database_path);
}
let new = !database_path.exists();
trace!("Creating pool.");
let manager = ConnectionManager::<SqliteConnection>::new(database_path.to_str().unwrap());
let pool = r2d2::Pool::builder()
.connection_customizer(Box::new(ConnectionOptions {
enable_foreign_keys: true,
enable_wal_mode: true,
busy_timeout: Some(Duration::from_secs(30)),
}))
.build(manager)?;
let enable_wal_mode = true;
pool.get().unwrap().execute(if enable_wal_mode {
trace!("Enabling WAL journal mode");
"PRAGMA journal_mode = WAL;"
} else {
trace!("Enabling TRUNCATE journal mode");
"PRAGMA journal_mode = TRUNCATE;"
})?;
trace!("Pool created, running migrations...");
embedded_migrations::run_with_output(