Skip to content

Commit

Permalink
ledger: Add skip clearing if there is a connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Sep 4, 2024
1 parent effce25 commit 76e63e2
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::utils;
use rusqlite::Connection;
use std::{
env,
process::Command,
sync::{Arc, Mutex},
};

Expand Down Expand Up @@ -43,12 +44,26 @@ impl Ledger {
let path = Ledger::get_database_path(path);
let _ = utils::initialize_logger();

tracing::trace!("Creating new database at path {path}");
// Check if database has another connections.
let is_open = {
let ret = Command::new("lsof")
.args([&path])
.output()
.expect("failed to execute process");

ret.stdout.len() != 0
};

let database = Connection::open(path.clone()).unwrap();

Ledger::drop_tables(&database).unwrap();
Ledger::create_tables(&database).unwrap();
// If database has another connections, skip clearing.
if !is_open {
tracing::trace!("Creating new database at path {path}");
Ledger::drop_tables(&database).unwrap();
Ledger::create_tables(&database).unwrap();
}

tracing::trace!("Database connection to {path} is established");

Self {
database: Arc::new(Mutex::new(database)),
Expand Down

0 comments on commit 76e63e2

Please sign in to comment.