Skip to content

Commit

Permalink
implement prune_identical_backups() and doc, see #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Fleck committed Aug 12, 2020
1 parent 030ffba commit 0160856
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export(n_backups)
export(newest_backup)
export(oldest_backup)
export(prune_backups)
export(prune_identical_backups)
export(rotate)
export(rotate_date)
export(rotate_rds)
Expand Down
8 changes: 8 additions & 0 deletions R/BackupQueue.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ BackupQueue <- R6::R6Class(
},



#' @description Delete all identical backups. Uses [tools::md5sum()] to
#' compare the files.
prune_identical = function(){
NotImplementedError()
},


print = function(){
cat(fmt_class(class(self)[[1]]), "\n\n")

Expand Down
31 changes: 31 additions & 0 deletions R/rotate.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,34 @@ prune_backups <- function(
invisible(file)
}




#' @description `prune_backups()` physically deletes all backups of a file
#' based on `max_backups`
#' @section Side Effects:
#' `prune_backups()` may delete files, depending on `max_backups`.
#' @export
#' @rdname rotate
prune_identical_backups <- function(
file,
dir = dirname(file),
dry_run = FALSE,
verbose = dry_run
){
assert_pure_BackupQueue(file, dir = dir)
assert(is_scalar_character(file))

if (dry_run){
DRY_RUN$activate()
on.exit(DRY_RUN$deactivate())
}

bq <- BackupQueueIndex$new(file, dir = dir)

if (!bq$has_backups)
bq <- BackupQueueDateTime$new(file, dir = dir)

bq$prune_identical()
invisible(file)
}
12 changes: 12 additions & 0 deletions man/BackupQueue.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/BackupQueueDate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/BackupQueueDateTime.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions man/BackupQueueIndex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/rotate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions tests/testthat/test_rotate.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,36 @@ test_that("backup/rotate dry_run", {

expect_snapshot_unchanged(snap)
})




test_that("BackupQueueIndex: $prune_identical works", {
tf <- file.path(td, "test")

saveRDS(iris, tf)
iris_md5 <- tools::md5sum(tf)
bq <- BackupQueueIndex$new(tf)
on.exit({
bq$prune(0)
unlink(tf)
})
backup(tf)
backup(tf)
rotate(tf)

saveRDS(cars, tf)
cars_md5 <- tools::md5sum(tf)
backup(tf)
saveRDS(cars, tf)
rotate(tf)

saveRDS(iris, tf)

prune_identical_backups(tf)

expect_identical(
unname(tools::md5sum(bq$files$path)),
unname(c(cars_md5, iris_md5))
)
})

0 comments on commit 0160856

Please sign in to comment.