2023-09-07 18:37:29 +00:00
|
|
|
testDatabases <- c("postgresql")
|
|
|
|
|
2023-10-26 18:13:30 +00:00
|
|
|
if (Sys.getenv("CDM5_POSTGRESQL_SERVER") != "") {
|
|
|
|
library(DatabaseConnector)
|
2021-08-20 02:23:39 +00:00
|
|
|
|
2023-10-26 18:13:30 +00:00
|
|
|
if (dir.exists(Sys.getenv("DATABASECONNECTOR_JAR_FOLDER"))) {
|
|
|
|
jdbcDriverFolder <- Sys.getenv("DATABASECONNECTOR_JAR_FOLDER")
|
2023-09-07 18:37:29 +00:00
|
|
|
} else {
|
2023-10-26 18:13:30 +00:00
|
|
|
jdbcDriverFolder <- file.path(tempdir(), "jdbcDrivers")
|
|
|
|
dir.create(jdbcDriverFolder, showWarnings = FALSE)
|
|
|
|
DatabaseConnector::downloadJdbcDrivers("all", pathToDriver = jdbcDriverFolder)
|
2023-09-07 18:37:29 +00:00
|
|
|
|
2023-10-26 18:13:30 +00:00
|
|
|
Sys.setenv(DATABASECONNECTOR_JAR_FOLDER = jdbcDriverFolder)
|
|
|
|
|
|
|
|
withr::defer({
|
|
|
|
unlink(jdbcDriverFolder,
|
|
|
|
recursive = TRUE,
|
|
|
|
force = TRUE)
|
|
|
|
},
|
|
|
|
testthat::teardown_env())
|
|
|
|
}
|
2023-09-07 17:52:20 +00:00
|
|
|
} else {
|
2023-10-26 18:13:30 +00:00
|
|
|
message("Skipping driver setup because environmental variables not set")
|
2021-08-20 02:23:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 18:37:29 +00:00
|
|
|
# Helper functions used in tests
|
2021-08-20 02:23:39 +00:00
|
|
|
getConnectionDetails <- function(dbms) {
|
2023-10-26 18:13:30 +00:00
|
|
|
switch (
|
|
|
|
dbms,
|
2021-08-20 02:23:39 +00:00
|
|
|
"postgresql" = createConnectionDetails(
|
|
|
|
dbms = "postgresql",
|
|
|
|
user = Sys.getenv("CDMDDLBASE_POSTGRESQL_USER"),
|
|
|
|
password = Sys.getenv("CDMDDLBASE_POSTGRESQL_PASSWORD"),
|
|
|
|
server = Sys.getenv("CDMDDLBASE_POSTGRESQL_SERVER"),
|
2023-10-26 18:13:30 +00:00
|
|
|
pathToDriver = jdbcDriverFolder
|
2021-08-20 02:23:39 +00:00
|
|
|
),
|
|
|
|
"redshift" = createConnectionDetails(
|
|
|
|
dbms = "redshift",
|
|
|
|
user = Sys.getenv("CDMDDLBASE_REDSHIFT_USER"),
|
|
|
|
password = Sys.getenv("CDMDDLBASE_REDSHIFT_PASSWORD"),
|
|
|
|
server = Sys.getenv("CDMDDLBASE_REDSHIFT_SERVER"),
|
2023-10-26 18:13:30 +00:00
|
|
|
pathToDriver = jdbcDriverFolder
|
2021-08-20 02:23:39 +00:00
|
|
|
),
|
|
|
|
"sql server" = createConnectionDetails(
|
|
|
|
dbms = "sql server",
|
|
|
|
user = Sys.getenv("CDMDDLBASE_SQL_SERVER_USER"),
|
|
|
|
password = Sys.getenv("CDMDDLBASE_SQL_SERVER_PASSWORD"),
|
|
|
|
server = Sys.getenv("CDMDDLBASE_SQL_SERVER_SERVER"),
|
2023-10-26 18:13:30 +00:00
|
|
|
pathToDriver = jdbcDriverFolder
|
2021-08-20 02:23:39 +00:00
|
|
|
),
|
|
|
|
"oracle" = createConnectionDetails(
|
|
|
|
dbms = "oracle",
|
|
|
|
user = Sys.getenv("CDMDDLBASE_ORACLE_USER"),
|
|
|
|
password = Sys.getenv("CDMDDLBASE_ORACLE_PASSWORD"),
|
|
|
|
server = Sys.getenv("CDMDDLBASE_ORACLE_SERVER"),
|
2023-10-26 18:13:30 +00:00
|
|
|
pathToDriver = jdbcDriverFolder
|
2021-08-20 02:23:39 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
getSchema <- function(dbms) {
|
2023-10-26 18:13:30 +00:00
|
|
|
switch (
|
|
|
|
dbms,
|
2021-08-20 02:23:39 +00:00
|
|
|
"postgresql" = Sys.getenv("CDMDDLBASE_POSTGRESQL_SCHEMA"),
|
|
|
|
"redshift" = Sys.getenv("CDMDDLBASE_REDSHIFT_SCHEMA"),
|
|
|
|
"sql server" = Sys.getenv("CDMDDLBASE_SQL_SERVER_CDM_SCHEMA"),
|
|
|
|
"oracle" = Sys.getenv("CDMDDLBASE_ORACLE_CDM_SCHEMA")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
listTablesInSchema <- function(connectionDetails, schema) {
|
2023-10-26 18:13:30 +00:00
|
|
|
stopifnot(
|
|
|
|
"ConnectionDetails" %in% class(connectionDetails),
|
|
|
|
class(schema) == "character",
|
|
|
|
length(schema) == 1
|
|
|
|
)
|
2021-08-20 02:45:23 +00:00
|
|
|
stopifnot(connectionDetails$dbms %in% c("postgresql", "redshift", "sql server", "oracle"))
|
2021-08-20 02:23:39 +00:00
|
|
|
con <- DatabaseConnector::connect(connectionDetails)
|
|
|
|
on.exit(DatabaseConnector::disconnect(con))
|
|
|
|
dbms <- connectionDetails$dbms
|
2023-10-26 18:13:30 +00:00
|
|
|
if (dbms %in% c("postgresql", "redshift", "sql server")) {
|
|
|
|
tables <-
|
|
|
|
dbGetQuery(
|
|
|
|
con,
|
|
|
|
paste0(
|
|
|
|
"select table_name from information_schema.tables where table_schema = '",
|
|
|
|
schema,
|
|
|
|
"'"
|
|
|
|
)
|
|
|
|
)[[1]]
|
|
|
|
} else if (dbms == "oracle") {
|
|
|
|
query <-
|
|
|
|
paste0(
|
|
|
|
"select table_name from all_tables where owner = '",
|
|
|
|
toupper(schema),
|
|
|
|
"' and tablespace_name = 'USERS'"
|
|
|
|
)
|
2021-08-20 02:23:39 +00:00
|
|
|
tables <- dbGetQuery(con, query)[[1]]
|
|
|
|
}
|
|
|
|
return(tables)
|
|
|
|
}
|
|
|
|
|
|
|
|
dropAllTablesFromSchema <- function(connectionDetails, schema) {
|
2023-10-26 18:13:30 +00:00
|
|
|
stopifnot(
|
|
|
|
"ConnectionDetails" %in% class(connectionDetails),
|
|
|
|
class(schema) == "character",
|
|
|
|
length(schema) == 1
|
|
|
|
)
|
2021-08-20 02:45:23 +00:00
|
|
|
stopifnot(connectionDetails$dbms %in% c("postgresql", "redshift", "sql server", "oracle"))
|
2023-09-07 17:52:20 +00:00
|
|
|
tableNames <- listTablesInSchema(connectionDetails, schema)
|
2021-08-20 02:23:39 +00:00
|
|
|
|
|
|
|
con <- DatabaseConnector::connect(connectionDetails)
|
|
|
|
on.exit(DatabaseConnector::disconnect(con))
|
|
|
|
dbms <- connectionDetails$dbms
|
2023-10-26 18:13:30 +00:00
|
|
|
if (dbms %in% c("redshift", "postgresql", "sql server")) {
|
|
|
|
for (tableName in tableNames) {
|
|
|
|
DBI::dbExecute(con, paste(
|
|
|
|
"DROP TABLE IF EXISTS",
|
|
|
|
paste(schema, tableName, sep = "."),
|
|
|
|
"CASCADE"
|
|
|
|
))
|
2021-08-20 02:23:39 +00:00
|
|
|
}
|
2023-10-26 18:13:30 +00:00
|
|
|
} else if (dbms == "oracle") {
|
|
|
|
for (tableName in tableNames) {
|
|
|
|
DBI::dbExecute(con, paste("DROP TABLE IF EXISTS", tableName, "CASCADE"))
|
2021-08-20 02:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|