Skip to content

Commit

Permalink
Merge pull request #222 from vimc/vimc-3702
Browse files Browse the repository at this point in the history
vimc-3702: Backup before rebuild
  • Loading branch information
hillalex authored Apr 27, 2020
2 parents e512e6c + bedd914 commit a65f1e1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: orderly
Title: Lightweight Reproducible Reporting
Version: 1.1.30
Version: 1.1.31
Description: Order, create and store reports from R. By defining a
lightweight interface around the inputs and outputs of an
analysis, a lot of the repetitive work for reproducible research
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# orderly 1.1.31

* `orderly::orderly_rebuild()` creates a dated backup of the database before running, allowing this potentially destructive operation to be recovered from (VIMC-3702)

# orderly 1.1.28

* `orderly::orderly_develop_clean()` no longer deletes artefacts that are re-exported from sources (VIMC-3671, reported by @cewalters)
Expand Down
5 changes: 3 additions & 2 deletions R/db.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ orderly_rebuild <- function(root = NULL, locate = TRUE, verbose = TRUE,
}

if (!if_schema_changed || report_db_needs_rebuild(config)) {
orderly_backup(config, suffix = paste0(".", iso_time_str(Sys.time())))
orderly_log("rebuild", "db")
report_db_rebuild(config, verbose)
invisible(TRUE)
Expand All @@ -202,12 +203,12 @@ orderly_rebuild <- function(root = NULL, locate = TRUE, verbose = TRUE,
## > backup may be performed on a live source database without
## > preventing other database connections from reading or writing to
## > the source database while the backup is underway.
orderly_backup <- function(config = NULL, locate = TRUE) {
orderly_backup <- function(config = NULL, locate = TRUE, suffix = NULL) {
config <- orderly_config_get(config, locate)
if (config$destination$driver[[1]] == "RSQLite") {
curr <- orderly_db_args(config$destination, config)$args$dbname

dest <- path_db_backup(config$root, curr)
dest <- path_db_backup(config$root, curr, suffix)
dir.create(dirname(dest), FALSE, TRUE)

prefix <- paste0(config$root, "/")
Expand Down
9 changes: 7 additions & 2 deletions R/paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ path_changelog_txt <- function(path, type) {
}


path_db_backup <- function(root, file) {
file.path(root, "backup", "db", basename(file), fsep = "/")
path_db_backup <- function(root, file, suffix = NULL) {
if (is.null(suffix)) {
filename <- basename(file)
} else {
filename <- paste0(basename(file), suffix)
}
file.path(root, "backup", "db", filename, fsep = "/")
}


Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-db.R
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,32 @@ test_that("db includes elapsed time", {
expect_equal(d$elapsed,
readRDS(path_orderly_run_rds(p))$meta$elapsed)
})


test_that("rebuild nonempty database with backup", {
skip_on_cran_windows()
path <- prepare_orderly_example("minimal")
id <- orderly_run("example", root = path, echo = FALSE)
orderly_commit(id, root = path)

con <- orderly_db("destination", path)
DBI::dbExecute(con, "UPDATE report_version SET published = 1")
DBI::dbDisconnect(con)

orderly_rebuild(path)

files <- dir(file.path(path, "backup/db"))
expect_equal(length(files), 1)
expect_match(files, "^orderly\\.sqlite\\.[0-9]{8}-[0-9]{6}$")

con1 <- orderly_db("destination", path)
con2 <- DBI::dbConnect(RSQLite::SQLite(),
dbname = file.path(path, "backup/db", files))
expect_equal(
DBI::dbReadTable(con1, "report_version")$published, 0)
expect_equal(
DBI::dbReadTable(con2, "report_version")$published, 1)

DBI::dbDisconnect(con1)
DBI::dbDisconnect(con2)
})

0 comments on commit a65f1e1

Please sign in to comment.