Skip to content

Commit

Permalink
Merge pull request #342 from r-dbi/f-write-table-savepoint
Browse files Browse the repository at this point in the history
- `dbWriteTable()` uses savepoints for its transactions, even if an external transaction is open. This does not affect Redshift, because savepoints are not supproted there (#342).
  • Loading branch information
krlmlr authored Dec 5, 2021
2 parents 7b0073d + 61e8d06 commit bd136d9
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions R/tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,20 @@ setMethod("dbWriteTable", c("PqConnection", "character", "data.frame"),
need_transaction <- !connection_is_transacting(conn@ptr)
if (need_transaction) {
dbBegin(conn)
on.exit(dbRollback(conn))
}

if (!is(conn, "RedshiftConnection")) {
dbBegin(conn, name = "dbWriteTable")
# This is executed first, the `after` argument requires quite recent R
on.exit({
dbRollback(conn, name = "dbWriteTable")
})
}

if (need_transaction) {
on.exit({
dbRollback(conn)
})
}

found <- dbExistsTable(conn, name)
Expand Down Expand Up @@ -127,10 +140,13 @@ setMethod("dbWriteTable", c("PqConnection", "character", "data.frame"),
db_append_table(conn, name, value, copy, warn = FALSE)
}

if (!is(conn, "RedshiftConnection")) {
dbCommit(conn, name = "dbWriteTable")
}
if (need_transaction) {
dbCommit(conn)
on.exit(NULL)
}
on.exit(NULL)

invisible(TRUE)
}
Expand Down

0 comments on commit bd136d9

Please sign in to comment.