Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement postgresIsTransacting() #351

Merged
merged 9 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(Postgres)
export(Redshift)
export(postgresDefault)
export(postgresHasDefault)
export(postgresIsTransacting)
export(postgresWaitForNotify)
exportClasses(PqConnection)
exportClasses(PqDriver)
Expand Down
13 changes: 13 additions & 0 deletions R/PqConnection.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,19 @@ postgresWaitForNotify <- function (conn, timeout = 1) {
if ('pid' %in% names(out)) out else NULL
}

#' Return whether a transaction is ongoing
#'
#' Detect whether the transaction is active for the given connection. A
#' transaction might be started with [dbBegin()] or wrapped within
#' [DBI::dbWithTransaction()].
#' @export
#' @param conn a [PqConnection-class] object, produced by
#' [DBI::dbConnect()]
#' @return A boolean, indicating if a transaction is ongoing.
postgresIsTransacting <- function(conn) {
connection_is_transacting(conn@ptr)
}

#' Determine database type for R vector.
#'
#' @export
Expand Down
20 changes: 20 additions & 0 deletions man/postgresIsTransacting.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-isTransacting.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
context("isTransacting")

test_that("isTransacting detects transactions correctly", {
skip_on_cran()
db <- postgresDefault()
krlmlr marked this conversation as resolved.
Show resolved Hide resolved
expect_false(postgresIsTransacting(con))
dbBegin(con)
expect_true(postgresIsTransacting(con))
dbCommit(con)
expect_false(postgresIsTransacting(con))
})