diff --git a/NEWS.md b/NEWS.md index 78ff3aa75..3ccf6c68f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # dbplyr (development version) +* Fixed overwrite flag in `copy_to` to work when source is in the same remote DB + as destination (@liudvikasakelis, #1535) + * Tightened argument checks for SQL translations. These changes should result in more informative errors in cases where code already failed, possibly silently; if you see errors with code that used to run correctly, please report diff --git a/R/verb-compute.R b/R/verb-compute.R index c04a885f7..adcbe0127 100644 --- a/R/verb-compute.R +++ b/R/verb-compute.R @@ -34,6 +34,7 @@ collapse.tbl_sql <- function(x, ...) { compute.tbl_sql <- function(x, name = NULL, temporary = TRUE, + overwrite = FALSE, unique_indexes = list(), indexes = list(), analyze = TRUE, @@ -62,6 +63,7 @@ compute.tbl_sql <- function(x, name <- db_compute(x$src$con, name, sql, temporary = temporary, + overwrite = overwrite, unique_indexes = unique_indexes, indexes = indexes, analyze = analyze, diff --git a/R/verb-copy-to.R b/R/verb-copy-to.R index cd0a2bc50..f9a870a48 100644 --- a/R/verb-copy-to.R +++ b/R/verb-copy-to.R @@ -71,6 +71,7 @@ copy_to.src_sql <- function(dest, out <- compute(df, name = name, temporary = temporary, + overwrite = overwrite, unique_indexes = unique_indexes, indexes = indexes, analyze = analyze, diff --git a/man/collapse.tbl_sql.Rd b/man/collapse.tbl_sql.Rd index c93a83aa2..f75341205 100644 --- a/man/collapse.tbl_sql.Rd +++ b/man/collapse.tbl_sql.Rd @@ -12,6 +12,7 @@ x, name = NULL, temporary = TRUE, + overwrite = FALSE, unique_indexes = list(), indexes = list(), analyze = TRUE, @@ -31,6 +32,10 @@ \item{temporary}{Should the table be temporary (\code{TRUE}, the default) or persistent (\code{FALSE})?} +\item{overwrite}{If \code{TRUE}, will overwrite an existing table with +name \code{name}. If \code{FALSE}, will throw an error if \code{name} already +exists.} + \item{unique_indexes}{a list of character vectors. Each element of the list will create a new unique index over the specified column(s). Duplicate rows will result in failure.} diff --git a/tests/testthat/test-verb-copy-to.R b/tests/testthat/test-verb-copy-to.R index 185d05076..374b7ebe9 100644 --- a/tests/testthat/test-verb-copy-to.R +++ b/tests/testthat/test-verb-copy-to.R @@ -35,6 +35,15 @@ test_that("only overwrite existing table if explicitly requested", { expect_silent(copy_to(con, data.frame(x = 1), name = "df", overwrite = TRUE)) }) +test_that("overwrite flag works when copying *within* db sources", { + con <- local_sqlite_connection() + df <- copy_to(con, tibble(x = 1:10), "df", temporary = FALSE) + copy_to(con, df, "df2", temporary = FALSE) + + expect_error(copy_to(con, df, name = "df2", temporary = FALSE), "exists") + expect_silent(copy_to(con, df, name = "df2", temporary = FALSE, overwrite = TRUE)) +}) + test_that("can create a new table in non-default schema", { con <- local_sqlite_con_with_aux()