Skip to content

Commit

Permalink
feat: cleanup tempfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson committed Jan 27, 2025
1 parent 5ef75fc commit 836c840
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vrsix"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[lib]
Expand Down
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn vcf_to_sqlite(vcf_path: PathBuf, db_url: String, vcf_uri: Option<String>)
vcf_uri.unwrap_or_else(|| format!("file://{}", vcf_path.to_string_lossy().into_owned()));
let rt = Runtime::new().unwrap();
rt.block_on(load::load_vcf(vcf_path, &db_url, uri_value))?;
let _ = sqlite::cleanup_tempfiles(&db_url);
Ok(())
}

Expand Down
32 changes: 31 additions & 1 deletion rust/src/sqlite.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use log::info;
use sqlx::{migrate::MigrateDatabase, Error, Sqlite, SqlitePool};
use std::fs;
use std::path::PathBuf;

pub async fn get_db_connection(db_url: &str) -> Result<SqlitePool, Error> {
let db_pool = SqlitePool::connect(db_url).await?;
Expand Down Expand Up @@ -41,6 +43,17 @@ pub async fn setup_db(db_url: &str) -> Result<(), Error> {
Ok(())
}

pub fn cleanup_tempfiles(db_url: &str) -> Result<(), Error> {
let owned_db_url = String::from(db_url);
let db_path = owned_db_url.strip_prefix("sqlite://").unwrap();
let mut db_pathbuf = PathBuf::from(db_path);
db_pathbuf.set_extension("db-shm");
let _ = fs::remove_file(db_pathbuf.clone());
db_pathbuf.set_extension("db-wal");
let _ = fs::remove_file(db_pathbuf);
Ok(())
}

#[derive(Debug)]
pub struct DbRow {
pub vrs_id: String,
Expand All @@ -52,12 +65,29 @@ pub struct DbRow {
#[cfg(test)]
mod tests {
use super::*;
use tempfile::NamedTempFile;
use tempfile::{tempdir, NamedTempFile};

#[tokio::test]
async fn test_setup_db() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let db_url = format!("sqlite://{}", temp_file.path().to_str().unwrap());
setup_db(&db_url).await.expect("Setup DB failed");
}

#[test]
fn test_cleanup_tempfiles() {
let temp_dir = tempdir().expect("Failed to create temp file");
let temp_path = temp_dir.path();
let db_file_path = temp_path.join("test.sqlite");
let db_url = format!("sqlite://{}", db_file_path.to_str().unwrap());
let db_shm_path = temp_path.join("test.db-shm");
let db_wal_path = temp_path.join("test.db-wal");
fs::File::create(&db_shm_path).expect("Failed to create db-shm file");
fs::File::create(&db_wal_path).expect("Failed to create db-wal file");
assert!(db_shm_path.exists());
assert!(db_wal_path.exists());
cleanup_tempfiles(&db_url).expect("cleanup_tempfiles failed");
assert!(!db_shm_path.exists());
assert!(!db_wal_path.exists());
}
}

0 comments on commit 836c840

Please sign in to comment.