Skip to content

Commit

Permalink
Fix postgres upsert column order
Browse files Browse the repository at this point in the history
  • Loading branch information
mgirlich committed Aug 22, 2023
1 parent ba01847 commit 46fc6d3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ S3method(db_col_types,MySQLConnection)
S3method(db_col_types,PostgreSQL)
S3method(db_col_types,PqConnection)
S3method(db_col_types,TestConnection)
S3method(db_col_types,default)
S3method(db_collect,DBIConnection)
S3method(db_compute,DBIConnection)
S3method(db_connection_describe,DBIConnection)
Expand Down
3 changes: 2 additions & 1 deletion R/backend-postgres.R
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ sql_query_upsert.PqConnection <- function(con,
parts <- rows_prep(con, table, from, by, lvl = 0)

insert_cols <- c(by, update_cols)
select_cols <- ident(insert_cols)
insert_cols <- escape(ident(insert_cols), collapse = ", ", parens = TRUE, con = con)

update_values <- set_names(
Expand All @@ -355,7 +356,7 @@ sql_query_upsert.PqConnection <- function(con,
by_sql <- escape(ident(by), parens = TRUE, collapse = ", ", con = con)
clauses <- list(
sql_clause_insert(con, insert_cols, table),
sql_clause_select(con, sql("*")),
sql_clause_select(con, select_cols),
sql_clause_from(parts$from),
# `WHERE true` is required for SQLite
sql("WHERE true"),
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/_snaps/backend-.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# can translate subsetting

Code
test_translate_sql(a[[x]])
Condition
Error in `a[[x]]`:
! Can only index with strings and numbers
Code
test_translate_sql(a[[TRUE]])
Condition
Error in `a[[TRUE]]`:
! Can only index with strings and numbers

# can translate case insensitive like

Code
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/_snaps/backend-postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@

Code
sql_query_upsert(con = con, table = ident("df_x"), from = sql_render(df_y, con,
lvl = 1), by = c("a", "b"), update_cols = c("c", "d"), returning_cols = c("a",
lvl = 1), by = c("c", "d"), update_cols = c("a", "b"), returning_cols = c("a",
b2 = "b"), method = "on_conflict")
Output
<SQL> INSERT INTO `df_x` (`a`, `b`, `c`, `d`)
SELECT *
<SQL> INSERT INTO `df_x` (`c`, `d`, `a`, `b`)
SELECT `c`, `d`, `a`, `b`
FROM (
SELECT `a`, `b`, `c` + 1.0 AS `c`, `d`
FROM `df_y`
) AS `...y`
WHERE true
ON CONFLICT (`a`, `b`)
ON CONFLICT (`c`, `d`)
DO UPDATE
SET `c` = `excluded`.`c`, `d` = `excluded`.`d`
SET `a` = `excluded`.`a`, `b` = `excluded`.`b`
RETURNING `df_x`.`a`, `df_x`.`b` AS `b2`

# can explain
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-backend-postgres.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ test_that("`sql_query_upsert()` with method = 'on_conflict' is correct", {
con = con,
table = ident("df_x"),
from = sql_render(df_y, con, lvl = 1),
by = c("a", "b"),
update_cols = c("c", "d"),
by = c("c", "d"),
update_cols = c("a", "b"),
returning_cols = c("a", b2 = "b"),
method = "on_conflict"
)
Expand Down

0 comments on commit 46fc6d3

Please sign in to comment.