From 9b6c011cddb88e99e0f0a45ec9617d88122dd0ed Mon Sep 17 00:00:00 2001 From: Liudvikas Akelis Date: Fri, 7 Feb 2025 20:00:38 +0200 Subject: [PATCH 1/8] test case that reproduces the bug in SQLite --- tests/testthat/test-verb-copy-to.R | 9 +++++++++ 1 file changed, 9 insertions(+) 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() From 73cd51fd9a4dffd640661b1bb2b2d4a446bd3801 Mon Sep 17 00:00:00 2001 From: Liudvikas Akelis Date: Fri, 7 Feb 2025 20:10:56 +0200 Subject: [PATCH 2/8] Changes that make the test cases pass --- R/verb-compute.R | 2 ++ R/verb-copy-to.R | 1 + 2 files changed, 3 insertions(+) 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, From 6cf6f38e9327f270e0dd407ac0f931b8d4ab433a Mon Sep 17 00:00:00 2001 From: Liudvikas Akelis Date: Fri, 7 Feb 2025 20:48:09 +0200 Subject: [PATCH 3/8] temporary workaround for tests --- tests/testthat/_snaps/tbl-sql.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/_snaps/tbl-sql.md b/tests/testthat/_snaps/tbl-sql.md index f207e90ea..a8b07508f 100644 --- a/tests/testthat/_snaps/tbl-sql.md +++ b/tests/testthat/_snaps/tbl-sql.md @@ -3,7 +3,7 @@ Code mf2 Output - # Source: SQL [3 x 2] + # Source: SQL [?? x 2] # Database: sqlite ?.?.? [:memory:] x y From 8560310573eface558101ee14315d9927cdfa37d Mon Sep 17 00:00:00 2001 From: Liudvikas Akelis Date: Fri, 7 Feb 2025 21:13:55 +0200 Subject: [PATCH 4/8] missed documentation --- man/collapse.tbl_sql.Rd | 5 +++++ 1 file changed, 5 insertions(+) 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.} From 7affdebeed08bc2ae07a1d8bd810ceb3a9a637f2 Mon Sep 17 00:00:00 2001 From: Liudvikas Akelis Date: Wed, 12 Feb 2025 13:08:25 +0200 Subject: [PATCH 5/8] Revert "temporary workaround for tests" This reverts commit 6cf6f38e9327f270e0dd407ac0f931b8d4ab433a. --- tests/testthat/_snaps/tbl-sql.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/_snaps/tbl-sql.md b/tests/testthat/_snaps/tbl-sql.md index a8b07508f..f207e90ea 100644 --- a/tests/testthat/_snaps/tbl-sql.md +++ b/tests/testthat/_snaps/tbl-sql.md @@ -3,7 +3,7 @@ Code mf2 Output - # Source: SQL [?? x 2] + # Source: SQL [3 x 2] # Database: sqlite ?.?.? [:memory:] x y From 00f7921006db651089be59a639ec5c3aca30fdfb Mon Sep 17 00:00:00 2001 From: Liudvikas Akelis Date: Wed, 12 Feb 2025 13:29:13 +0200 Subject: [PATCH 6/8] news.md --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) 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 From 352314a920a284ef7329b02a07e0a47b2543c9b9 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 26 Nov 2025 13:48:17 +0200 Subject: [PATCH 7/8] Re-format with air; update news stylle --- NEWS.md | 4 +--- tests/testthat/test-verb-copy-to.R | 8 +++++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 14c19cfb9..b9e722733 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,6 @@ # dbplyr (development version) -* Fixed overwrite flag in `copy_to` to work when source is in the same remote DB - as destination (@liudvikasakelis, #1535) - +* Fixed overwrite flag in `copy_to()` to work when source is in the same DB as destination (@liudvikasakelis, #1535) * Snowflake correctly translates `$` to `:` (@jsowder, #1608) * `dbplyr_uncount()` now works with Redshift (@owenjonesuob, #1601). diff --git a/tests/testthat/test-verb-copy-to.R b/tests/testthat/test-verb-copy-to.R index e7dd220e8..0ed13313c 100644 --- a/tests/testthat/test-verb-copy-to.R +++ b/tests/testthat/test-verb-copy-to.R @@ -41,7 +41,13 @@ test_that("overwrite flag works when copying *within* db sources", { 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)) + expect_silent(copy_to( + con, + df, + name = "df2", + temporary = FALSE, + overwrite = TRUE + )) }) test_that("can create a new table in non-default schema", { From c4465d0e07205e56bb7e6768a4cc8ac770bbe064 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 26 Nov 2025 13:51:33 +0200 Subject: [PATCH 8/8] Simplify test --- tests/testthat/test-verb-copy-to.R | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test-verb-copy-to.R b/tests/testthat/test-verb-copy-to.R index 0ed13313c..67b1bc747 100644 --- a/tests/testthat/test-verb-copy-to.R +++ b/tests/testthat/test-verb-copy-to.R @@ -37,17 +37,12 @@ test_that("only overwrite existing table if explicitly requested", { 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) + df <- copy_to(con, tibble(x = 1), "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 - )) + df2 <- tibble(x = 2) + out <- copy_to(con, df2, name = "df2", temporary = FALSE, overwrite = TRUE) + expect_equal(collect(out), df2) }) test_that("can create a new table in non-default schema", {