Skip to content

Commit

Permalink
improve with_db_user example reproducibilty
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Feb 4, 2025
1 parent c06d138 commit 915b0ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion R/with.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#' library(RPostgres)
#' con <- dbConnect(Postgres())
#'
#' rls_current_user(con)
#' current1 <- rls_current_user(con)
#' current1
#'
#' dbCreateTable(con, "cars", mtcars)
#' rows_append(
Expand All @@ -30,12 +31,21 @@
#' )
#'
#' dbExecute(con, "CREATE ROLE analysts")
#' dbExecute(con, "GRANT SELECT ON TABLE cars TO analysts")
#'
#' with_db_user(
#' con = con,
#' user = "analysts",
#' code = dbGetQuery(con, "select * from cars")
#' )
#' # current user should be the same as above
#' current2 <- rls_current_user(con)
#' identical(current1, current2)
#'
#' # cleanup
#' dbRemoveTable(con, "cars")
#' dbExecute(con, "DROP ROLE analysts")
#' dbDisconnect(con)
with_db_user <- function(con, user, code) {
original_user <- rls_current_user(con)
defer(dbExecute(con,
Expand Down
12 changes: 11 additions & 1 deletion man/with_db_user.Rd

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

0 comments on commit 915b0ea

Please sign in to comment.