Skip to content

Commit

Permalink
Merge pull request #22 from elreda/check-open-connections-in-unix
Browse files Browse the repository at this point in the history
Check open connections in unix before deleting. Candidate for #5
  • Loading branch information
bjcairns authored Oct 9, 2019
2 parents 68c2168 + d9571bd commit 54cdf46
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
16 changes: 13 additions & 3 deletions R/misc-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Parse file name and path
if (file == "") file <- paste0("ukb-schemas-", date_str, ".sqlite")
full_path <- paste0(path.expand(path), "\\", file)
full_path <- paste0(path.expand(path), "/", file)

# If `file`` exists, session is `interactive()` and `!overwrite`, then prompt
# to overwrite the file
Expand All @@ -22,8 +22,18 @@
}
if (!isTRUE(overwrite))
stop(UKBSCHEMAS_ERRORS$OVERWRITE)
else tryCatch(
file.remove(full_path),
else tryCatch({
if (.Platform$OS.type == "unix"){
sys_command <- paste("lsof", full_path, "| wc -l")
processes_using_file <- as.numeric(system(sys_command, intern = TRUE,
ignore.stdout = TRUE,
ignore.stderr = TRUE))
if (processes_using_file>1) {
stop(UKBSCHEMAS_ERRORS$FAILED_OVERWRITE)
}
}
file.remove(full_path)
},
error = function(err) {
stop(UKBSCHEMAS_ERRORS$FAILED_OVERWRITE)
},
Expand Down
2 changes: 1 addition & 1 deletion R/tidy-schemas.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
dplyr::mutate(.x, parent_id = NA, selectable = NA) %>%
dplyr::mutate(type = type, value = as.character(value)) %>%
dplyr::group_by(encoding_id) %>%
dplyr::mutate(code_id = row_number()) %>%
dplyr::mutate(code_id = dplyr::row_number()) %>%
dplyr::ungroup()
}
)
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-save-db.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ test_that("save_db() fails on overwrite = FALSE, non-interactive", {

test_that("save_db() fails to overwrite when db is connected", {

# This only seems to work on Windows
skip_if_not(.Platform$OS.type == "windows", "Skipping test if not Windows")

db_file <- test_db_file()

expect_error(
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-ukbschemas-db.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ test_that("ukbschemas_db() fails on overwrite = FALSE, non-interactive", {

test_that("ukbschemas_db() fails to overwrite when db is connected", {

# This only seems to work on Windows
skip_if_not(.Platform$OS.type == "windows", "Skipping test if not Windows")

db_file <- test_db_file()

expect_error(
Expand Down

0 comments on commit 54cdf46

Please sign in to comment.