Skip to content

Commit 0160856

Browse files
author
Stefan Fleck
committed
implement prune_identical_backups() and doc, see #9
1 parent 030ffba commit 0160856

File tree

9 files changed

+111
-0
lines changed

9 files changed

+111
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export(n_backups)
1515
export(newest_backup)
1616
export(oldest_backup)
1717
export(prune_backups)
18+
export(prune_identical_backups)
1819
export(rotate)
1920
export(rotate_date)
2021
export(rotate_rds)

R/BackupQueue.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ BackupQueue <- R6::R6Class(
5959
},
6060

6161

62+
63+
#' @description Delete all identical backups. Uses [tools::md5sum()] to
64+
#' compare the files.
65+
prune_identical = function(){
66+
NotImplementedError()
67+
},
68+
69+
6270
print = function(){
6371
cat(fmt_class(class(self)[[1]]), "\n\n")
6472

R/rotate.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,34 @@ prune_backups <- function(
307307
invisible(file)
308308
}
309309

310+
311+
312+
313+
#' @description `prune_backups()` physically deletes all backups of a file
314+
#' based on `max_backups`
315+
#' @section Side Effects:
316+
#' `prune_backups()` may delete files, depending on `max_backups`.
317+
#' @export
318+
#' @rdname rotate
319+
prune_identical_backups <- function(
320+
file,
321+
dir = dirname(file),
322+
dry_run = FALSE,
323+
verbose = dry_run
324+
){
325+
assert_pure_BackupQueue(file, dir = dir)
326+
assert(is_scalar_character(file))
327+
328+
if (dry_run){
329+
DRY_RUN$activate()
330+
on.exit(DRY_RUN$deactivate())
331+
}
332+
333+
bq <- BackupQueueIndex$new(file, dir = dir)
334+
335+
if (!bq$has_backups)
336+
bq <- BackupQueueDateTime$new(file, dir = dir)
337+
338+
bq$prune_identical()
339+
invisible(file)
340+
}

man/BackupQueue.Rd

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/BackupQueueDate.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/BackupQueueDateTime.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/BackupQueueIndex.Rd

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/rotate.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_rotate.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,36 @@ test_that("backup/rotate dry_run", {
123123

124124
expect_snapshot_unchanged(snap)
125125
})
126+
127+
128+
129+
130+
test_that("BackupQueueIndex: $prune_identical works", {
131+
tf <- file.path(td, "test")
132+
133+
saveRDS(iris, tf)
134+
iris_md5 <- tools::md5sum(tf)
135+
bq <- BackupQueueIndex$new(tf)
136+
on.exit({
137+
bq$prune(0)
138+
unlink(tf)
139+
})
140+
backup(tf)
141+
backup(tf)
142+
rotate(tf)
143+
144+
saveRDS(cars, tf)
145+
cars_md5 <- tools::md5sum(tf)
146+
backup(tf)
147+
saveRDS(cars, tf)
148+
rotate(tf)
149+
150+
saveRDS(iris, tf)
151+
152+
prune_identical_backups(tf)
153+
154+
expect_identical(
155+
unname(tools::md5sum(bq$files$path)),
156+
unname(c(cars_md5, iris_md5))
157+
)
158+
})

0 commit comments

Comments
 (0)