Skip to content

Commit

Permalink
Merge pull request #152 from Appsilon/bugFix/#147-PostgreSQL-Connection
Browse files Browse the repository at this point in the history
Added flexibility to select between [RPostgreSQL, RPostgres] drivers
  • Loading branch information
arunkodati77 authored Mar 12, 2024
2 parents 2838e13 + a427351 commit 67dc7ed
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: shiny.telemetry
Title: 'Shiny' App Usage Telemetry
Version: 0.2.0.9000
Version: 0.2.0.9002
Authors@R: c(
person("André", "Veríssimo", , "[email protected]", role = c("aut", "cre")),
person("Kamil", "Żyła", , "[email protected]", role = "aut"),
Expand Down Expand Up @@ -44,6 +44,7 @@ Suggests:
RColorBrewer,
RMariaDB,
RPostgreSQL,
RPostgres,
scales,
semantic.dashboard (>= 0.1.1),
shiny.semantic (>= 0.2.0),
Expand All @@ -55,4 +56,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
Language: en-US
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# shiny.telemetry (development version)

### New Features

- Added flexibility to select between [`RPostgreSQL`, `RPostgres`] drivers (#147).

# shiny.telemetry 0.2.0

### New Features
Expand Down
25 changes: 17 additions & 8 deletions R/data-storage-postgresql.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,23 @@ DataStoragePostgreSQL <- R6::R6Class( # nolint object_name_linter
#' @param password string with the password for the username.
#' @param hostname string with hostname of PostgreSQL instance.
#' @param port numeric value with the port number of PostgreSQL instance.
#' @param dbname string with the name of the database in the PostgreSQL
#' instance.
#' @param dbname string with the name of the database in the PostgreSQL instance.
#' @param driver string, to select PostgreSQL driver among `c("RPostgreSQL", "RPostgres")`.

initialize = function(
username = NULL,
password = NULL,
hostname = "127.0.0.1",
port = 5432,
dbname = "shiny_telemetry"
dbname = "shiny_telemetry",
driver = "RPostgreSQL"
) {
checkmate::assert_string(password)
checkmate::assert_string(username)
checkmate::assert_string(hostname)
checkmate::assert_int(port)
checkmate::assert_string(dbname)
checkmate::assert_choice(driver, choices = c("RPostgreSQL", "RPostgres"))

logger::log_debug(
"Parameters for PostgreSQL:\n",
Expand All @@ -60,7 +62,9 @@ DataStoragePostgreSQL <- R6::R6Class( # nolint object_name_linter
" * db name: {dbname}\n",
namespace = "shiny.telemetry"
)
private$connect(username, password, hostname, port, dbname)
# private method to select the driver
selected_driver <- private$select_driver(driver)
private$connect(username, password, hostname, port, dbname, selected_driver)
private$initialize_connection()
}

Expand All @@ -71,13 +75,18 @@ DataStoragePostgreSQL <- R6::R6Class( # nolint object_name_linter
# Private Fields
db_con = NULL,
timestamp_wrapper = "to_timestamp({seconds})",

select_driver = function(driver) {
if (driver == "RPostgres") {
return(RPostgres::Postgres())
} else { # Default to RPostgreSQL if input is not recognized
return(RPostgreSQL::PostgreSQL())
}
},
# Private methods

connect = function(user, password, hostname, port, dbname) {
connect = function(user, password, hostname, port, dbname, driver) {
# Initialize connection with database
private$db_con <- odbc::dbConnect(
RPostgreSQL::PostgreSQL(),
driver,
user = user,
password = password,
dbname = dbname,
Expand Down
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ JSON
Javascript
Logfile
ODBC
RPostgreSQL
RPostgres
RStudio
Rhinoverse
ShinySession
Expand Down
8 changes: 5 additions & 3 deletions man/DataStoragePostgreSQL.Rd

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

3 changes: 2 additions & 1 deletion tests/testthat/helper-init-data-storage.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ init_test_postgres <- function(.local_envir = parent.frame()) {
password = Sys.getenv("TEST_POSTGRESQL_PASSWORD"),
port = Sys.getenv("TEST_POSTGRESQL_PORT"),
dbname = Sys.getenv("TEST_POSTGRESQL_DBNAME"),
hostname = Sys.getenv("TEST_POSTGRESQL_HOSTNAME")
hostname = Sys.getenv("TEST_POSTGRESQL_HOSTNAME"),
driver = Sys.getenv("TEST_POSTGRESQL_DRIVER")
)
testthat::skip_on_cran()
skip_if_storage_config_missing(storage_config, "PostgreSQL")
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test-data-storage-postgresql.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@

# Test suite common to data storages (see `helper-data_storage.R`)
test_that_common_data_storage(init_test_postgres, "PostgreSQL")
withr::with_envvar(
c("TEST_POSTGRESQL_DRIVER" = "RPostgres"),
code = test_that_common_data_storage(init_test_postgres, "PostgreSQL")
)

withr::with_envvar(
c("TEST_POSTGRESQL_DRIVER" = "RPostgreSQL"),
code = test_that_common_data_storage(init_test_postgres, "PostgreSQL")
)

0 comments on commit 67dc7ed

Please sign in to comment.